SoFunction
Updated on 2025-03-02

Install the latest version of nodejs using npm

# Install nodejs

The following is the command line installation method. Readers can also download and install the latest version on the nodejs official website.

sudo apt-get install nodejs-legacy
sudo apt-get instatll npm

Upgrade npm to the latest version

sudo npm install npm@latest -g

Often, nodejs installed using the command line is not the latest version, and upgrading nodejs is not a very simple thing; at this time, we need to use the very easy nodejs version management module n

sudo npm install -g n

Install the specified version of nodejs through module n

sudo n latest
sudo n stable
sudo n lts

We can check whether the installation is successful and whether it is installed to the desired version by checking the version number

node -v
npm -v

# Talk about npm

npm (node ​​package manage) is a package manager for nodejs, used to manage node plug-ins including installation, uninstallation, management of dependencies, etc.

Use npm to install the plugin, the syntax is; npm install <name> [-g] [--save-dev]
2,1  <name>: The name of the plugin to be managed, for example: npm install gulp-less --save-dev
2.2 -g: Global installation. Optionally, the unaddressed plug-in will be installed in the node_modules folder of the currently located directory. The advantage of global installation is that it can be called from the command line anywhere.
2.3 --save: Save configuration information into the file. It is the configuration file of the nodejs project.
2.4 -dev: The devDependencies node saved to. If -dev is not added, it will be saved to the dependencies node.
2.5 Why save it in? Because the node plug-in package is huge, it is not added to version management. Adding the configuration information can indicate that these plug-ins need to be version management. When browsing or using it by other developers, you can understand which version of the plug-in should be used. At the same time, when installing with npm install, the required package will be downloaded according to the specified content. In addition, using npm install --production will only download the dependencies node packages

Use npm to uninstall the plug-in, the syntax format is: npm uninstall <name> [-g][--save-dev]
3.1 Do not delete the plug-in package directly locally
3.2 Delete all plug-ins, the syntax is: npm uninstall gulp-less gulp-gulify gulp-concat...
3.3 Delete at one time with rimraf: npm install rimraf -g, usage rimraf node_modules

Use npm to update the plugin, the syntax is: npm update <name> [-g] [--save-dev]
4.1 Update all plug-ins: ```npm update [--save-dev]

Check npm help, syntax: npm help

View installed plug-ins, syntax: npm list

# Choose cnpm

The npm service is abroad, so it is greatly affected by the network and abnormalities often occur. The Taobao team has made a complete mirror for this, and the version synchronization frequency is 10 minutes every time. The official website address is:

Install cnpm and use the commandnpm install cnpm -g --registry=After the installation is completed, use cnpm -v to check whether the installation is successful
The usage method of cnpm is exactly the same as npm. You only need to change npm to cnpm.

# Create a new file

Files are essential configuration files for nodejs-based projects, and they are saved in the root directory as normal json files.
The content and format are roughly as follows: (Json files cannot be commented, please delete them)

{
 "name": "test", // Project name (must) "version": "1.0.0", // Project version (required) "description": "project description!", // Project description (must) "homepage": "", // Project homepage "repository": { // Project Resource Library  "type": "git",
  "url": "https://xxxx/xxx"
 },
 "author": { // Project author information  "name": "surging",
  "email": "xxx@"
 },
 "license": "ISC", // Project License Agreement "devDependencies": { // Project dependencies plug-in, adding -dev installation will automatically be added here  "gulp": "^3.8.11",
  "gulp-less": "^3.0.0"
 }
}

Create file, syntax npm init

cd ~/workspace/demo
npm init

Then follow the prompts to fill in the information, as follows:

npm init

Then you can see a file generated in the folder, and the printing information is as follows:

Information printing

Of course you can create it manually, but I believe you won't do that.

4. Check the help document, the syntax is: cnpm help

# Postscript

This article introduces the commonly used usage of npm. If you have different opinions and suggestions, please feel free to communicate in the comment area~