我遇到了一个似乎非常受Node / NPM欢迎的问题
我的搜索结果似乎都没有完全解决我的问题......
我使用Restify& NodeJs ...但是一个简单的npm run start
会产生以下错误......
import logger from 'src/modules/amLogger.js';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
我的packake.json文件:
{
"name": "example",
"version": "0.0.1",
"author": "Me@me.com",
"engines": {
"node": ">=8.2.1",
"npm": ">=5.3.0"
},
"license": "MIT",
"dependencies": {
"ascii-art": "^1.4.2",
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-generator": "^6.25.0",
"babel-plugin-lodash": "^3.2.11",
"babel-plugin-syntax-async-functions": "^6.13.0",
"babel-plugin-syntax-trailing-function-commas": "^6.20.0",
"babel-plugin-transform-async-to-module-method": "^6.16.0",
"babel-plugin-transform-class-properties": "^6.19.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
"babel-plugin-transform-export-extensions": "^6.8.0",
"babel-plugin-transform-flow-strip-types": "^6.21.0",
"babel-plugin-transform-object-rest-spread": "^6.20.2",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.24.0",
"babel-register": "^6.18.0",
"canvas": "^1.6.6",
"chalk": "^2.0.1",
"compression": "^1.7.0",
"documentation": "^5.0.1",
"dotenv": "^4.0.0",
"lodash": "^4.17.4",
"pretty-error": "^2.1.1",
"restify": "^5.0.1",
"restify-error": "^1.0.0",
"restify-plugins": "^1.6.0",
"socket.io": "^2.0.3",
"util": "^0.10.3",
"winston": "^2.3.1"
},
"devDependencies": {
"ascii-art": "^1.4.2",
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-generator": "^6.25.0",
"babel-plugin-lodash": "^3.2.11",
"babel-plugin-syntax-async-functions": "^6.13.0",
"babel-plugin-syntax-trailing-function-commas": "^6.20.0",
"babel-plugin-transform-async-to-module-method": "^6.16.0",
"babel-plugin-transform-class-properties": "^6.19.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
"babel-plugin-transform-export-extensions": "^6.8.0",
"babel-plugin-transform-flow-strip-types": "^6.21.0",
"babel-plugin-transform-object-rest-spread": "^6.20.2",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.24.0",
"babel-register": "^6.18.0",
"canvas": "^1.6.6",
"chalk": "^2.0.1",
"compression": "^1.7.0",
"documentation": "^5.0.1",
"dotenv": "^4.0.0",
"es6-promise": "^4.1.1",
"lodash": "^4.17.4",
"pretty-error": "^2.1.1",
"restify": "^5.0.1",
"restify-error": "^1.0.0",
"restify-plugins": "^1.6.0",
"socket.io": "^2.0.3",
"util": "^0.10.3",
"winston": "^2.3.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"am:version": "/bin/sh scripts/display.sh",
"am:pretty-error": "node --require pretty-error/start am.js",
"am:start": "/bin/sh scripts/display.sh && node am.js",
"start": "node am.js"
},
"description": "example",
"main": "am.js"
}
这是我的am.js文件
'use strict';
require('dotenv').config();
import logger from 'src/modules/amLogger.js';
import chalk from 'chalk';
import util from 'util';
/**
* Module Dependencies
*/
const restify = require('restify'),
winston = require('winston'),
errs = require('restify-errors'),
art = require('ascii-art'),
compression = require('compression');
// create the Restify Server
const server = restify.createServer({
name: 'example',
version: 'v0.0.1',
...
错误发生在import logger from 'src/modules/amLogger.js';
但是如果我注释掉这一行错误只会移到下一个导入语句 - 我也尝试在没有'use strict';
我错过了什么?
答案 0 :(得分:5)
Node不支持es6导入,你有一篇解释它的文章 https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c