NPM
Table of contents
- What is NPM?
- Installation
- Check version
- Check for hints of usage
- Starting the package
- Then you can change the config details
- check and remove default configuration
- Install package
- Remove or uninstall module
- Install module with version and upate
- To check the root folder of global install
- Remove global package
- To show the installed packages
What is NPM?
npm
is the world’s largest software registry. Open source developers from every continent use npm to share and borrow packages, and many organizations use npm to manage private development as well.
See more details on About NPM
Installation
Install the npm
with NodeJS by downloading it on the download page. It is strongly recommended to install Nodejs and npm with version manager like nvm
Check version
Once the npm
is installed, check the version by running:
npm -v (or --version)
Check for hints of usage
npm help
Starting the package
Run npm init
to start the use and it will create folder node_module
and file called package.json
. It will ask question in steps about the details. If you wish to use all defauylt options, add -y
or --yes
npm init -y (or --yes)
The default package.json
will look lik this:
"name": "package-name",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
}
}
Then you can change the config details
Example
npm config set init-author-name "Irawan"
npm set init-license "MIT"
Then it will look like this:
"name": "package-name",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "gulp test",
"start": "gulp serve",
"build": "gulp build"
},
"keywords": [],
"author": "Irawan",
"license": "MIT",
"description": "",
"dependencies": {
"gulp": "^4.0.2",
"lodash": "^4.17.3"
}
}
check and remove default configuration
To check the configuration details, run the following:
npm config get init-author-name
npm get init-license
to remove details;
npm config delete init-author-name
npm delete init-license
Install package
Locally
E.g installing gulp-sass
npm install gulp gulp-sass --save-dev
Always use --save-dev
to ensure that the changes is saved in the package.json file.
Install globally (in the machine and not only in current folder)
npm install -g nodemon
npm install -g live-server
Remove or uninstall module
npm uninstall gulp-sass --save-dev
# or
npm remove gulp --save-dev
#or
npm rm gulp-sass --save-dev
Install module with version and upate
npm install gulp-sas@5.5.5 --save
# to update
# UPDATE
npm update lodash --sav
To check the root folder of global install
npm root -g
Remove global package
npm remove -g gulp-sass
To show the installed packages
#Main modules only:
npm list
#result:
╰─ npm list
user@1.0.0 /Users/project-name
├── gulp-cli@2.3.0
├── gulp-sass@5.1.0
├── gulp@4.0.2
└── lodash@4.17.21
# Main modules with the direct dependencies:
npm list --depth 0
#Main modules with the dependencies next level
npm list --depth 1
# the most important part of the package.json is the `scripts`
```json
"scripts": {
"test": "gulp test",
"start": "gulp serve",
"build": "gulp build"
},
When you run the scripts e.g npm start
it will run the module gulp serve