npm basic operation and command explanation

Introduction to npm:

npm is a package management tool for Node.js, which can be used to install, upgrade, and delete Node.js modules. npm is the package manager that comes with Node.js. It is easy to use. You can easily use third-party modules in the project, and you can also publish your own modules for others to use.

Common commands of npm:

Install the module:

npm install express

Upgrade modules:

npm update express

Remove module:

npm uninstall express

Check installed modules:

npm ls

Check the version of the module:

npm view express version

Initialize the project:

npm init

Release module:

npm publish

Update the module version number:

npm version patch

Install native modules:

The npm install command installs local modules, such as log modules, database modules, etc. When installing a local module, you need to add the --save or --save-dev option to the command to automatically update the dependencies in the package.json file after the installation is complete.

npm install module-name --save
npm install module-name --save-dev

Manage dependencies:

Dependencies need to be added manually, using the --save or --save-dev option of the npm install command

npm install module-name --save
npm install module-name --save-dev

To uninstall a module, use the npm uninstall command:

npm uninstall module-name

Management command script:

These commands are typically used to run tests, build applications, deploy applications, etc.

Script commands defined in the package.json file, and then use the npm run command to run these commands. For example, define a test script in the package.json file:

"scripts": {
    
    
  "test": "mocha test/*.js"
}

You can use the npm run command to run this test script:

npm run test

In addition to the test script, you can also define other custom scripts, such as: build script, start script, etc.

When using npm, you can run some custom build and start commands by defining custom scripts in your package.json file. Among them, the two most common custom scripts are build script and start script.

Custom build script:

Build scripts are often used to build applications, such as packaging applications into deployable code packages. In the package.json file, you can use the "scripts" field to define your own build script:

"scripts": {
    
    
  "build": "webpack --config webpack.config.js"
}

A script named build is defined in the code, which will execute the webpack command and specify the webpack configuration file as webpack.config.js. When running the npm run build command, npm will automatically execute the defined build script, that is, execute the webpack packaged application.

Custom start script:

The start script is usually used to start the application. In the package.json file, you can use the "scripts" field to define your own start script:

"scripts": {
    
    
  "start": "node app.js"
}

In the code, a script named start is defined, which will execute the node command and start the app.js file. When running the npm start command, npm will automatically execute the defined start script, that is, start the application.

Guess you like

Origin blog.csdn.net/weixin_43749805/article/details/130648561