DEV Community

Cover image for npm basic commands
sndp
sndp

Posted on

npm basic commands

Photo by Paul Esch-Laurent

npm

NPM a.k.a. 'Node Package Manager' is a registry of libraries for use in the development of javascript based applications. This is the default and largest package manager in the world.

The npm was originally invented by Isaac Z. Schlueter 11 years ago. Continued releases with the support of developers made a huge contribution in making npm the best version it is today.

NPM basically comes with the Node.js runtime environment and as a dependency. Many developers use Node.js with npm effectively to build apps quick and easily. Learning npm would be a big opportunity in the job industry as well.

npm mainly consists of,

  • Libraries of packages (registry)
  • Package Manager (cli)
  • Installer

npm package registry ->

The npm package registry is a database of javascript packages or libraries. These packages takes the structure of a repository which can be seen as modules, readme files, metadata etc. These libraries are available to use by anyone for their projects. Many libraries are available to open source. We can access the registry by using the npm's cli.

npm package manager ->

The npm has its own multi-platform command line interface and it has many uses for the developer. To get npm cli we have to download and install Node.js. Click this.
The npm can be referred to this cli due to the distributed architecture of it.
The cli can be used to,

  • login to npm
npm-cli-login
Enter fullscreen mode Exit fullscreen mode
  • initialize nodejs project (create 'package.json' file)
npm init
Enter fullscreen mode Exit fullscreen mode
  • run custom application based scripts (server/index.js/app.js)
npm run server
Enter fullscreen mode Exit fullscreen mode
  • start application
npm start app
Enter fullscreen mode Exit fullscreen mode
  • test your code
npm run tests
Enter fullscreen mode Exit fullscreen mode

npm installer ->

This part of npm downloads and installs the npm packages to Node.js development project when required.
First we need to initialize our app's package.json file.

npm init --y

This command makes a package.json file with default values engaged with the project's metadata. We can edit this later on ourselves. To get the initial dependencies run the installer by adding dependencies to the package.json file by adding,

package.json file

You can also add the version as the value of package name.
Then we can run the command,

npm install

To automatically (without editing the package.json file first) install and edit the package.json by itself, we can use the command below.

npm i --save <package_name1> <package_name2>

Then a folder with packages named as node_modules should be installed to the location the command used in (project root directory).
After that we can use the modules.

Developers can also use developer dependencies like nodemon, karma etc. by adding -dev flag.

npm install -dev <dev_package_name>

We are now ready to code with the aid of newly added dependencies. By using such efficient tool we can reduce the development and reduce time by without using external plugins.

Learn more about npm with following links.

https://docs.npmjs.com/

Top comments (2)

Collapse
 
codewithpom profile image
Padmashree Jha • Edited

A good quick overview of npm and I want to know how you got the npm sticker plz 😥😥

Collapse
 
lizardkinglk profile image
sndp