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
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 automatically
cli.js
View all configuration information of npm
npm config list
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:
follow the steps below to implement it
- Initialize the package first, create a new
my-kaimo-pack
package, and initialize the project below
npm init -y
- Then create the bin configuration, create a new bin folder, add it
kaimo.js
, andpackage.json
configure the bin configuration in
"bin": {
"kaimo": "./bin/kaimo.js"
},
- 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.js
node#! /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)));
- 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 link
the will kaimo.js
module to create a local dependency package.
npm link
- Execute
kaimo 1 2 3 4 5
, you can see that the execution is successful
- unlink
npm unlink
Install project packages (dev/production)
You can npm info xxx
view 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"
}
- bundleDependencies: package dependencies
npm pack
It will be typed in when using the package
"bundleDependencies": [
"jquery"
]
We can see that jquery is packaged in
- 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 versionbeta
: public beta versionrc
: 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
Looking at an example: first installmime
npm i mime
Add script:
"scripts": {
"kaimo": "node ./bin/kaimo.js",
"mime": "mime"
},
npm run mime -- a.js
Why is it OK or not here mime a.js
?
Execute npm run env
, you can see that Path
the variable is added.bin
Just because the default run will npm run
put 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