Skip to content

NPM

NPM 是随同 NodeJS 一起安装的包管理工具。

检查 NPM 版本

bash
npm -v

升级 NPM

bash
npm install npm -g

安装模块

bash
npm install <Module Name> #本地安装
npm install <Module Name> -g #全局安装

全局安装

  1. 将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modules 目录。

  2. 可以通过 require() 来引入本地安装的包。

本地安装

  1. 将安装包放在 /usr/local 下或者你 node 的安装目录。

  2. 可以直接在命令行里使用。

查看安装信息

bash
npm list
npm list -g

查看某个模块的版本号

bash
npm list <Module Name>
npm list -g <Module Name>

package.json

package.json 位于模块的目录下,用于定义包的属性

  • name - 包名。
  • version - 包的版本号。
  • description - 包的描述。
  • homepage - 包的官网 url。
  • author - 包的作者姓名。
  • contributors - 包的其他贡献者姓名。
  • dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。
  • repository - 包代码存放的地方的类型,可以是 gitsvngit 可在 Github 上。
  • main - main 字段指定了程序的主入口文件,require('moduleName') 就会加载这个文件。这个字段的默认值是模块根目录下面的 index.js
  • keywords - 关键字

卸载模块

bash
npm uninstall <Module Name>

更新模块

bash
npm update <Module Name>
npm update <Module Name> -g

搜索模块

bash
npm search <Module Name>

创建模块

bash
npm init
# 之后根据提示输入

在 npm 资源库中注册用户(使用邮箱注册)

bash
npm adduser

发布模块

bash
npm publish

版本号

语义版本号分为 X.Y.Z 三位,分别代表主版本号、次版本号和补丁版本号。当代码变更时,版本号按以下原则更新。

  • 如果只是修复 bug,需要更新 Z 位。
  • 如果是新增了功能,但是向下兼容,需要更新 Y 位。
  • 如果有大变动,向下不兼容,需要更新 X 位。

查看帮助

bash
npm help <command>

使用淘宝 NPM 镜像

bash
npm install -g cnpm --registry=https://registry.npm.taobao.org
npm config set registry https://registry.npm.taobao.org

验证镜像是否切换成功

bash
npm config get registry

使用 CNPM 安装模块

bash
cnpm install <Module Name>

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.