Introduction to npm

Node Package Manager (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 packages called the npm registry. We get access to the registry with the help of the command-line client (npm)

npm gets automatically installed when we download the node. So we don't have to install it separately. We can download node.js from its official website- https://nodejs.org/en/

Once downloaded, we can check the version of node and npm by typing the commands below in the terminal -

node -v
npm -v

package.json

The package.json is the project manifest file. Using package.json, you can manage dependencies and write scripts. It has all the metadata about the project.

You can add a package.json file to your project to make it easy for others to manage and install. It lists the packages your project depends on

How to create package.json?

Move into the directory where you want to store all your project files. Open the terminal and type the command-

npm init

The above command will create a package.json for your project. It asks you for some data-

Name: Type the name of the package.

Version: Type the version.

Description: Type the description.

Entry Point: By default, it's index.js

Test command: Type the test command

Git repository: Type the link to the Github repo

Keywords: If published on the npm website, keywords will help other developers find your package.

Author: Type your name

License: Type the kind of software license.

Press Yes to create package.json

npm commands

Installing packages

npm install

The above command will install all the dependencies/ packages require for the project to run. It will also generate new folders in the directory-

  • node_modules: find all the packages that are required to run the project in this folder.
  • package-lock.json: for any operations where npm modifies either the node_modules tree or package.json.

Installing a package by its name

npm install [package name]

Replace the package name with the package name you want to install.

npm install <package_name> --save-dev

The above command will install the package as a developer dependency. Packages that are required while developing or testing the project but not in the final project falls under the dev dependency.

Installing a Package Globally

Globally installed packages work anywhere on the machine.

npm install -g [package name]

Generally, install the packages that you use in your project locally. And the ones you use in the command line are to be installed globally.

Update the Package

npm update [package name] // update a specific package
npm update // update all the packages

Uninstall Package

npm uninstall [package name]

Start a new project

Create an index.js file in the same directory. Open the file in the text editor of your choice.

Add the code in the index.js file-

console.log('Hello world')

Press ctrl+s

Add the following code in the package.json in the "scripts" under the tests -

"start": "node index.js"

In the terminal, type the command found below-

npm start

The above command will run the project. It will run the start script - node index.js.

Output: Hello World

Full Code

Package.json

{
"name": "npm-basics",
"version": "1.0.0",
"description": "Sample package.json",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "KaranJagota",
"license": "MIT",
"dependencies": {
}
}

index.js

console.log("Hello world")