问题:
I don't understand what is wrong. Node v5.6.0 NPM v3.10.6
The code:
function (exports, require, module, __filename, __dirname) {
import express from 'express'
};...
可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:
问题:
I don't understand what is wrong. Node v5.6.0 NPM v3.10.6
The code:
function (exports, require, module, __filename, __dirname) {
import express from 'express'
};
The error:
SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:140:18)
at node.js:1001:3
回答1:
Update: In Node 9, it is enabled behind a flag, and uses the .mjs
extension.
node --experimental-modules my-app.mjs
While import
is indeed part of ES6, it is unfortunately not yet supported in NodeJS by default, and has only very recently landed support in browsers.
See browser compat table on MDN and this Node issue.
From James M Snell's Update on ES6 Modules in Node.js (February 2017):
Work is in progress but it is going to take some time — We’re currently looking at around a year at least.
Until support shows up natively, you'll have to continue using classic require
statements:
const express = require("express");
If you really want to use new ES6/7 features in NodeJS, you can compile it using Babel. Here's an example server.
回答2:
Unfortunately, Node.js doesn't support ES6's import
yet.
To accomplish what you're trying to do (import the Express module), this code should suffice
var express = require("express");
Also, be sure you have Express installed by running
$ npm install express
See the Node.js Docs for more information about learning Node.js.
回答3:
Error: SyntaxError: Unexpected token import or SyntaxError: Unexpected token export
Solution: Change all your imports as example
const express = require('express');
const webpack = require('webpack');
const path = require('path');
const config = require('../webpack.config.dev');
const open = require('open');
And also change your export default = foo;
to module.exports = foo;
回答4:
As mentioned in other answers Node JS currently doesn't support ES6 imports
Enable ES6 imports in node js provides a solution to this issue. I have tired this and it worked for me.
Run the command:
npm install babel-register babel-preset-env --save-dev
Now you need to create a new file(config.js) and add the following code to it.
require('babel-register')({
presets: [ 'env' ]
})
// Import the rest of our application.
module.exports = require('./your_server_file.js')
Now you can write import statements without getting any errors.
Hope this helps.
EDIT:
You need to run the new file which you created with above code. In my case it was config.js
. So i have to run:
node config.js
回答5:
if you can use 'babel', try to add build scripts in package.json(--presets=es2015) as below. it make to precompile import code to es2015
"build": "babel server --out-dir build --presets=es2015 && webpack"
回答6:
In case that you still can't use "import" here is how I handled it: Just translate it to a node friendly require. Example:
import { parse } from 'node-html-parser';
Is the same as:
const parse = require('node-html-parser').parse;
回答7:
In my case it was looking after .babelrc
file, and it should contain something like this:
{
"presets": ["es2015-node5", "stage-3"],
"plugins": []
}