如何在节点中使用es6导入?

时间:2022-06-22 03:21:17

I'm trying to get the hang of es6 imports in node and am trying to use the syntax provided in this example:

我试图在节点中获取es6导入的挂起,并尝试使用此示例中提供的语法:

Cheatsheet Link: https://hackernoon.com/import-export-default-require-commandjs-javascript-nodejs-es6-vs-cheatsheet-different-tutorial-example-5a321738b50f

Cheatsheet链接:https://hackernoon.com/import-export-default-require-commandjs-javascript-nodejs-es6-vs-cheatsheet-different-tutorial-example-5a321738b50f

I'm looking through the support table: http://node.green/, but was not able to find what version supports the new import statements (I tried looking for the text import/require) I'm currently running node 8.1.2 and also believe that since the cheatsheet is referring to .js files it should work with .js files.

我正在查看支持表:http://node.green/,但无法找到支持新导入语句的版本(我尝试查找文本import / require)我正在运行节点8.1。 2并且还认为,由于cheatsheet是指.js文件,它应该与.js文件一起使用。

As I run the code (taken from cheatsheet's 1st example):

当我运行代码时(取自cheatsheet的第一个例子):

import { square, diag } from 'lib';

I get the error: SyntaxError: Unexpected token import.

我收到错误:SyntaxError:意外的令牌导入。

Reference to lib I'm trying to import:

参考lib我正在尝试导入:

//------ 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));
}

What am I missing and how can I get node to recognize my import statement?

我缺少什么,如何让节点识别我的import语句?

6 个解决方案

#1


26  

Node.js has included experimental support for ES6 support. Read more about here: https://nodejs.org/api/esm.html.

Node.js包含了对ES6支持的实验性支持。在此处阅读更多信息:https://nodejs.org/api/esm.html。

TLDR; Save the file with ES6 modules with .mjs extension and run it like:

TLDR;使用扩展名为.mjs的ES6模块保存文件并运行如下:

node --experimental-modules my-app.mjs

Node.js doesn't support ES6 modules. This blog by James describes the reasons for it. Though you can use Babel to use ES6 modules syntax.

Node.js不支持ES6模块。詹姆斯的这篇博客描述了它的原因。虽然您可以使用Babel来使用ES6模块语法。

#2


11  

If you are using the modules system on the server side, you do not need to use Babel at all. To use modules in NodeJS ensure that:

如果您在服务器端使用模块系统,则根本不需要使用Babel。要在NodeJS中使用模块,请确保:

  1. Use a version of node that supports the --experimental-modules flag
  2. 使用支持--experimental-modules标志的节点版本
  3. Your .js files must then be renamed to .mjs
  4. 然后必须将.js文件重命名为.mjs

That's it.

而已。

However and this is a big however, while your shinny pure ES6 code will run in an environment like NodeJS (at writing 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 NodeJS, its 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.

然而,这是一个很大的问题,而你的闪亮纯ES6代码将运行在像NodeJS这样的环境中(编写9.5.0),你仍然会有疯狂的疯狂只是为了测试。另外请记住,Ecma已经声明Javascript的发布周期会更快,更新的功能会定期提供。虽然对于像NodeJS这样的单一环境来说这不会有任何问题,但它对于浏览器环境来说却略有不同。很明显,测试框架在追赶方面有很多工作要做。您仍然可能需要转换测试框架。我建议用jest。

Also be aware of bundling frameworks, you will be running into problems there

还要注意捆绑框架,你会遇到问题

#3


8  

Wasted about 3 hours.

浪费了大约3个小时。

I just wanted to use the import and export in js files.

我只是想在js文件中使用导入和导出。

Everyone says not possible. But, as of May 2018, it's possible to use above in plain node.js, without any modules like babel, etc.

每个人都说不可能。但是,从2018年5月开始,可以在普通的node.js中使用上面的内容,而不需要像babel等那样的模块。

Here is a simple way to do it.

这是一个简单的方法。

Create below files, run and see output for yourself.

创建以下文件,运行并查看输出。

Also don't forget to see Explanation below.

另外别忘了看下面的说明。

myfile.mjs

function myFunc() {
    console.log("Hello from myFunc")
}

export default myFunc;

index.mjs

import myFunc from "./myfile"

myFunc();

run

node  --experimental-modules  index.mjs

output

(node:12020) ExperimentalWarning: The ESM module loader is experimental.

Hello from myFunc

Explanation:

说明:

  1. Since it is experimental modules, .js files are named .mjs files
  2. 由于它是实验模块,.js文件被命名为.mjs文件
  3. While running you will add "--experimental-modules" to the "node index.js"
  4. 在运行时,您将“--experimental-modules”添加到“node index.js”
  5. While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. "
  6. 在输出中使用实验模块运行时,您将看到:“(node:12020)ExperimentalWarning:ESM模块加载器是实验性的。”
  7. I have current release of node js, so if run "node --version", it givese me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
  8. 我当前发布了节点js,所以如果运行“node --version”,它会给我“v10.3.0”,尽管LTE /稳定/推荐版本是8.11.2 LTS。
  9. Some day in future, you could use .js instead of .mjs, as the features you become stable instead of Experimental.
  10. 将来的某一天,你可以使用.js代替.mjs,因为你变得稳定而不是实验。
  11. To use experimental features, see: https://nodejs.org/api/esm.html
  12. 要使用实验性功能,请参阅:https://nodejs.org/api/esm.html

Hope that helped.

希望有所帮助。

#4


4  

Back to Jonathan002's original question about

回到Jonathan002的原始问题

"... what version supports the new ES6 import statements?"

“......什么版本支持新的ES6导入语句?”

based on the article by Dr. Axel Rauschmayer, there is a plan to have it supported by default (without the experimental command line flag) in Node.js 10.x LTS. According to node.js's release plan as it is on 3/29, 2018, it's likely to become available after Apr 2018, while LTS of it will begin on October 2018.

根据Axel Rauschmayer博士的文章,计划在Node.js 10.x LTS中默认支持它(没有实验命令行标志)。根据node.js的发布计划,即2018年3月29日,它可能会在2018年4月之后上市,而它的LTS将于2018年10月开始。

#5


3  

You may try esm.

你可以试试esm。

Here are some introduction: https://www.npmjs.com/package/esm

以下是一些介绍:https://www.npmjs.com/package/esm

#6


0  

solution

https://www.npmjs.com/package/babel-register

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');

below is definition of mixins/index.js

export { default as FormValidationMixin } from './form-validation'; // eslint-disable-line import/prefer-default-export

that worked just fine inside my node.js CLI app.

在我的node.js CLI应用程序中运行得很好。

#1


26  

Node.js has included experimental support for ES6 support. Read more about here: https://nodejs.org/api/esm.html.

Node.js包含了对ES6支持的实验性支持。在此处阅读更多信息:https://nodejs.org/api/esm.html。

TLDR; Save the file with ES6 modules with .mjs extension and run it like:

TLDR;使用扩展名为.mjs的ES6模块保存文件并运行如下:

node --experimental-modules my-app.mjs

Node.js doesn't support ES6 modules. This blog by James describes the reasons for it. Though you can use Babel to use ES6 modules syntax.

Node.js不支持ES6模块。詹姆斯的这篇博客描述了它的原因。虽然您可以使用Babel来使用ES6模块语法。

#2


11  

If you are using the modules system on the server side, you do not need to use Babel at all. To use modules in NodeJS ensure that:

如果您在服务器端使用模块系统,则根本不需要使用Babel。要在NodeJS中使用模块,请确保:

  1. Use a version of node that supports the --experimental-modules flag
  2. 使用支持--experimental-modules标志的节点版本
  3. Your .js files must then be renamed to .mjs
  4. 然后必须将.js文件重命名为.mjs

That's it.

而已。

However and this is a big however, while your shinny pure ES6 code will run in an environment like NodeJS (at writing 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 NodeJS, its 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.

然而,这是一个很大的问题,而你的闪亮纯ES6代码将运行在像NodeJS这样的环境中(编写9.5.0),你仍然会有疯狂的疯狂只是为了测试。另外请记住,Ecma已经声明Javascript的发布周期会更快,更新的功能会定期提供。虽然对于像NodeJS这样的单一环境来说这不会有任何问题,但它对于浏览器环境来说却略有不同。很明显,测试框架在追赶方面有很多工作要做。您仍然可能需要转换测试框架。我建议用jest。

Also be aware of bundling frameworks, you will be running into problems there

还要注意捆绑框架,你会遇到问题

#3


8  

Wasted about 3 hours.

浪费了大约3个小时。

I just wanted to use the import and export in js files.

我只是想在js文件中使用导入和导出。

Everyone says not possible. But, as of May 2018, it's possible to use above in plain node.js, without any modules like babel, etc.

每个人都说不可能。但是,从2018年5月开始,可以在普通的node.js中使用上面的内容,而不需要像babel等那样的模块。

Here is a simple way to do it.

这是一个简单的方法。

Create below files, run and see output for yourself.

创建以下文件,运行并查看输出。

Also don't forget to see Explanation below.

另外别忘了看下面的说明。

myfile.mjs

function myFunc() {
    console.log("Hello from myFunc")
}

export default myFunc;

index.mjs

import myFunc from "./myfile"

myFunc();

run

node  --experimental-modules  index.mjs

output

(node:12020) ExperimentalWarning: The ESM module loader is experimental.

Hello from myFunc

Explanation:

说明:

  1. Since it is experimental modules, .js files are named .mjs files
  2. 由于它是实验模块,.js文件被命名为.mjs文件
  3. While running you will add "--experimental-modules" to the "node index.js"
  4. 在运行时,您将“--experimental-modules”添加到“node index.js”
  5. While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. "
  6. 在输出中使用实验模块运行时,您将看到:“(node:12020)ExperimentalWarning:ESM模块加载器是实验性的。”
  7. I have current release of node js, so if run "node --version", it givese me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS.
  8. 我当前发布了节点js,所以如果运行“node --version”,它会给我“v10.3.0”,尽管LTE /稳定/推荐版本是8.11.2 LTS。
  9. Some day in future, you could use .js instead of .mjs, as the features you become stable instead of Experimental.
  10. 将来的某一天,你可以使用.js代替.mjs,因为你变得稳定而不是实验。
  11. To use experimental features, see: https://nodejs.org/api/esm.html
  12. 要使用实验性功能,请参阅:https://nodejs.org/api/esm.html

Hope that helped.

希望有所帮助。

#4


4  

Back to Jonathan002's original question about

回到Jonathan002的原始问题

"... what version supports the new ES6 import statements?"

“......什么版本支持新的ES6导入语句?”

based on the article by Dr. Axel Rauschmayer, there is a plan to have it supported by default (without the experimental command line flag) in Node.js 10.x LTS. According to node.js's release plan as it is on 3/29, 2018, it's likely to become available after Apr 2018, while LTS of it will begin on October 2018.

根据Axel Rauschmayer博士的文章,计划在Node.js 10.x LTS中默认支持它(没有实验命令行标志)。根据node.js的发布计划,即2018年3月29日,它可能会在2018年4月之后上市,而它的LTS将于2018年10月开始。

#5


3  

You may try esm.

你可以试试esm。

Here are some introduction: https://www.npmjs.com/package/esm

以下是一些介绍:https://www.npmjs.com/package/esm

#6


0  

solution

https://www.npmjs.com/package/babel-register

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');

below is definition of mixins/index.js

export { default as FormValidationMixin } from './form-validation'; // eslint-disable-line import/prefer-default-export

that worked just fine inside my node.js CLI app.

在我的node.js CLI应用程序中运行得很好。