npm
I want to learn more about npm (node package manager).
References
Notes
npm is the world's largest software registry. Open source developers from every content use npm to share and borrow packages, and many organizations use npm to manage private development as well. npm consists of three distinct components:
- the website
- Use the website to discover packages, set up profiles, and manage other aspects of your npm experience.
- the Command Line Interface (CLI)
- The CLI runs from a terminal and is how most developers interact with npm
- the registry
- the registry is a large public database of JavaScript software and the meta-information surrounding it
Use npm to:
- adapt packages of code for your apps, or incorporate packages as they are
- Download standalone tools you can use right away
- Run packages without downloading using
npx
- ...
The public npm registry is a database of JavaScript packages, each comprised of software and metadata. Open source developers and developers at companies use the npm registry to contribute packages to the entire community or members of their organizations, and download packages to use in their own projects.
A package is a file or directory that is described by package.json
file. A package must contain a package.json
file in order to be published to the npm registry.
A module is any file or directory in the node_modules
directory that can be loaded by the Node.js require()
function. To be loaded by the Node.jsrequire()
function, a module must be one of the following:
- A folder with a
package.json
file containing a"main"
field. - A JavaScript file.
In the context of a Node program, the module
is also the thing that was loaded from a file.
You can install a package locally if you want to depend on the package from your own module, using something like Node.js require
. This is npm install
's default behavior. To install a public package, on the command line, run:
$ npm install <package_name>
Installing a package globally allows you to use the code in the package as a set of tools on your local computer. To download and install packages globally, on the command line, run the following command:
$ npm install -g <package_name>
An Introduction to the NPM Package Manager
npm
is the standard package manager for Node.js. In September 2022, over 2.1 million packages were reported being listed in the npm registry, making it the biggest single language code repository on Earth, and you can be sure there is a package for almost everything. npm
installs, updates, and manages downloads of dependencies of your projects. Dependencies are pre-build pieces of code, such as libraries and packages, that your Node.js application needs to work. npm
installs, updates, and manages downloads of dependencies of your project. Dependencies are pre-built pieces of code, such as libraries and packages, that your Node.js application needs to work.
If your package has a package.json
file, by running
$ npm install
it will install everything the project needs, in the node_modules
folder, creating it if it's not existing already.
You can also install a specific package by running:
$ npm install <package-name>
More flags can be added to the above command:
--save-dev
: installs and saves the entry topackage.json
file devDependencies--no-save
: installs but does not add the entry to thepackage.json
file dependencies--save-optional
: installs and adds the entry to thepackage.json
optionalDependencies--no-optional
: will prevent optional dependencies from being installed
The difference between devDependencies and dependencies is that the former contains development tools, like a testing library, while the latter is bundled with the app in production. As for the optionalDependencies, the differenece is that build failure of the dependency will not cause installation to fail. But it is your program's responsibility to handle the lack of the dependency. updating is made easy by running:
$ npm update [<package_name>]
Comments
You have to be logged in to add a comment
User Comments
There are currently no comments for this article.