我不明白是怎么回事。
节点v5.6.0
NPM v3.10.6
代码:
function (exports, require, module, __filename, __dirname) {
import express from 'express'
};
错误:
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
Node 13+从Node 13开始,你可以使用.mjs扩展名,或者在package.json中设置{"type": "module"}。你不需要使用——experimental-modules标志。Modules现在在node.js中被标记为稳定
Node 12从Node 12开始,你可以使用.mjs扩展名,或者在package.json中设置"type": "module"。你需要带着——experimental-modules标志运行node。
节点9在节点9中,它被启用在一个标志后面,并使用.mjs扩展名。
node --experimental-modules my-app.mjs
虽然导入确实是ES6的一部分,但不幸的是,它在默认情况下还不被NodeJS支持,直到最近才在浏览器中获得支持。
查看浏览器compat表上的MDN和这个节点的问题。
摘自James M Snell关于Node.js中ES6模块的更新(2017年2月):
工作正在进行中,但需要一些时间——我们目前预计至少需要一年。
在本地支持出现之前(现在在Node 13+中标记为稳定),你必须继续使用经典的require语句:
const express = require("express");
如果你真的想在NodeJS中使用新的ES6/7特性,你可以使用Babel编译它。下面是一个服务器示例。
Node 13+从Node 13开始,你可以使用.mjs扩展名,或者在package.json中设置{"type": "module"}。你不需要使用——experimental-modules标志。Modules现在在node.js中被标记为稳定
Node 12从Node 12开始,你可以使用.mjs扩展名,或者在package.json中设置"type": "module"。你需要带着——experimental-modules标志运行node。
节点9在节点9中,它被启用在一个标志后面,并使用.mjs扩展名。
node --experimental-modules my-app.mjs
虽然导入确实是ES6的一部分,但不幸的是,它在默认情况下还不被NodeJS支持,直到最近才在浏览器中获得支持。
查看浏览器compat表上的MDN和这个节点的问题。
摘自James M Snell关于Node.js中ES6模块的更新(2017年2月):
工作正在进行中,但需要一些时间——我们目前预计至少需要一年。
在本地支持出现之前(现在在Node 13+中标记为稳定),你必须继续使用经典的require语句:
const express = require("express");
如果你真的想在NodeJS中使用新的ES6/7特性,你可以使用Babel编译它。下面是一个服务器示例。
作为Node.js v12(这可能是相当稳定的现在,但仍然标记为“实验”),你有两个选项使用ESM (ECMAScript模块)在Node.js(文件,有第三种方法计算字符串),以下是文档所说的:
The --experimental-modules flag can be used to enable support for
ECMAScript modules (ES modules).
Once enabled, Node.js will treat the following as ES modules when passed to
node as the initial input, or when referenced by import statements within
ES module code:
Files ending in .mjs.
Files ending in .js, or extensionless files, when the nearest parent
package.json file contains a top-level field "type" with a value of
"module".
Strings passed in as an argument to --eval or --print, or piped to
node via STDIN, with the flag --input-type=module.
Node.js will treat as CommonJS all other forms of input, such as .js files
where the nearest parent package.json file contains no top-level "type"
field, or string input without the flag --input-type. This behavior is to
preserve backward compatibility. However, now that Node.js supports both
CommonJS and ES modules, it is best to be explicit whenever possible. Node.js
will treat the following as CommonJS when passed to node as the initial input,
or when referenced by import statements within ES module code:
Files ending in .cjs.
Files ending in .js, or extensionless files, when the nearest parent
package.json file contains a top-level field "type" with a value of
"commonjs".
Strings passed in as an argument to --eval or --print, or piped to
node via STDIN, with the flag --input-type=commonjs.