1, Official website
https://www.npmjs.com/
The software package can be found here. If you want to download a software package, you can first search on the official website, then copy the name and install
2, Common commands
1,npm init -y
xinxizhongxin-lijin:npm-study lijin$ npm init -y Wrote to /Users/lijin/Desktop/npm-study/package.json: { "name": "npm-study", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }
After initializing the project, a pakeage.xml file will be generated in the root directory JSON file.
Why use npm init to initialize a project?
Using npm init in node development will generate a pakeage JSON file, which is mainly used to record the details of the project. It will record the packages we need in the project development and the details of the project in the project. It will be more convenient in later version iteration and project migration. It also prevents the project from running normally due to the accidental deletion of a package in later project maintenance. Another advantage of initializing a project with npm init is that it is not necessary to send the project dependency package to the other party during project delivery. The other party can download all the project dependencies into the project by executing npm install after receiving your project. Without saying much, we started the operation directly.
2,npm install jquery
The command format is: npm install package name, which can be abbreviated as npm i jquery
xinxizhongxin-lijin:npm-study lijin$ npm install jquery npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN npm-study@1.0.0 No description npm WARN npm-study@1.0.0 No repository field. + jquery@3.6.0 added 1 package from 1 contributor in 0.536s
Download the latest version by default.
After installation, pakeage Some dependencies will be added to the JSON file, as follows:
{ "name": "npm-study", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "jquery": "^3.6.0" } }
At the same time, an additional directory node will appear under the root directory_ Modules. The installed software packages are stored in this directory, as follows:
xinxizhongxin-lijin:node_modules lijin$ pwd /Users/lijin/Desktop/npm-study/node_modules xinxizhongxin-lijin:node_modules lijin$ ls jquery xinxizhongxin-lijin:node_modules lijin$
3,npm uninstall jquery
The command format is: npm uninstall package name
After uninstalling, node_ jquery will be deleted from the modules directory, and pakeage The dependency declaration in the JSON file will also be deleted
4,npm install jquery@3.0.0
The command format is: npm uninstall package name @ version number
5,npm update jquery
Update the software package to the latest version
6,npm install
If we put node_ Delete the modules directory. Just execute the npm install command, and npm will automatically delete the pakeage Download the dependencies in JSON and recreate node_modules directory, so node_ The modules directory does not need to upload git
3, npm script
npm script is recorded in package Some user-defined scripts in the scripts field of JSON. Using user-defined scripts, users can record some command lines commonly used in projects in package JSON doesn't need to be typed every time.
Developers often need to use the following commands to count the number of lines of code in the project:
find src -name "*.js" | xargs cat | wc -l
Developers can write it into package JSON:
"scripts":{ "lines": "find src -name \"*.js\" | xargs cat | wc -l", }
In the future, developers only need to execute npm run lines instead of writing such a long command line, which can greatly improve efficiency. It should be noted that because the command is written in the json file, some special characters need to be translated, such as the double quotation marks above.