Basic usage npm

A. Installation

npm based nodejs, and therefore should be installed nodejs
  • You can nodejs official website to download installation
  • We generally choose to install the stable version, that long-term support version
  • Installation is simple, and common software, has been 下一步just fine
nodejs official website

After installation is complete, you can view the following two commands at the command line nodejsand npmversion number; (Here is my current version number)

node -v
- v10.13.0

npm -v
- 6.4.1
View version

II. Update

npmUpdates through npmtheir own, there are two ways
  • Update by specifying the version number
  • Recent updates to the stable release
npm install npm@x.x.x -g
// x.x.x是版本号,指定更新到某个具体版本 -g指全局更新

npm install npm@latest -g
// 最近更新的稳定版

III. Use

Use npmfirst initialization files in the root directory of the project beforenpm
npm init

After initializing the project will have many options
these options can fill their own needs
or use the default, all the way round is also OK
in the project root folder of the package.jsonfile can be seen in the default configuration, or to fill out

Initialization npm

If you do not want or need the option to write your own, and do not want Enter you can use the following this command

npm init -y

IV. Install / update / unloading dependencies

// 安装
npm install xxx
// xxx 是要安装的依赖包
// install 可简写为  i 
npm i xxx
// 默认安装最新版的依赖包

// 安装依赖包 指定版本号
npm i xxx@n.n.n
// n.n.n 是依赖包的指定版本号

// 更新
npm update xxx
// xxx 是要更新的依赖包

// 卸载
npm uninstall xxx
// xxx 是要卸载的依赖包

Installed into the production environment

npm i xxx -S
// -S 是 --save 的缩写;使用 --save(-S) 安装的插件,被写入到 dependencies 对象里面去
// 不写 -S 默认也是安装到生产环境

To install the development environment

npm i xxx -D
// -D 是 -- save-dev;使用 --save-dev(-D) 安装的插件,被写入到 devDependencies 对象里面去

That package.json file inside devDependencies objects and dependencies What difference does it make?

devDependencies inside the plug-in is used only for development environments, not in production environments
dependencies are required to publish to the production environment.

other

Since the problem of domestic network environment, the installation dependencies often unstable NPM; may be used nrmto switch node
after the other switch node operations further continue

// 安装
npm install nrm -g

// 查看节点
nrm ls
// 星号标注的是当前使用的节点
// 破折线前问节点名

// 切换节点
nrm use xxx
// xxx 为节点名

// 测试节点速度
nrm test

// 添加/删除节点
nrm add xxxxxxxxx
nrm del xxxxxxxxx
Switching node
Original Address: https: //www.jianshu.com/p/ceb4606dd482

Guess you like

Origin www.cnblogs.com/jpfss/p/11113225.html