Nodejs 包管理 - npm(持续学习更新ing……)

npm 管理工具使用(默认linux系统,mac会有标注)

注:参考官方文档整理,原文档地址:https://docs.npmjs.com/

一、Package Introduction ?

1.Understanding Packages and Modules?

(1) what is package? A package is any of the following:

- a) A folder containing a program described by a package.json file.
- b) A gzipped tarball containing (a).
- c) A url that resolves to (b).
- d) A <name>@<version> that is published on the registry with (c).
- e) A <name>@<tag> that points to (d).
- f) A <name> that has a latest tag satisfying (e).
- g) A git url that, when cloned, results in (a).

(2) what is modules? A module is anything that can be loaded with require() in a Node.js program. The following are all examples of things that can be loaded as modules:

- a) A folder with a package.json file containing a main field.
- b) A folder with an index.js file in it.
- c) A JavaScript file.
1. Package Version Rules:

注:语义化版本规范,地址:https://semver.org/lang/zh-CN/
Code status | Stage | Rule | Example version
---|---|---|---
First release | New product | Start with 1.0.0 | 1.0.0
Backward compatible bug fix | Patch release | Increment the third digit | 1.0.1
Backward compatible new feature | Minor release | Increment the middle digit and reset last digit to zero | 1.1.0
Changes that break backward compatibility | Major release | Increment the first digit and reset middle and last digits to zero | 2.0.0

2. package.json 介绍:
注:全局配置
> npm set init.author.email "[email protected]"
> npm set init.author.name "xxx"
> npm set init.license "xxx"
1.默认文件 (default package.json):
{
  "name": "demo",
  "description": "",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/ashleygwilliams/my_package.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/ashleygwilliams/my_package/issues"
  },
  "homepage": "https://github.com/ashleygwilliams/my_package"
}
属性名 是否必需 作用
name 必填 当前所在文件夹名,即项目名(一般放在项目根目录)
version 必填 当前版本号,默认第一个版本:1.0.0,详细参见↑(1.Package Version Rules)
description 可选 当前项目描述,可以截取 README.md 内容
main 可选 启动默认指定文件,即项目入口
scripts 可选 shell命令,通过npm run [参数] 执行
keywords 可选 empty
author 可选 项目作者名
license 可选 许可声明,默认 ISC(开源声明的一种)
bugs 可选 fix bug 提交的 分支/仓库 地址
homepage 可选 发布地址,即 master 分支, 或官方首页
2.package.json 扩展属性:
属性名 是否必需 作用
dependencies 可选 发布版本所需的依赖包(npm install
devDependencies 可选 开发版本所需的依赖包(npm install

二、How to use npm.

1.npm 查看版本号:
    npm -v
2.npm 最新发布版本(install/update):
    npm install npm@latest -g
3.npm 最新测试版本(install/update):
    npm install npm@next -g
4.npm 修改包 安装目录(解决权限问题(sudo): permissions errors):
(1)方法一:
    ① mkdir ~/.npm-global 
        //在当前用户的根目录下创建文件夹
    ② npm config set prefix '~/.npm-global' 
        //配置自定义文件目录到 npm 全局配置文件
    ③ export PATH=~/.npm-global/bin:$PATH
        //配置自定义目录文件/bin 到环境变量
    ④ source ~/.profile
        //更新当前用户下的环境变量配置文件
(2)方法二:
    ① mkdir ~/.npm-global 
        //在当前用户的根目录下创建文件夹
    ② sudo vi /etc/profile (mac: /etc/paths)
        //编辑全局环境变量,添加 NPM_CONFIG_PREFIX=~/.npm-global
    ④ source /etc/profile
5.npm CLI:
1.npm install/uninstall 用法示例:
npm install (with no args, in package dir)
npm install [<@scope>/]<name>
npm install [<@scope>/]<name>@<tag>
npm install [<@scope>/]<name>@<version>
npm install [<@scope>/]<name>@<version range>
npm install <git-host>:<git-user>/<repo-name>
npm install <git repo url>
npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install -g <package_name>

npm uninstall
npm uninstall <package_name>
npm uninstall -g <package_name>

alias: npm i
common options: 
[-P|--save-prod]
[-D|--save-dev]
[-O|--save-optional]
[-E|--save-exact] 
[-B|--save-bundle] 
[--no-save] 
[--dry-run]
2.npm update 用法示例:
npm update
npm update -g
npm update -g <package> / npm install npm@latest -g
npm outdated -g --depth=0

猜你喜欢

转载自www.cnblogs.com/changbaihe/p/10197268.html