npm is the world's largest Software Library (Registry), and it is also a software Package Manager and Installer for the javascript projects. npm is widely used by nodejs, angular, react, vuejs frameworks, you can create your own package and publish the package to the marketplace. it is very helpful to share the code, plugin to with other developers and open source community. npm has boosted the open source community to the new level.
How to install
To use the npm, you need to install the nodejs, npm is in-built with nodejs. You can download the nodejs from the official site nodejs.org, After downloading, please install it.
-
For windows Install it in the c drive, and copy the installed location path
C:\Program Files\nodejs
and add it in your enviorement variables -
Right click on
my computer
and thenproperties
\ -
Now click on the
Advanced system settings
(Left bar) \ -
Now click on the
path
and add the new path \
How npm package works
After installing the nodejs, you can verify the installation by running the npm --version
command from your command runner.
What is package.json
package.json
file is used to maintain the all npm package with the version. this file contains all the npm packages you installed.
How to generate the package.json file
npm provides npm init
command to generate the package.json
file while running this command it will ask some details like - package name
: Name of the package you want to add like demo-app
version
: version by default 1.0.0description
: add descriptionentry point
: when you runnpm start
, the provided entry point will run, you can add index.js or leave blank ittest command
: Any script you want to use for testinggit repo path
: You can provide your GitHub repo URLkeywords
: For searching and SEO purposeauthor
: Add the name of the authorlicence
: Add like MIT or ISC
How to install the npm package
You can get the npm package you want to install from the NpmJS You can install the package globally and locally
- a. Globally
To install any package you need to add -g
prefix like
npm i expressjs -g
: it will install the expressjs globally, you don't need to install it for your projects individually.
The package will be accessibly globally.
- b. Locally
npm i expressjs
: it will install the expressjs locally, the package will be only accessible to your local project.
List of npm common commands
Below is the list of all npm commands
npm init
: to generate the package.json filenpm i
ornpm install
: to install all packages of thepackage.json
filenpm i -g
: install the package globallynpm start
: It will execute the entry point script you added in thepackage.json
filenpm install npm@latest -g
: Upgrade the npm package to the latest versionnpm outdated
: list of all out date packagesnpm list -g
: list of all global npm packagesnpm uninstall "package name"
: it will uninstall the packagenpm publish
: This is used to publish the package at npmjs