wmmhihaa
wmmhihaa

Reputation: 953

Require npm from different location

I have an application that needs to download npm packages at run-time. This has all worked fine, until we deployed the application as a snap to Ubuntu Core. This left the original app on a write protected area, and all run-time installed packages in the home directory (~/home/use/node_modules). And this was where the issues came :(

As I wanted to leave the application as is (as much as possible), I didn’t want to create my own require method, but just adding the path to the home directory when starting the application (using process.env.NODE_PATH).

The original application was then able to load the run-time installed packages, but those packages were not able to find other packages anywhere (not in it’s own node_modules folder, nor in the root).

I think I get the same issue (symptoms are the same) if I try to require a package from a different part of the disk, after added the path to the process.env.NODE_PATH variable.

QUESTION: Is there a better (working) way to solve this, without overriding the require method?

BTW.. I've tried using the app-module-path package as well, but with the same result.

Upvotes: 0

Views: 26

Answers (1)

Jonny Asmar
Jonny Asmar

Reputation: 1990

Have you tried creating a symlink from the app's directory? The NPM packages are probably trying to use paths relative to your app because that's their context. If you create a symlink from ./node_modules to ~/home/use/node_modules, it may work.

Upvotes: 1

Related Questions