简体   繁体   English

ES6 模块导入会执行导入文件中的代码吗?

[英]Does ES6 module importing execute the code inside the imported file?

Does the code inside the js file gets run during the import? js 文件中的代码是否在导入过程中运行? if yes, then once or each time?如果是,那么一次还是每次? eg例如

// a.js
console.log("A");
const a = "a"; 
export default a;

// b.js
import a from "./a"; // => console logs?

// c.js
import a from "./a"; // => console logs again?

Yes, it does, exactly one time.是的,确实有一次。

See http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-module-records :请参阅http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-module-records

Do nothing if this module has already been evaluated.如果此模块已被评估,则不执行任何操作。 Otherwise, transitively evaluate all module dependences of this module and then evaluate this module否则,传递性地评估此模块的所有模块依赖性,然后评估此模块

The accepted answer is not fully correct.接受的答案并不完全正确。 An installed module will execute once when imported, yes, but if the module is installed multiple times (which is very easy to do in node) then it will execute as many times as it is installed.一个已安装的模块在导入时会执行一次,是的,但是如果模块被安装多次(这在 node 中很容易做到),那么它会执行多少次安装。

Consider what happens if a.js , b.js and c.js are in three separate packages ( package_a , package_b and package_c respectively) and package_b and package_c both specify package_a as a dependency, and your project specifies package_b and package_c :考虑一下如果a.jsb.jsc.js位于三个独立的包(分别是package_apackage_bpackage_c )并且package_bpackage_c都指定package_a作为依赖项,并且您的项目指定package_bpackage_c 会发生什么情况

node_modules/
├── package_b/
│   └── node_modules/
│       └── package_a/
|           └── a.js
└── package_c/
    └── node_modules/
        └── package_a/
            └── a.js

Because package_a will be installed twice (as far as your project is concerned these are two totally different packages) the code in a.js will be imported, and therefore executed twice .因为package_a将被安装两次(就您的项目而言,这是两个完全不同的包) a.js 中的代码将被导入,因此执行两次

Many people landing on this question are unlikely to be aware of this quirk of node, yet probably need to be if they landed on this question.许多登陆这个问题的人不太可能意识到这个节点的怪癖,但如果他们登陆这个问题,可能需要知道。

Here is an old but good article on understanding-the-npm-dependency-model with further detail on how and why npm does this.这是一篇关于理解 npm-dependency-model的旧但很好的文章,其中详细介绍了 npm 如何以及为什么这样做。

In case someone is using TypeScript with "module": "es6" and wondering how to do this, use the globalThis keyword:如果有人使用带有"module": "es6" TypeScript 并想知道如何做到这一点,请使用globalThis关键字:

function outputMsg(msg: string) : void {
    console.log(msg);
}

// export function for global scope
globalThis.outputMsg = outputMsg;

and then call outputMsg("my console output") as usual in the Chrome DevTools console and it should autocomplete and run your function.然后像往常一样在 Chrome DevTools 控制台中调用outputMsg("my console output") ,它应该自动完成并运行您的函数。

You could also rename your "global export":您还可以重命名您的“全球出口”:

globalThis.myCrazyFunc = outputMsg;

and call myCrazyFunc("crazy message") in the console.并在控制台中调用myCrazyFunc("crazy message")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM