一 权限操作

npm允许通过scope组织私有包,通过team细化权限控制

npm官方仓储有两种类型的包,普通包和scope包

普通包特征: - 只能公有,谁都可以下载使用 - 仅可以通过所有者(owner)进行权限控制,如果要允许某个用户修改或发布包,必须将该用户添加到包的所有者列表。添加到包所有者列表的用户具备所有的权限.

scope包特征:
- 包名有两部组成,@scope/name, @后的为scope名,/后的才是具体的包名 - 可以控制公有和私有 - 细化的权限控制,比如可以创建团队,并赋予团队对包只读/修改的权限

1. owner

1
2
3
npm owner add <user> [<@scope>/]<pkg> # 将用户添加到包的所有者列表  
npm owner rm <user> [<@scope>/]<pkg> # 从包的所有这列表中删除用户
npm owner ls [<@scope>/]<pkg> # 列出包的所有者

成为包的所有者的用户,将能够修改元数据(如标记弃用),发布新版本,添加其他用户到包的所有者列表

2. t/team

1
2
3
4
5
6
7
8
9
npm team create <scope:team> # 创建团队  
npm team destroy <scope:team> # 删除团队

npm team add <scope:team> <user> # 添加用户到团队
npm team rm <scope:team> <user> # 从团队中移除用户

npm team ls <scope>|<scope:team> 列出团队/成员

npm team edit <scope:team> 用编辑器编辑团队信息

3. access

1
2
3
4
5
6
7
8
9
npm access public [<package>]  # 设置包开放  
npm access restricted [<package>] # 设置包私有

npm access grant <read-only|read-write> <scope:team> [<package>] # 设置团队对包可以只读/允许修改
npm access revoke <scope:team> [<package>] # 从团队中收回包权限

npm access ls-packages [<user>|<scope>|<scope:team>] # 列出用户/域/团队能够访问的包
npm access ls-collaborators [<package> [<user>]] # 列出包的权限信息
npm access edit [<package>] # 用编辑器编辑包权限

4. adduser/login

1
npm adduser [--registry=url] [--scope=@orgname] [--always-auth]

提示输入username, password, email,进行登录校验,返回token保存到.npmrc

5. logout

1
npm logout [--registry=<url>] [--scope=<@scope>]

请求仓储服务将当前token失效

6. whoami

1
npm whoami [--registry <registry>]

列出用户在npmjs.org上的用户名

二 远程操作

1. s/se/search

1
npm search [-l|--long] [--json] [--parseable] [--no-description] [search terms ...]
  • -l|–long: 展示出全部的DESCRIPTION栏信息
  • –no-description: 不显示DESCRIPTION栏信息

2. publish

1
npm publish [<tarball>|<folder>] [--tag <tag>] [--access <public|restricted>]
  • –tag: 带上tag信息发布,之后包可以通过npm install <name>@<tag>安装
  • –access: 仅适用于scope包,默认为restricted

3. unpublish

1
npm unpublish [<@scope>/]<pkg>[@<version>]

从仓储中删除包,该操作会破坏依赖,不推荐适用,如果是为了鼓励用户适用新版本,可以使用deprecate命令

4. deprecate

1
npm deprecate <pkg>[@<version>] <message>

标记包弃用,用户在安装时npm会有警告

5. stars

1
npm stars [<user>]

查看用户喜欢的包

6. star/unstart

1
2
npm star [<pkg>...]  
npm unstar [<pkg>...]

标记喜欢/取消喜欢标记

三 本地操作

1. init

1
npm init [-f|--force|-y|--yes]

初始化package.json, 默认会有很多输入提示,可以通过-f|--force|-y|--yes选项创建默认配置的package.json 已经存在package.json后再次运行npm init不会破坏已有配置,只会变更你真正改动的部分

2. i/install

1
2
3
4
5
6
7
8
9
npm install (with no args, in package dir) # 读取package.json安装  
npm install [<@scope>/]<name> # 默认安装标签为latest
npm install [<@scope>/]<name>@<tag> # 指定标签
npm install [<@scope>/]<name>@<version> # 指定版本
npm install [<@scope>/]<name>@<version range> # 指定版本范围
npm install <tarball file> # 通过tarball文档安装
npm install <tarball url> # 通过tarball文档url链接安装
npm install <git remote url> # 通过git安装包, url格式为<protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish>]
npm install <folder> 通过包所在的文档夹安装
  • –registry: 从指定仓储中下载安装包
  • -S/–save: 安装并保存包信息到package.json的dependencies区
  • -D/–save-dev: 安装并保存包信息到package.json的devDependencies区
  • –tag: 优先根据标签而不是版本安装包
  • –dry-run: 报告安装状况而不真的安装
  • -f/–force: 安装时跳过缓存直接从远程下载
  • -g/–global: 安装到全局
  • –link: 链接全局安装的包的本地
  • –no-shrinkwrap: 安装时忽略shrinkwrap

3. un/uninstall

1
npm uninstall [<@scope>/]<pkg>[@<version>]... [-S|--save|-D|--save-dev]
  • -S/–save: 删除包并移除包在package.json的dependencies区的信息
  • -D/–save-dev: 删除包并移除包在package.json的devDependencies区的信息

4. ddp/dedupe

1
npm dedupe

npm检查包依赖树并清除不要的包

5. dist-tags

1
2
3
npm dist-tag add <pkg>@<version> [<tag>] # 添加标签  
npm dist-tag rm <pkg> <tag> # 移除标签
npm dist-tag ls [<pkg>] # 列出包所包含的标签

常见标签有latest, next, lts等

可以在发布和下载包是带上标签

1
2
3
4
5
npm publish # 默认标签latest  
npm publish --tag next # 发布next标签
npm install npm # 默认标签latest
npm install npm@next
npm install --tag next
  • –registry: 发布包到指定仓储

6. v/version

1
npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]

该命令执行步骤 1. 检查git工作目录

  1. 运行preversion脚本, 可以写些触发测试的脚本
  2. 结合当前包当前版本信息和patch, minor, major等,生成新版本号,更新package.json中version字段
  • patch 1.0.0 => 1.0.1 - prepatch 1.0.0 => 1.0.1-0 - minor 1.0.0 => 1.1.0 - preminor 1.0.0 => 1.1.0-0 - major 1.0.0 => 2.0.0 - premajor 1.0.0 => 2.0.0-0 - prerelease 1.0.0-0 => 1.0.0-1 - from-git 从git获取版本信息 4. 运行version脚本
  1. git提交并打标签
  2. 运行postversion脚本

7. it/install-test

1
2
npm it  
npm install-test

相当于运行npm install && npm test

8. ln/link

1
2
npm link  # 在全局node_modules下创建当前文档夹的超链接  
npm link [<@scope>/]<pkg>[@<version>] # 将全局安装的包链接到本地node_modules中

9. ls/list

1
npm ls [[<@scope>/]<pkg> ...]

打印依赖树

  • –json: 已json格式输出
  • –long: 展示更多信息
  • –parseable: 显示展平的目录而不是依赖树
  • –global: 显示全局安装的包的依赖树
  • –depth: 树层级,从0开始
  • –prod/production: 仅显示package.json里dependencies包的依赖
  • –dev: 仅显示package.json里devDependencies包的依赖

10. up/update

1
npm update [-g] [<pkg>...]

更新包到包的semver所允许的最新版本, 并安装遗漏的包

  • –save: 更新并保存更新到package.json
  • –dev: 同时更新devDependencies中的包
  • –depth: 默认情况下仅更新顶层(–depth=0)为0的包,如果想更新所有包,可以指定–depth=9999

11. outdated

1
npm outdated [[<@scope>/]<pkg> ...]

.e.g

1
2
3
4
Package        Current  Wanted  Latest  Location  
ajv 4.8.2 4.11.8 5.0.1 example
async 2.1.2 2.4.0 2.4.0 example
body-parser 1.15.2 1.17.1 1.17.1 example

列表栏

  • Current: 当前版本
  • Wanted: smever允许的最高版本
  • Latest: 仓储中最新版本
  • Location: 依赖树中的位置

命令选项

  • –json: 已json格式输出
  • –long: 展示更多信息
  • –parseable: 平铺展示
  • –global: 显示全局安装的包的依赖树
  • –depth: 树层级,默认0

12. pack

1
npm pack [[<@scope>/]<pkg>...]

从包生成名为<name>-<version>.tgz的tarball,并缓存

13. prune

1
npm prune [[<@scope>/]<pkg>...] [--production]

清理不在package.json生成的依赖树中的包

  • –production: 移除devDependencies中的包

14. shrinkwrap

1
npm shrinkwrap

shrinkwrap用来锁定依赖包的版本

包A的package.json

1
2
3
4
5
6
7
{
"name": "A",
"version": "0.1.0",
"dependencies": {
"B": "<0.1.0"
}
}

包A的依赖树

1
2
3
A@0.1.0
`-- B@0.0.1
`-- C@0.0.1

当B有新版本0.0.2发布, B@0.0.2满足<0.1.0, 所以npm install A安装成功后依赖树

大专栏  npm参考手册>
1
2
3
A@0.1.0
`-- B@0.0.2
`-- C@0.0.1

我们希望包A依赖的B版本保持在B@0.0.1, 可以运行

1
npm shrinkwrap

该命令会生成npm-shrinkwrap.json, 其内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"name": "A",
"version": "0.1.0",
"dependencies": {
"B": {
"version": "0.0.1",
"from": "B@^0.0.1",
"resolved": "https://registry.npmjs.org/B/-/B-0.0.1.tgz",
"dependencies": {
"C": {
"version": "0.0.1",
"from": "org/C#v0.0.1",
"resolved": "git://github.com/org/C.git#5c380ae319fc4efe9e7f2d9c78b0faa588fd99b4"
}
}
}
}
}

运行npm install时如果存在npm-shrinkwrap.json, npm在安装包时会根据shrinkwrap.json锁定依赖包的版本

15. cache

1
2
3
4
5
6
7
8
npm cache add <tarball file> # 添加到缓存  
npm cache add <folder>
npm cache add <tarball url>
npm cache add <name>@<version>

npm cache ls [<path>] # 缓存明细

npm cache clean [<path>] # 清除缓存

缓存路径可以通过npm config get cache获取

四 脚本

package.json的scripts区可以用来定义自定义脚本

1. run/run-script

1
npm run <command> [-- <args>...]

运行package.json的scripts中定义的命令

npm run会自动将node_modules/.bin添加到环境变量PATH中。如果本地安装过mocha, 可以这样编写"scripts": {"test": "mocha test/*.js"}而不需要"scripts": {"test": "node_modules/.bin/tap test/*.js"}

2. start

1
npm start [-- <args>]

等同与npm run start [-- <args>]

3. stop

1
npm stop [-- <args>]

等同与npm run stop [-- <args>]

4. tst/test

1
npm test [-- <args>]

等同与npm run test [-- <args>]

5. rb/rebuild

1
npm rebuild [[<@scope>/<name>]...]

运行指定包中的build脚本,适用于更新node版本后,重新编译C++包

6. restart

1
npm restart [-- <args>]

循序执行1. prerestart 2. prestop 3. stop 4. poststop 5. restart 6. prestart 7. start 8. poststart 9. postrestart

五 配置

1. c/config

1
2
3
4
5
npm config set <key> <value> [-g|--global] # 添加或更新  
npm config get <key> # 获取
npm config delete <key> # 删除
npm config list # 配置明细
npm config edit # 编辑器编辑
  • –global: 全局配置

2. get

1
npm get <key> # 同npm config get

3. set

1
npm set <key> <value> [-g|--global] #同npm config set

六 查看

1. root

1
2
npm root # 打印本地node_modules目录  
npm root -g # 打印全局node_modules目录

2. prefix

1
2
npm prefix # 打印包含package.json最近父目录  
npm prefix -g # 打印全局配置prefix的值

3. view

1
npm view [<@scope>/]<name>[@<version>] [<field>[.<subfield>]...]

查看仓储信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
npm view compact

# 打印
{ name: 'compact',
description: 'A JavaScript compacting middleware for express',
'dist-tags': { latest: '0.1.2' },
maintainers: [ 'serby <paul@serby.net>' ],
time:
{ modified: '2017-03-28T12:49:48.000Z',
created: '2012-02-06T01:39:50.261Z',
'0.1.2': '2012-09-04T11:19:17.618Z',
'0.1.1': '2012-08-29T08:18:12.345Z',
'0.1.0': '2012-07-09T14:40:56.751Z',
'0.0.7': '2012-07-04T17:14:01.593Z',
'0.0.6': '2012-06-29T14:29:04.847Z',
'0.0.5': '2012-05-23T10:10:15.010Z',
'0.0.4': '2012-03-31T09:05:40.450Z',
'0.0.3': '2012-03-23T15:25:18.289Z',
'0.0.2': '2012-03-21T18:15:24.718Z',
'0.0.1': '2012-02-06T01:39:50.261Z' },
users: { serby: true },
author: 'Paul Serby <paul@serby.net>',
repository: { type: 'git', url: 'git://github.com/serby/compact.git' },
versions:
[ '0.0.1',
'0.0.2',
'0.0.3',
'0.0.4',
'0.0.5',
'0.0.6',
'0.0.7',
'0.1.0',
'0.1.1',
'0.1.2' ],
version: '0.1.2',
main: './lib/compact.js',
scripts: { test: 'mocha -r should -R spec' },
engines: { node: '>=0.8' },
dependencies:
{ lodash: '~0.3',
async: '~0.1',
'uglify-js': '~1.3',
mkdirp: '~0.3' },
devDependencies: { mocha: '*', should: '~1.1', async: '~0.1', asyncjs: '~0.0' },
optionalDependencies: {},
dist:
{ shasum: '66361e17108185bf261d42aff6a91b925e473139',
size: 7603,
noattachment: false,
tarball: 'http://registry.npm.taobao.org/compact/download/compact-0.1.2.tgz' },
directories: {},
publish_time: 1346757557618 }
1
2
3
4
5
6
7
npm view compact@0.1.2 dependencies

# 打印
{ lodash: '~0.3',
async: '~0.1',
'uglify-js': '~1.3',
mkdirp: '~0.3' }

4. bin

1
2
npm bin # 打印包含npm bin目录, 通常为node_modules/.bin/  
npm prefix -g # 打印全局npm bin目录

5. bugs/issue

1
npm bugs [<packagename>]

打开包bug追踪url

1
npm bugs npm # 浏览器打开https://github.com/npm/npm/issues

6. docs/home

1
2
3
4
npm docs [<pkgname> [<pkgname> ...]]  
npm docs .
npm home [<pkgname> [<pkgname> ...]]
npm home .

打开文档url

1
npm docs npm #浏览器打开https://docs.npmjs.com/

7. repo

1
npm repo [<pkg>]

打开git url

1
npm repo npm #浏览器打开https://github.com/npm/npm

8. help

1
npm help <term> [<terms..>]

打印特定术语或命令的帮助

9. help-search

1
npm help-search <text>

从npm官方markdown文档中搜索词条

七 其他

1. completion

1
npm completion >> ~/.bashrc

npm命令插补脚本

2. doctor

1
npm doctor

环境检测 - npm能调用node和git命令 - registry能够访问 - 本地和全局node_modules可写 - 缓存存在且tarball文档健全

3. edit

1
npm edit <pkg>[@<version>]

进入包目录并启动编辑器

4. explore

1
npm explore <pkg> [-- <cmd>]

进入包目录并运行命令

1
2
3
4
npm explore connect -- ls

# 打印
HISTORY.md index.js LICENSE node_modules package.json README.md

5. ping

1
2
npm ping [--registry <registry>]  
npm ping --registry https://registry.npmjs.org

检查仓储是否可用

npm参考手册的更多相关文章

  1. [转] Mongoose 参考手册

    Mongoose 参考手册 标签(空格分隔): MongoDB Mongoose 是什么? 一般我们不直接用MongoDB的函数来操作MongoDB数据库 Mongose就是一套操作MongoDB数据 ...

  2. AngularJS 参考手册

    AngularJS 参考手册 AngularJS 指令 本教程用到的 AngularJS 指令 : 指令 描述 ng-app 定义应用程序的根元素. ng-bind 绑定 HTML 元素到应用程序数据 ...

  3. MySQL 5.1 参考手册CHM (官方 简体中文版)

    点此下载: MySQL 5.1 参考手册CHM (官方 简体中文版) 在线文档:http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chapter/

  4. Vue.js 2.0 参考手册.CHM下载

    下载地址 Vue.js 2.0 参考手册.CHM下载链接: http://pan.baidu.com/s/1kVbhd4b 密码: wxfh

  5. Gulp.js 参考手册,自动化构建利器

    Gulp 是最新的基于 Node 的自动化构建工具,希望能够取代 Grunt,成为最流行的 JavaScript 任务运行器.通过结合 NodeJS 的数据流的能力,只需几步就能搭建起自己的自动化项目 ...

  6. DOM参考手册及事件参考手册

    给全局HTML DOM元素增加函数的方法 HTMLElement.prototype.hasClass = function (className) { return new RegExp(" ...

  7. jQuery 参考手册 - 遍历

    jQuery 参考手册 - 遍历 jQuery Ajax jQuery 数据 jQuery 遍历函数 jQuery 遍历函数包括了用于筛选.查找和串联元素的方法. 函数描述 .add()将元素添加到匹 ...

  8. JavaScript 参考手册——javascript本地和内置对象、BOM、DOM

    本部分提供完整的 JavaScript 参考手册: JavaScript 本地对象和内置对象 Browser 对象(BOM) HTML DOM 对象 JavaScript 对象参考手册 本参考手册描述 ...

  9. hive函数参考手册

    hive函数参考手册 原文见:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF 1.内置运算符1.1关系运算符 运 ...

随机推荐

  1. HCTF2018-admin

    记录一道比较有意思的题目,对于萌新来说能学到很多东西orz.. 三种解法: 1: flask session 伪造 2: unicode欺骗 3: 条件竞争 注册账户查看源码: 发现提示,根据提示和题 ...

  2. Python基础学习二

    Python基础学习二 1.编码 utf-8编码:自动将英文保存为1个字符,中文3个字符.ASCll编码被囊括在内. unicode:将所有字符保存为2给字符,容纳了世界上所有的编码. 2.字符串内置 ...

  3. JS 日期格式化为 2020-11-01 22:33:44 格式

    项目中经常会用到将JS日期格式化输出为 标准格式(2020-11-01 22:33:44)的问题.写个精简版的,代码如下: function formatDate(d){ d = d || new D ...

  4. Ubuntu---gedit 打开windows 下 .txt 文件乱码的解决方法

    问题出现情况:在windows 下编辑的 .txt 文件复制到 Ubuntu 下打开,默认打开方式为 gedit 软件打开,出现如下乱码: 出现原因:在 windows 系统下,.txt 文件默认编码 ...

  5. 【网易官方】极客战记(codecombat)攻略-森林-流星雨star-shower

    流星雨不仅是一个了不起的现象,而且是获得一些钱的好机会. 简介 流星雨正在下着你的宝石和硬币! 但星形金属不是很长寿,硬币很快就消失了. 宝石不会消失. 使用或语句提取密切的金币或宝石: if ite ...

  6. mysql慢SQL排查之show processlist和show full processlist

    mysql排查线上数据库问题,经常会用到 show processlist和show full processlist这两条命令 processlist命令的输出结果显示了有哪些线程在运行,不仅可以查 ...

  7. springCloud eureka服务治理集群增加安全认证

    做为SpringCloud Netflix服务套件中的一部分,springCloud eureka基于Netflix Eureka做了二次封装,默认提供WEB管理页面及服务治理. 为了确保在生产环境中 ...

  8. 刷题34. Find First and Last Position of Element in Sorted Array

    一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...

  9. 0.3W微功率放大器

    电路结构 电路摘自<晶体管电路设计(上)>. 电路采用+5V单电源供电,两级结构.Tr1构成共射极放大电路作为电压放大级:Tr3,Tr4构成推挽的射极跟随器作为输出级:Tr2作为射极跟随器 ...

  10. 四十一、LAMP与LNMP加速与缓存优化进阶实战下部

    一.配置,在nginx和apache所在的服务器中: 1.配置:cd /application/php/lib/php.ini 1)extension_dir="/application/p ...