当我运行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.