当前位置:  开发笔记 > 编程语言 > 正文

SyntaxError:意外的令牌导入

如何解决《SyntaxError:意外的令牌导入》经验,为你挑选了0个好方法。

当我运行index.js时给出错误SyntaxError: Unexpected token import.虽然我使用babel将ES6转换为ES5.

的package.json

{
  "name": "espract",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "build": "babel src -d lib"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.3.17"
  }
}

Person.js

'use strict';

module.exports = class Person {
    constructor(firstname, lastname) {
        this.firstName = firstname;
        this.lastName = lastname;
    }

    greet() {
        console.log(`Hello, ${ this.firstName } ${ this.lastName }`);
    }
};

index.js

import * as Person from './lib/Person';


//es class inherit Person
class Policeman extends Person {

    constructor(firstname, lastname, badgenumber) {
        //call parent constructor
        super(firstname, lastname, badgenumber);
        this.badgeNumber = badgenumber;
    }

    greet(){
        //call parent class method
        super.greet();
        console.log(`License: ${this.badgeNumber}`);
    }

}

var officer = new Policeman("John", "Solan", "1231341314134");
officer.greet();

注意:当我使用var Person = require('./lib/Person')它的工作时绝对没问题.

请指导我,我刚开始学习nodejs.

推荐阅读
pan2502851807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有