'Npm shared node_modules with correct package.json
I have two projects (basically a server and a client), which have very similar dependencies. I'd like to setup shared dependencies, in a convenient manner (not have to manually edit files, not have to install everything twice, not have to do potential changes twice, and node_modules being 90% duplicate).
This seems to work, as nodejs resolves in the parent folders, if a module wasn't found locally. However, i did so far not find a way, in which this would be supported by NPM, to properly get the modules, and add them to the respective package.json(s) automatically (e.g. with something similar to npm i --save-dev module_name). I'd need to manually edit the package.json(s), and e.g. any npm i would break the system.
Is this simply so far off the norm, that i should just bit into the sour apple, and get all the modules twice, for each project individually, or is there a clean way to accomplish this?
Examples of shared modules would be electron, electron-packager, typescript, ... - mostly dev-dependencies, that will always be the same for both sides. I would not want to add them globally.
Solution 1:[1]
I probably understand your needs.
I think you need npmpkgx
Its offline mode can add your project's dependencies to your package.json by matching global modules without installing modules
Its online mode adds the dependencies required by the project to your package.json by matching the npm registry, also without installing modules
When you need to install, just execute npm install to automatically install all the dependencies added to package.json by npmpkgx, instead of manually entering the module name.
Install
$ npm install npmpkgx -g
Example
$ npmpkgx ./ --registry
The following packages have been added to the dependencies
{ 'npm-pkgs': '^2.0.1' }
The following packages are not added to the dependencies
{
fs: '*',
path: '*',
https: '*',
'./cmd/command': '*',
'npm-pkgxxx': '*'
}
$ npm install
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mr.lin |
