Node import modules after executing code (or ESM Dynamic import)

Viewed 1441

i'm writing some code in nodejs and express, i need to execute some code BEFORE continuing to import modules. 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. Using commonJS and require () works perfectly, but with import I can't.

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

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

second.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.

Any idea?

1 Answers
Related