📜  如何在 Node.js 中使用 ES6 导入?

📅  最后修改于: 2022-05-13 01:56:45.190000             🧑  作者: Mango

如何在 Node.js 中使用 ES6 导入?

ES6导入简介:

import 语句用于导入由其他模块导出的模块。模块是包含一段可重用代码的文件。无论是否声明,导入模块都处于严格模式。

导入语法:

import name from 'module-name'

导入可以通过多种方式完成:

  1. 导入整个模块:

    import * as name from 'module-name'
  2. 从模块导入默认导出:

    import name from 'module-name'
  3. 从模块导入单个导出:

    import { name } from 'module-name'
  4. 从一个模块导入多个导出:

    import { nameOne , nameTwo } from 'module-name'
  5. 仅为副作用导入模块

    import './module-name'

    Node js 不直接支持 ES6 导入。如果我们尝试使用 import 直接在 node js 中导入模块,它会抛出错误。例如,如果我们尝试通过编写import express from 'express' node js 来导入 express 模块,则会抛出如下错误:

    Node 对 ES 模块有实验性的支持。要启用它们,我们需要对 package.json 文件进行一些更改。在执行这些步骤之前,请确保已安装 Node。以下是实现相同的步骤。

    1. package.json文件中添加“type” : “module” 。添加它启用 ES6 模块。

      package.json文件应如下所示:

      package.json
      //package.json
      {
        "name": "index",
        "version": "1.0.0",
        "description": "",
        "main": "index.js",
        "type": "module",
        "scripts": {
          "test": "echo \"Error: no test specified\" && exit 1"
        },
        "keywords": [],
        "author": "",
        "license": "ISC"
      }


      index.js file
      //index.js
        
      import express from 'express';
        
      const app = express();
        
      app.get('/',(req,res) => {
          res.send('GeeksforGeeks');
      })
        
      const PORT = 5000;
        
      app.listen(PORT,() => {
          console.log(`Running on PORT ${PORT}`);
      })


      Javascript
      // package.json when using .mjs file
      {
        "name": "index",
        "version": "1.0.0",
        "description": "",
        "main": "index.js",
        "scripts": {
          "test": "echo \"Error: no test specified\" && exit 1"
        },
        "keywords": [],
        "author": "",
        "license": "ISC"
      }


      Javascript
      //index.mjs
        
      import express from 'express';
        
      const app = express();
        
      app.get('/',(req,res) => {
          res.send('GeeksforGeeks');
      })
        
      const PORT = 5000;
        
      app.listen(PORT,() => {
          console.log(`Running on PORT ${PORT}`);
      })


    2. 创建文件 index.js 并使用 ES6 导入编写程序。例如,让我们尝试在 index.js 文件中导入 express

      index.js 文件

      //index.js
        
      import express from 'express';
        
      const app = express();
        
      app.get('/',(req,res) => {
          res.send('GeeksforGeeks');
      })
        
      const PORT = 5000;
        
      app.listen(PORT,() => {
          console.log(`Running on PORT ${PORT}`);
      })
      

    现在通过在终端中键入node –experimental-modules index.js来运行 index.js 文件。

    使用 esm 模块:

    另一种方法是创建一个扩展名为 .mjs的文件。如果我们使用扩展名为 .mjs 的文件,那么我们不必在 package.json 文件中添加“type”:“module” 。我们可以直接编写程序,在终端输入node –experimental-modules index.mjs即可执行。

    现在,当使用扩展名为 .mjs 的文件时,package.json 文件将如下所示:

    Javascript

    // package.json when using .mjs file
    {
      "name": "index",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC"
    }
    

    创建文件 index.mjs 并使用 ES6 导入编写程序。

    Javascript

    //index.mjs
      
    import express from 'express';
      
    const app = express();
      
    app.get('/',(req,res) => {
        res.send('GeeksforGeeks');
    })
      
    const PORT = 5000;
      
    app.listen(PORT,() => {
        console.log(`Running on PORT ${PORT}`);
    })
    

    现在在终端类型node –experimental-modules index.mjs中。这将执行文件,应用程序现在将在 PORT 5000 上运行。

    使用 esm 模块

    安装

    npm install esm

    现在尝试通过在终端中键入node -r esm index.js来执行之前在 index.js 文件中编写的程序。

    使用esm 模块的另一种方法是创建另一个文件,例如 server.js,它在实际应用程序之前加载 esm。在 server.js 文件中写入以下代码

    //server.js
    require = require("esm")(module);
    module.exports = require("./index.js");

    注意:在文件server.js中,我们正在导入 index.js 文件,该文件包含需要执行的实际程序。

    现在在终端输入node server.js来执行程序

    上述方法中index.jsindex.mjs文件的输出为:

    本地主机:5000

    在 nodejs 中使用 import 代替 require 的优点:

    • 导入有助于有选择地加载有助于节省内存的代码片段。
    • 如果需要加载是同步的,而导入可以是异步的,因此它的性能比要求的要好。