40 # npm usage

npm

3n:

  • nrm: source management tool in node
  • nvm: version management tool in node
  • npm: node's package manager, which manages node modules

third party module

There are two types:

  • Global modules: can only be used in the command line, any path is fine
  • Local module: for development or online use

Package initialization work

npm init
npm init -y

Installation of global modules

npm install nrm -g --registry https://registry.npm.taobao.org

After installation, there will be a point to

C:\Program Files\nodejs\nrm -> C:\Program Files\nodejs\node_modules\nrm\cli.js

insert image description here

The principle that nrm is not in the environment variable path and can be used directly:

Put the currently installed module in the npm directory (shortcut key), when the nrm command is executed, it will be executed automaticallycli.js

View all configuration information of npm

npm config list

insert image description here

For nrm use, you can check my article: Use nrm image management tool for npm source management

Write a global module yourself

Implement the following commands to execute on the console

village 1 2 3 4 5 6

The current implementation is definitely not working:
insert image description here
follow the steps below to implement it

  1. Initialize the package first, create a new my-kaimo-packpackage, and initialize the project below
npm init -y

insert image description here

  1. Then create the bin configuration, create a new bin folder, add it kaimo.js, and package.jsonconfigure the bin configuration in
"bin": {
    
    
    "kaimo": "./bin/kaimo.js"
},

insert image description here

  1. Add this line to the file and put it in the header (that is, the Shebang we mentioned in the previous analysis of the require source code), indicating that it is executed in the form of kaimo.jsnode#! /usr/bin/env node
#! /usr/bin/env node

console.log("进入 bin 下的 kaimo.js 文件");

console.log(process.argv.slice(2).reduce((x, y) => Number(x) + Number(y)));
  1. Put the package in the npm global (method 1: global installation, method 2: use npm link)

Used here npm link, it can avoid repeated and cumbersome packaging and publishing operations, bring convenience to development and debugging, and is easy to use. Use npm linkthe will kaimo.jsmodule to create a local dependency package.

npm link

insert image description here

insert image description here

  1. Execute kaimo 1 2 3 4 5, you can see that the execution is successful

insert image description here

  1. unlink
npm unlink

insert image description here

Install project packages (dev/production)

You can npm info xxxview package information with

npm i webpack --save-dev
npm i vue
npm i [email protected]
  • dependencies: project dependencies (–save)
  • devDependencies: development dependencies (–save-dev)
  • peerDependencies: same version dependencies (vue vue-template-compiler)

If we have not installed jquery and configured the following dependencies, we will be prompted

"peerDependencies": {
    
    
    "jquery": "^3.0.0"
}

insert image description here

  • bundleDependencies: package dependencies

npm packIt will be typed in when using the package

"bundleDependencies": [
    "jquery"
]

insert image description here
We can see that jquery is packaged in
insert image description here

  • optionalDependencies: optional dependencies (can be installed or not)

version problem

  • major.minor.patch: Indicates a destructive update. Added features/revised features in major releases. Minor bugs
  • ^2.0.0: limit large version
  • ~2.3.0: restricted version
  • >=
  • <=
  • 1.0.0-2.0.0
  • alpha: closed beta version beta: public beta version rc: final beta ( 2.1.0-beta.1)

run script problem

add script

"scripts": {
    
    
    "kaimo": "node ./bin/kaimo.js"
},

Pass to participate--

npm run kaimo -- 1 2 3 4 5

insert image description here

Looking at an example: first installmime

npm i mime

insert image description here
Add script:

"scripts": {
    
    
    "kaimo": "node ./bin/kaimo.js",
    "mime": "mime"
},

insert image description here

npm run mime -- a.jsWhy is it OK or not here mime a.js?

Execute npm run env, you can see that Paththe variable is added.bin

insert image description here

Just because the default run will npm runput the directory under the global under, all the commands under the current file can be used.node_modules.bin

Another solution is to use the npx command: provided after npm 5.2 (this command is not as easy to manage as npm run), npx can download the package, execute it after the download is complete, and delete the package after execution, so the latest package is taken every time to execute

npm uninstall mime
npx mime a.js

insert image description here

Guess you like

Origin blog.csdn.net/kaimo313/article/details/131326476
40
NPM