02. 【Node.js Module】Install Pakages Using NPM

心然may發表於2019-02-16

I`m practicing my English ability lately in order to be recruited by a foreign company for my next job-hopping,if there is anything that I did`t express precisely, please pardon me.

1. Enter the npm website and choose the module you want: https://www.npmjs.com

2. Install your package.

There are several falgs that you should know about:

(1) “-g”

a.Install with the “-g” flag, means package will be installed globally.
Install with it when you are going to use command line, such as “grunt”.
npm init -g [packgeName]

b.Install Without the “-g” flag, means package will be installed in local node module for your application.
Install without it when you are going to require the package by using “require()” statement in your application.
npm init [packageName]

(2) “–save”

Install with the “–save” flag, means it will create a “dependencies” section in your “package.json” file, and add the package you installed to it.
When someone else use your application or develop against it, they will have a complete list of dependencies that you build against.
npm init [packageName] --save

(3) “–save-dev”

Install with the “–save-dev” flag, means it will create a “devDependencies” section in your “package.json” file, and add the package you installed to it.
Use it when you installed something that you need to share with your other developers , but isn`t needed on your production server. Such as things that you use for test.
npm init [packageName] --save-dev

3. Check your “package.json” file after installing.

The meaning of some symbol before the version of your packages:

e.g:

     "devDependencies": {
        "mocha": "^2.2.5" // meaning: [major version.[minor version].[patch level]
    }

(1) “^” : update until the major version

e.g: “mocha”: “^2.2.5” means you will update mocha until >= “2.2.5”, but < “3.0.0”

(2) “~” : update until the minor version

e.g: “mocha”: “~2.2.5” means you will update mocha until >= “2.2.5”, but < “2.3.0”

(3) “*”: update to the latest version (not recomanded)

e.g: “mocha”: “” means you will update mocha to the latest version*

Related:
Create a Node.js Module and Use it Locally
Publish a Node.js Module to the NPM Registry

相關文章