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

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.

Related

How to find global npm's package.json?

How can I find the package.json for npm that lists all globally installed packages? I know I can find the root with npm root -g, but I cannot find the package.json in that directory.
If you run npm list inside the node_modules directory, it will list all the globally installed modules.

I deleted a package folder under node_modules by mistake

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.

How to manage a non-node module as dependency for Node Project thorugh NPM?

I was using bower in my project for dependency management. But now I wanted to move to node and npm. But the problem is few of my dependencies are not node modules like Smooth-Div-Scroll1 and swiftype-search-jquery2
How to manage all those dependencies in my node project. When I tried doing
"swiftype-search-jquery": "git#github.com:swiftype/swiftype-search-jquery.git",
"smooth-div-scroll": "git#github.com:tkahn/Smooth-Div-Scroll.git",
it is not able to download dependency and throw error
npm ERR! code ENOPACKAGEJSON npm ERR! package.json Non-registry package missing package.json: swiftype-search-jquery#git+ssh://git#github.com/swiftype/swiftype-search-jquery.git. npm ERR! package.json npm can't find a package.json file in your current directory.
These dependencies don't have package.json in their folder.
I don't think it's possible.
Here is a guide to migrate from bower to npm, it's pretty easy.
https://codecraft.tv/courses/angularjs-migration/step-2-typescript-and-webpack/converting-bower-to-npm/
Yup, It is not possible with NPM but at the same time, it is possible with Yarn without making any change. Instead of "npm install" we have to use "yarn install" .
Add dependencies with git url which is not present with NPM .

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?