I deleted a package folder under node_modules by mistake - npm

I deleted a folder under node_modules by mistake, should I run npm install in the project root to recover the deleted files without deleting the entire node_modules folder or should i delete the node_modules first and then run npm i?
If I run npm install without deleting the node_modules folder; will npm install only add the missing package files under the node_modules?

You probably must be having package.json present in your project root directory. If you have so, then you can then directly run npm install and all the packages and dependencies will be restored.

Related

prevent npm from removing node_modules folder on failure

I want to npm install a broken package, i.e., the installation process does not work properly and results in an error. I am used to npm keeping the successfully installed packages in the local node_modules folder. For some reason npm has stopped doing that and removes the node_modules folder if the install is not successful.
How can I tell npm to keep the local node_modules folder regardless of the success or failure of the installation process?
I faced a similar problem when one of the packages failed to build its C++ addon and npm removed node_modules. The below steps helped me:
Install the packages with scripts disabled.
$ npm install --ignore-scripts
Fix the package
...
Rebuild the package
$ npm rebuild PACKAGE

npm install errors out on all packes if npm-shrinkwrap.json is present

node v10.15.0
npm v6.4.1
I inherited a project that has an npm-shrinkwrap.json next to the package.json.
When I run npm install I get errors like the following for the files in every single package that should be downloaded
npm WARN tar ENOENT: no such file or directory, open 'C:\...\node_modules\.staging\core-js-12a70f6a\fn\number\virtual\index.js'
Checking the .staging folder I see that the files are actually there!
Out of curiosity I removed the npm-shrinkwrap.json and run npm install again and now it creates the node_modules folders as expected.
I found that shrinkwrap is an old method, replaced by the lock mechanism in later NPM versions?
Would it be save to just delete the npm-shrinkwrap.json and not run into any issues with package versions later?

Should I publish my module's node_modules when doing npm publish?

I am working on a npm module with a couple dependencies. Now, should i publish the dependencies along with my product via the node_modules folder or not?
No you should not.
You should list all your dependencies in your package.json and publish that.
Your node_modules folder is automatically created when doing an npm install of the module, or npm install in a directory with a package.json.

Why does npm install dependency package out of it's folder

when I run npm install all of the dependencies will not load in their own folder but instead install them in my node_modules folder so I end up with 50 or so folders. Is there a way to get dependency packages node_modules to stay within that dependencies folder?

What is the relationship between the node_modules directory and package.json?

Is the structure of dependencies in node_modules simply a mirror of the dependency tree structure found in package.json? Or does performing npm install download what is in package.json and organize node_modules in some special way?
Ideally package.json will correspond to node_modules. Running npm install (with no arguments) will install all the packages described in package.json into node_modules, but running npm install somepackage won't modify package.json unless you use the --save option.
You can also use npm list to check if your node_modules and package.json are in sync. Packages in package.json that aren't in node_modules are tagged UNMET DEPENDENCY, whereas packages in node_modules but not in package.json are tagged extraneous.
Also note that the root package.json doesn't contain the full dependency tree; it only contains the list of direct dependencies. Dependencies of dependencies are listed in the package.json files of the dependencies themselves, recursively.