Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the main uses for the NPM package.json file?

I read from here that the dependencies in the package.json file allow people to install the dependencies if they install your project through npm-

Finally, the dependencies field is used to list all the dependencies of your project that are available on npm. When someone installs your project through npm, all the dependencies listed will be installed as well. Additionally, if someone runs npm install in the root directory of your project, it will install all the dependencies to ./node_modules.

Where will all the dependencies be installed to if someone doesn't run npm install in the root directory of your project?

Also, what if they choose to clone this project through Github instead? It would be ready to go anyway, right? Then at that point what is the purpose of the package.json file besides giving the user meta data about the project?

like image 803
akantoword Avatar asked Mar 26 '16 00:03

akantoword


People also ask

What are NPM packages used for?

npm is the world's largest Software Registry. The registry contains over 800,000 code packages. Open-source developers use npm to share software. Many organizations also use npm to manage private development.

What is the use of package json in react application?

This is a JSON(JavaScript Object Notation) file that includes key information concerning your project. This is a required file that is needed to run any React project. This file includes metadata such as name , author and version as well as starter scripts and dependencies used in the project.

What do you know npm package json?

All npm packages contain a file, usually in the project root, called package. json - this file holds various metadata relevant to the project. This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies.


1 Answers

Where will all the dependencies be installed to if someone doesn't run npm install in the root directory of your project?

If by that you mean 'where will they be installed if you run the command in a different directory', NPM will search upwards through the parent directories until it finds package.json, and then install the dependencies in a node_modules folder next to that file. I.E. they'll always end up in the project root.

Also, what if they choose to clone this project through Github instead? It would be ready to go anyway, right? Then at that point what is the purpose of the package.json file besides giving the user meta data about the project?

This isn't the case! Node projects just about always have a .gitignore file which explicitly excludes node_modules from being committed to version control, and expect you to run npm install after downloading the source.

There's very few good reasons to have your dependencies in your GitHub repository - as long as a project adheres to Semantic Versioning (the vast majority of packages do), npm install will never cause incompatible versions to be downloaded, and if you absolutely need to lock down the versions of your dependencies, you can just use npm shrinkwrap.

EDIT: As Matt's comment very helpfully pointed out, there's several features of NPM that go beyond simple metadata - the one I probably get the most use out of is Scripts, which allow you to create project-specific aliases for command-line operations.

An example of where this has come in handy for me is running the Webpack development server - it's installed locally to my project in the devDependencies (which you can do using the --save-dev option when installing a package), so if I was doing it manually, I would have to type something along the lines of:

"./node_modules/.bin/webpack-dev-server" --inline --hot

Which quite frankly, would be a bit of a pain. Instead, I can just add this to my package.json (note that node_modules/.bin is automatically added to the system path when using an NPM script, so you don't need to type that every time):

"scripts": {
    "dev": "webpack-dev-server --inline --hot"
}

And then all I have to run is:

npm run dev

Beyond this simple use-case, there's also several 'special' script names which are automatically called upon certain events - for example, prepublish is run before publishing a package to the registry.

like image 139
Joe Clay Avatar answered Sep 21 '22 00:09

Joe Clay