What is Npm
npm is a package manager for the JavaScript programming language maintained by npm, Inc. npm is the default package manager for the JavaScript runtime environment Node.js. It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry.
What is a Package?
A package in Node.js contains all the files you need for a module. Modules are JavaScript libraries you can include in your project.
How to Install a new package
To install a new package, you use the following npm install
command:
npm install <package_name>
How to Install npm Packages in Global Mode
npm install -g <package-name>
How to Update Package
To update the package installed locally in your Node.js project, navigate the command prompt or terminal window path to the project folder and write the following update command.
npm update <package name>
How to Uninstall package
In case you want to remove a package from your system you can just type in the below command:
npm uninstall <package_name>
What is package.json
Every project in JavaScript – whether it's Node.js or a browser application – can be scoped as an npm package with its own package information and its package.json job to describe the project.
We can think of package.json as stamped labels on those npm good boxes that our army of Wombats delivers around.
package.json will be generated when npm init is run to initialise a JavaScript/Node.js project, with these basic metadata provided by developers:
Example:
{
"name": "samplenode",
"version": "1.0.0",
"description": "Edureka demo on how to build a Node.js application",
"main": "script.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Edureka",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.16.4",
"express-handlebars": "^3.0.2",
"nodemon": "^1.19.0"
},
"devDependencies": {},
"repository": {},
"bugs": {}
}
What is Dependencies
When you install an npm package using npm install <package-name>
, you are installing it as a dependency.The package is automatically listed in the package.json file
, under the dependencies list (as of npm 5: before you had to manually specify --save
).
Example:
Using npm to install dependencies
Create a file called package.json in current directory with following content.
{
"dependencies": {
"jquery": "3.3.1",
"bootstrap": "4.2.1"
}
}
Execute command npm install
.
npm install
This command would read package.json and install dependencies jquery and bootstrap.
A new directory called node_modules
should have been created and directories jquery
and bootstrap
should have been created inside node_modules
. Check that node_modules
was created with these two directories.
What is Module
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.js require()
function, a module must be one of the following:
A folder with a
package.json
file containing a"main"
field.A JavaScript file.
Note:Since modules are not required to have a
package.json
file, not all modules are packages. Only modules that have a
package.json
file are also packages.
In the context of a Node program, the module
is also the thing that was loaded from a file. For example, in the following program:
var req = require('request')
we might say that "The variable req
refers to the request
module".
Navigated to About packages and modules
What is npm-init
npm init <initializer>
can be used to set up a new or existing npm package.
initializer
in this case is an npm package named create-<initializer>
, which will be installed by npx
, and then have its main bin executed -- presumably creating or updating package.json
and running any other initialization-related operations.
The init command is transformed to a correspondingnpx
operation as follows:
npm init foo
-> npx create-foo
npm init @usr/foo
-> npx @usr/create-foo
npm init @usr
-> npx @usr/create
Any additional options will be passed directly to the command, so npm init foo --hello will map to npx create-foo --hello.
If the initializer is omitted (by just calling npm init
), init will fall back to legacy init behavior. It will ask you a bunch of questions, and then write a package.json for you. It will attempt to make reasonable guesses based on existing fields, dependencies, and options selected. It is strictly additive, so it will keep any fields and values that were already set. You can also use-y
/--yes
to skip the questionnaire altogether. If you pass --scope
, it will create a scoped package.
Use of Npm
npm
can manage packages that are local dependencies of a particular project, as well as globally-installed JavaScript tools.