Node Package Manager
npm -v : show the version , or npm --version
In the nodejs or javaScript world , package.json is the most important file, it manifest file with app info, list dependencies, specify version and so on.
1 Basic Usage
1.1 Init
-
just
npm init
, gonna create package.json file, and need to choose package name, entry point(lots of time it's index.js), and other things. -
if it's ok to skip all questions, just
npm init --yes
will keep default answers.- the npm system default value will be storaged in file
.npmrc
, path:/Users/macUser/.npmrc
, if put some value in it first:npm config set init-author-name "my name"
, then when usenpm init -y
, the author will read this config file automaticlly.(also can set init-license and so on).
- the npm system default value will be storaged in file
1.2 download dependencies
-
npm install lodash --save
, if without --save, it will just download this package to folder node_modules, but do not add this dependency into the file package.json -
run
npm install
when download a new project. -
npm install gulp gulp-sass --save-dev
will create dev dependencies, only used for dev.
-
for dependencies and devdependencies, if we clone a new project,
npm install
will download all dependencies(regular and dev ), butnpm install --production
will only download dependencies( only regular).
1.3 uninstall dependencies
npm uninstall gulp --save-dev
, remember to add --save-dev or --save behide the command, which make sure that package.json file will be updated.
1.4 download special version dependency
-
npm install [email protected] --save
-
update dependency to the latest version:
npm update lodash
1.5 Download global dependency(on the machine)
npm install nodemon -g
, which will not download into the project folder, will not add to the package.json file; usenpm root -g
can see where this nodemon dependency existed. (nodemon is a package wich will monitor every change in project and compile and run project after any changing, another one is live-server)
2. Semantic versioning
but for dependency, if : "lodash": "^4.17.4"
,
-
caret symbol ^: means if this dependency's minor version or patch version been updated, it will pull the latest minor version or patch version mirror.
-
tilde symbol ~: means if this dependency's patch version has been updated, it will pull the latest patch version mirror only!
-
without any symbol, will just download the special version
-
if it's
"lodash": "*"
, will download the latest version mirror, whatever a new major version or others.
3. Other operation
-
npm list
list all dependencies -
npm list --depth 0
only one level
if add some scripts into the package.json,
except start and test can use npm start
ornpm test
, others all need use npm run xxx
。