我试图在Node.js中获得ES6导入的窍门,并试图使用本示例中提供的语法:
Cheatsheet链接
我正在查看支持表,但我无法找到支持新的导入语句的版本(我尝试寻找文本import/require)。我目前正在运行Node.js 8.1.2,也相信由于备考表引用的是.js文件,它应该与.js文件一起工作。
当我运行代码时(摘自备忘单的第一个例子):
import { square, diag } from 'lib';
我得到了错误:
SyntaxError:意外的令牌导入。
引用库,我试图导入:
//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}
我错过了什么,我如何才能让节点识别我的导入语句?
我只是想在JavaScript文件中使用导入和导出。
每个人都说这不可能。但是,截至2018年5月,可以在普通的Node.js中使用以上内容,而不需要任何像Babel等模块。
这里有一个简单的方法。
创建下面的文件,运行并自己查看输出。
别忘了看下面的解释。
文件myfile.mjs
function myFunc() {
console.log("Hello from myFunc")
}
export default myFunc;
文件index.mjs
import myFunc from "./myfile.mjs" // Simply using "./myfile" may not work in all resolvers
myFunc();
Run
node --experimental-modules index.mjs
输出
(node:12020) ExperimentalWarning: The ESM module loader is experimental.
Hello from myFunc
解释:
Since it is experimental modules, .js files are named .mjs files
While running you will add --experimental-modules to the node index.mjs
While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental.
"
I have the current release of Node.js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
Someday in the future, you could use .js instead of .mjs, as the features become stable instead of Experimental.
More on experimental features, see: https://nodejs.org/api/esm.html
Node.js已经包含了对ES6支持的实验性支持。
更多信息请访问:https://nodejs.org/docs/latest-v13.x/api/esm.html#esm_enabling。
TLDR;
Node.js >= v13
这在Node.js 13及以上版本中非常简单。你需要:
保存扩展名为.mjs或
在最近的package.json中添加{"type": "module"}。
您只需要执行上面的其中一项,就可以使用ECMAScript模块。
Node.js <= v12
如果你使用的是Node.js 9.6 - 12版本,保存带有.mjs扩展名的ES6模块文件,并像这样运行它:
node --experimental-modules my-app.mjs
我只是想在JavaScript文件中使用导入和导出。
每个人都说这不可能。但是,截至2018年5月,可以在普通的Node.js中使用以上内容,而不需要任何像Babel等模块。
这里有一个简单的方法。
创建下面的文件,运行并自己查看输出。
别忘了看下面的解释。
文件myfile.mjs
function myFunc() {
console.log("Hello from myFunc")
}
export default myFunc;
文件index.mjs
import myFunc from "./myfile.mjs" // Simply using "./myfile" may not work in all resolvers
myFunc();
Run
node --experimental-modules index.mjs
输出
(node:12020) ExperimentalWarning: The ESM module loader is experimental.
Hello from myFunc
解释:
Since it is experimental modules, .js files are named .mjs files
While running you will add --experimental-modules to the node index.mjs
While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental.
"
I have the current release of Node.js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
Someday in the future, you could use .js instead of .mjs, as the features become stable instead of Experimental.
More on experimental features, see: https://nodejs.org/api/esm.html
如果您在服务器端使用模块系统,则根本不需要使用Babel。要在Node.js中使用模块,请确保:
使用支持——experimental-modules标志的node版本
你的*.js文件必须重命名为*.mjs
就是这样。
However and this is a big however, while your shinny pure ES6 code will run in an environment like Node.js (e.g., 9.5.0) you will still have the craziness of transpilling just to test. Also bear in mind that Ecma has stated that release cycles for JavaScript are going to be faster, with newer features delivered on a more regular basis. Whilst this will be no problems for single environments like Node.js, it's a slightly different proposition for browser environments. What is clear is that testing frameworks have a lot to do in catching up. You will still need to probably transpile for testing frameworks. I'd suggest using Jest.
还要注意捆绑框架。你会在那里遇到问题。