简体   繁体   English

执行代码后的节点导入模块(或 ESM 动态导入)

[英]Node import modules after executing code (or ESM Dynamic import)

i'm writing some code in nodejs and express, i need to execute some code BEFORE continuing to import modules.我正在 nodejs 和 express 中编写一些代码,我需要在继续导入模块之前执行一些代码。 My whole app has set up and uses modules with import instead of require and I can't change this setting.我的整个应用程序已经设置并使用带有导入而不是要求的模块,我无法更改此设置。

In order to get some npm packages to work I have to run them BEFORE continuing with importing modules.为了让一些 npm 包工作,我必须在继续导入模块之前运行它们。 Using commonJS and require () works perfectly, but with import I can't.使用 commonJS 和 require () 效果很好,但使用 import 我不能。

Even if I reverse the order of the modules or if I call them in different files they are all loaded FIRST and only afterwards my code is executed.即使我颠倒了模块的顺序,或者如果我在不同的文件中调用它们,它们也会首先加载,然后才执行我的代码。

Example start.js示例 start.js

import { mustBeLoadedAfterCode } from './second.js';
// some code here I need to execute first

second.js第二个.js

import { mustBeLoadedAtTheEnd } from './third.js';
// some code here to execute at the end

In all my test, my code will be executed only AFTER have imported 'second.js' and all it relative imports.在我的所有测试中,我的代码只会在导入“second.js”及其所有相关导入后执行。

Any idea?任何的想法?

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

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