1. 程式人生 > >Nodejs 包管理 - npm(持續學習更新ing……)

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 [email protected] -g
3.npm 最新測試版本(install/update):
    npm install [email protected] -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 [email protected] -g
npm outdated -g --depth=0