简体   繁体   中英

Can I put the npm node_modules directory outside of my project

Can I put the node_modules directory outside my project just the way maven does with its dependencies?

Sort of. The node_modules directory is not actually a feature of npm but a feature of node.js .

How node.js uses node_modules .

When you require a module node.js will search for a node_modules directory from the current directory upwards. That means if it can't find one in the current directory (which may actually be a subdirectory of your project instead of your project directory) it will look for it in the parent directory then the parent's parent all the way to your root directory.

For example, you can have a project structure like this:

/node_modules   <-------------------- modules available to all projects    
/code
     /project_A
                /node_modules <------ modules available to project A
                /lib
                    /node_modules <-- modules available to the lib directory
     /project_B
               /node_modules  <------ modules available to project B

This way you can have some modules shared by multiple projects and some modules that are project specific and even some modules that are only available to some files in your project.

How npm handles node_modules

Note however that npm has only one interpretation of node_modules . It only manages node_modules in your project directory. Specifically the directory that contains the package.json file.

So yes, you can do it but npm won't understand what you are doing and your package.json will contain incomplete dependencies. I wouldn't recommend doing this with projects involving multiple developers because it will look more like a misconfigured development environment - basically others will think this is a bug. However I personally have used such structures for personal projects where I don't care about my package.json file.

As mention in this question Don't do it. Let NPM work the way it's designed to. However, to save space, you can delete the node_modules folder on projects that are currently dormant, and recreate it with a single shot of npm install when you switch back to them.

It is an issue if the plugin declares to install the modules in a configurable directory. Therefore I suggest to fix the documentation, it gives false hopes. Please see npm docs , as this is the default behaviour of npm. Not a frontend-maven-plugin issue.

Yes you can, but you should try to avoid doing so.

npm install will always install the modules within node_modules folder in your parent working directory.

Whenever you install a module using npm install -g module_name , it installs modules globally outside your project directory which can be also used in other projects, normally some dev dependencies are installed globally which helps you in development purpose.

An example would be npm install -g @angular/cli , doing this once will enable you to use ng commands to build,test other angular projects as well.

Other than this,it would be ideal if all the node_modules which are required for your project would stay in the working directory of your project.

Installing anything globally outside your project is considered bad practice as different projects may depend on different versions of the same node_module.Installing node_modules locally within the project directory allows different projects to have different versions of same node_module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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