commonjs模块_在节点js中将es模块与commonjs模块一起使用

本文翻译自Medium文章,探讨如何在Node.js环境中同时使用ES模块和CommonJS模块,详细阐述了两者在实际项目中的配合使用方法。
摘要由CSDN通过智能技术生成

commonjs模块

This is not meant to be a comprehensive overview of ES modules and CommonJS modules — I do not go into depth about what modules are and how they work. Rather, it is meant to highlight the differences between ES modules and CommonJS modules and how you can use them together.

这并不是要对ES模块和CommonJS模块进行全面的概述—我不会深入探讨什么是模块以及它们如何工作。 而是要强调ES模块和CommonJS模块之间的差异以及如何一起使用它们。

This is part two of a four part series.

这是四部分系列的第二部分。

  1. The three differences between require and import in Node.js

    Node.js中require和import之间的三个区别

  2. Using ES modules with CommonJS modules in Node.js

    将ES模块与Node.js中的CommonJS模块一起使用
  3. Using ES modules with CommonJS modules in the browser

    在浏览器中将ES模块与CommonJS模块一起使用

  4. Using ES modules with CommonJS modules with webpack

    将ES模块与带有Webpack的CommonJS模块一起使用

Check out full code examples here: https://github.com/arcticmatt/javascript_modules/tree/master/node.

在此处查看完整的代码示例: https : //github.com/arcticmatt/javascript_modules/tree/master/node

什么是模块? (What is a module?)

A module is just a single JavaScript file.

模块只是一个JavaScript文件。

package.json “type ”字段 (package.json “type” field)

This field determines whether JavaScript files, i.e. files ending in .js, are treated as ES modules or CommonJS modules.

该字段确定JavaScript文件(即以.js结尾的文件)是否被视为ES模块或CommonJS模块。

ExampleSee the last field. Valid values are:

示例请参见最后一个字段。 有效值为:

  • "module"

    "module"

  • "commonjs"

    "commonjs"

If it is not specified, it implicitly defaults to "commonjs".

如果未指定,则默认为"commonjs"

{
"name": "es_module_playground",
"version": "1.0.0",
"description": "Testing ES Modules",
"main": "module1.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"type": "commonjs"
}

"commonjs" Behavior

"commonjs" 行为

  • .cjs files are treated as CommonJS modules

    .cjs文件被视为CommonJS模块

  • .js files are treated as CommonJS modules

    .js文件被视为CommonJS模块

  • .mjs files are treated as ES modules

    .mjs文件被视为ES模块

"module" Behavior

"module" 行为

  • .cjs files are treated as CommonJS modules

    .cjs文件被视为CommonJS模块

  • .js files are treated as ES modules

    .js文件被视为ES模块

  • .mjs files are treated as ES modules

    .mjs文件被视为ES模块

As you can see, the difference is how .js files are treated.

如您所见,区别在于如何处理.js文件。

混合ES模块和CommonJS模块 (Mixing ES modules and CommonJS modules)

Here are the general rules regarding what doesn’t work, accompanied by relevant errors. “Doesn’t work” means that it results in an error if you run the code with node from the command line.

以下是有关无效内容的一般规则, 伴有相关错误。 “不起作用”表示如果在命令行中使用node运行代码,则会导致错误。

Remember from above that, if "type": "module", then an ES module is any .js or .mjs file. If "type": "commonjs", then an ES module is any .mjs file.

从上面记住,如果"type": "module" ,则ES模块是任何.js.mjs文件。 如果"type": "commonjs" ,则ES模块是任何.mjs文件。

  1. You can only use import and export in an ES module. Specifically, this means you can only use import and export in a .mjs file, or in a .js file if "type": "module".

    您只能在ES模块中使用importexport 。 具体来说,这意味着您只能在.mjs文件或.js文件(如果为"type": "module"使用importexport

    You can only use import and export in an ES module. Specifically, this means you can only use import and export in a .mjs file, or in a .js file if "type": "module". Cannot use import statement outside a moduleUnexpected token ‘export’

    您只能在ES模块中使用importexport 。 具体来说,这意味着您只能在.mjs文件或.js文件(如果为"type": "module"使用importexportCannot use import statement outside a moduleUnexpected token 'export'

  2. You cannot use require in an ES module, you must use import.

    您不能在ES模块中使用require ,而必须使用import

    You cannot use require in an ES module, you must use import.ReferenceError: require is not defined

    您不能在ES模块中使用require ,而必须使用importReferenceError: require is not defined

  3. You cannot use require to load an ES module.

    您不能使用require加载ES模块。

    You cannot use require to load an ES module.Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

    您不能使用require加载ES模块。 Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

So, what does work?

那么, 什么工作呢?

  1. An ES module can import exports from a CommonJS module. As far as mixing goes, that’s it.

    ES模块可以从CommonJS模块导入导出。 就混合而言,就是这样。

For example, if "type": "commonjs", then this example works, i.e. node module1.mjs will run without any errors.

例如,如果"type": "commonjs" ,则此示例有效,即, node module1.mjs将运行而没有任何错误。

// module1.mjsconsole.log("require module1");import obj from "./module2.js";
console.log(`module2 = ${obj.module2}`);// module2.jsconsole.log("require module2");
exports.module2 = "require module2";

If "type": "module", then this example works, i.e. node module1 will run without any errors.

如果"type": "module" ,则此示例有效,即, node module1将运行而没有任何错误。

// module1.jsconsole.log("require module1");import obj from "./module2.cjs";
console.log(`module2 = ${obj.module2}`);// module2.cjsconsole.log("require module2");
exports.module2 = "require module2";

As a reminder, full code examples for these rules can be found here: https://github.com/arcticmatt/javascript_modules/tree/master/node.

提醒一下,这些规则的完整代码示例可以在以下位置找到: https : //github.com/arcticmatt/javascript_modules/tree/master/node

Image for post
This diagram summarizes the above rules.
此图总结了以上规则。

资源资源 (Resources)

翻译自: https://medium.com/@pencilflip/using-es-modules-with-commonjs-modules-in-node-js-1015786dab03

commonjs模块

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值