一、What is npm?
npm is the package manager for JavaScript. It’s also the world’s largest software registry.
npm consists of three distinct pieces:
the website
the registry
the Command Line Interface (CLI)
二、How to Install npm & Manage npm Versions
1. Install Node.js
2. After installing, run node -v The version should be higher than v8.9.1
3. Update npm
4. To test, run : npm -v
5. If the version is not the latest version, run:
npm install npm@latest -gIf you want to try the next, unreleased version of npm to test packages you have created, use this command:
npm install npm@next -g
三、How to Prevent Permissions Errors
Reinstall npm with a version manager (recommended),or Change npm’s default directory manually.
四、How to Install Local Packages
There are two ways to install npm packages: locally or globally. Choose which kind of installation to use based on how you want to use the package.
1. If you want to depend on the package from your own module, using something like Node.js’ require, then you want to install locally. This is npm install’s default behavior.
2. If you want to use a package as a command line tool, (such as grunt CLI), then install it globally.
3. Installing a Package
3.1 npm install
五、Working with package.json
1. A package.json file: lists the packages that your project depends on. allows you to specify the versions of a package that your project can use using semantic versioning rules. makes your build reproducible, and therefore much easier to share with other developers. Requirements A package.json must have: "name" all lowercase one word, no spaces dashes and underscores allowed "version" in the form of x.x.x 2. Creating a package.json To create a package.json with values that you supply, run: > `npm init` This will initiate a command line questionnaire that will conclude with the creation of a package.json in the directory in which you initiated the command. Create a default package.json To get a default package.json, run npm init with the --yes or -y flag: > `npm init --yes`
六、How to Update Local Packages
run npm update in the same directory as the package.json file of the application that you want to update.
Run npm outdated. There should not be any results
七、How to Uninstall Local Packages
To remove a package from your node_modules directory, use:
npm uninstall: npm uninstall lodash
To remove it from the dependencies in package.json, you will need to use the save flag:
npm uninstall --save lodash
Note: if you installed the package as a “devDependency” (i.e. with –save-dev) then –save won’t remove it from package.json. You have to use –save-dev to uninstall it.
八、How to Install Global Packages
To download packages globally, you simply use the command npm install -g
npm install -g jshint
九、How to Update Global Packages
To update global packages, tynpm update -g example, to update a packages called jshint, you’d type:
npm update -g jshint
To find out which packages need to be updated, type:
npm outdated -g --depth=0.
To update all global packages, type:
npm update -g.
十、How to uninstall global packages
To uninstall a global package,type:
npm uninstall -g
To uninstall a package called jshint, you would type:
npm uninstall -g jshint
十一、How to Work with Scoped Packages
If you use npm init, you can add your scope as an option to that commanpm init –scope=usernameyou use the same scope all the time, you will probably want to set this option in your .npmrc file.
npm config set scope username
Public scoped modules are free and don’t require a paid subscription. To publish a public scoped module, set the access option when publishing it. This option will remain set for all subsequent publishes.
npm publish --access=public
十二、How to Label Packages with Dist-tags
Adding tags
To add a tag to a specific version of your package, use:
npm dist-tag add
Installing with tags
Like npm publish, npm install will use the latest tag by default. To override this behavior, use npm install @. The following example will install the somepkg at the version that has been tagged with beta.
npm install somepkg@beta
十三、How to Change Profile Settings from the CLI
To view and set profile properties from the Command Line Interface (CLI), use these commands:
npm profile get npm profile set