我试图在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
我只是想在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
解决方案
https://www.npmjs.com/package/babel-register
// This is to allow ES6 export syntax
// to be properly read and processed by node.js application
require('babel-register')({
presets: [
'env',
],
});
// After that, any line you add below that has typical ES6 export syntax
// will work just fine
const utils = require('../../utils.js');
const availableMixins = require('../../../src/lib/mixins/index.js');
下面是mixins/index.js文件的定义
export { default as FormValidationMixin } from './form-validation'; // eslint-disable-line import/prefer-default-export
这在我的Node.js CLI应用程序中工作得很好。