NPM - Node Package Manager¶
What is NPM?¶
There are many modules/packages that you can add to your node project. Node Package Manager makes the process of installing and removing node packages easier and quicker.
Info
NPM (Node Package Manager) is a tool to install and remove node modules/packages.
Start a project¶
- Create a directory called myproject. Go to that directory.
- Type in the terminal
npm init
to initialize a new project. - The
npm init
command will create a configuration file for your project called package.json. - You will be prompted to provide information about your project. You can simply press enter for all the questions.
- The basic package.json file looks like the following:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
npm init
The npm init
command launches a node project and creates a package.json configuration file.
Installing packages¶
Task 1: Install a node package called http. Hint: type npm install http
. Check package.json file after installing the module; did it change?
- Run the command
npm install http
. - Notice that a new folder is created node_modules, and you can find a folder called http added within it.
- Notice that the package.json file changes to the following:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"http": "0.0.1-security"
}
}
Task 2: Install a node package called express. Check package.json file after installing the module; did it change?
- Run the command
npm install express
. - Notice that a new folder is created node_modules, and you can find a folder called express added within it.
- Notice that the package.json file changes to the following:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"http": "0.0.1-security"
}
}
Task 3: Install a node package called mustache. Check package.json file after installing the module; did it change?
- Run the command
npm install mustache
. - Notice that a new folder is created node_modules, and you can find a folder called mustache added within it.
- Notice that the package.json file changes to the following:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"http": "0.0.1-security",
"mustache": "^4.2.0"
}
}
npm install package_name
The npm install package_name
command installs a node module/package for your project, and it also adds it to the project dependencies in the package.json configuration file.
Uninstalling packages¶
Task 1: Uninstall the http node module. Hint: type npm uninstall http
. Check package.json file after installing the module; did it change?
- Run the command
npm uninstall http
. - Notice that nodes_modules/http is deleted.
- Notice that the package.json file changes to the following:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"mustache": "^4.2.0"
}
}
Task 2: Uninstall the express node module. Check package.json file after installing the module; did it change?
- Run the command
npm uninstall express
. - Notice that nodes_modules/express is deleted.
- Notice that the package.json file changes to the following:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"mustache": "^4.2.0"
}
}
Task 3: Uninstall the mustache node module. Check package.json file after installing the module; did it change?
- Run the command
npm uninstall mustache
. - Notice that nodes_modules/mustache is deleted.
- Notice that the package.json file changes to the following:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
}
}
npm install package_name
The npm uninstall package_name
command removes a node module/package from your project, and it also removes it from project dependencies in the package.json configuration file.