What is the NPM
NPM is nodeJS package management tool
What does NPM do
You can publish your code to NPM for others to download, or you can download code shared by others on NPM.
How to use NPM
NPM is a package management tool built into NodeJS. After installing NodeJS, you can use NPM.
Commonly used instructions
Installation:
npm install xxx
NPM install XXX -g
NPM install XXX -s is stored in the devDependencies node
NPM install XXX -d store in the Dependencies node
The plug-in in devDependencies is only used in the development environment, not in the production environment, and dependencies are published to the production environment
Upload your first NPM package
1. NPM website account password
The registered address address: https://www.npmjs.com/signup
2. Initialize the project
npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (npmdemo) zxb-npmdemo-wlww
Version: 1.0.0 (1.0.0)
description: test
entry point: (index.js)
test command:
git repository:
keywords:
author: zd
license: (ISC)
About to write to F:\ personal \npmDemo\package.json:
{
"name": "npmdemo".
"version": "1.0.0".
"description": "test".
"main": "index.js".
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "zd".
"license": "ISC"
}
Copy the code
3. Write code
Create index. Js
export function say() {
console.log("hello word")
}
Copy the code
4. Login NPM
npm login
5. Publish the NPM package
npm publish
6. Create a project to install the published NPM package
npm install zxb-npmdemo-wlww
Introduce the newly installed package into the file
import {say} from 'zxb-npmdemo-wlww
say()
Copy the code
Console output:
hello word
7. Update the NPM package
New method in index.js
export function say() {
console.log("hello word")
}
export function sayHi() {
console.log("hi word")
}
Copy the code
Change the version number and republish NPM publish
8. Update the NPM package in the project
npm update zxb-npmdemo-wlww
9. Apply new methods to projects
import {say,sayHi} from 'zxb-npmdemo-wlww'
SayHi () console outputs Hi Word
10. Delete the NPM package on NPM
npm unpublish --force
Due to NPM security policy adjustment, this command can only delete the latest version released within 24 hours