I want to run several code before the code inside import
syntax gets executed.
Example
file-1.js
console.log('Inside File 1')
import './file-2.js'
file-2.js
console.log('Inside File 2')
Output
Inside File 2
Inside File 1
The output I expected
Inside File 1
Inside File 2
Environment
Node JS v12.19.0
with Module configuration
Real Case
file-1.js
process.env.SHARED_DATA = 'Hello world'
import './file-2.js'
file-2.js
console.log(process.env.SHARED_DATA)
Output
undefined
file-2.js
only a function. Then, you import that function (but no code has run yet) and then after setting up the environment, you call the function you imported and file-2.js finishes initializing itself. Or, you just pass the relevant info directly to the function when you call it rather than setting it into the environment. In either case,file-1.js
gets to run code before it calls thefile-2.js
initialization function. This is a design pattern often called "module constructor" and is used to pass initialization parameters. – Denna