When I'm executing pure node.js scripts I can change the path where it searches for node_modules as the following:

export NODE_PATH="/home/user/node_modules"
node "/home/user/different/path/file.js"

This way, I can make scripts located inside /home/user/different/path/ see the node_modules folder located in /home/user/ when they are executed.

So far everything is fine, the problem starts with .mjs files. If I try running:

export NODE_PATH="/home/user/node_modules"
node "/home/user/different/path/file.mjs"

I'll receive the error Error [ERR_MODULE_NOT_FOUND] for the modules that I use in my code. The workaround that I know for that is creating a symbolic link inside my script's folder. Something like:

ln -s "/home/user/node_modules" "/home/user/different/path/node_modules"

After doing that, if I run node "/home/user/different/path/file.mjs" it'll work as expected and I'll be able to use libraries installed on /home/user/node_modules with the import statement in my script. However, I'd like to find a solution that doesn't require me to create a symbolic link of the node_modules folder. Is there any alternative solution when I'm working with .mjs files that allows me to change its relative path?

0

There are 0 best solutions below