文章目录
  1. 1. Defined
  2. 2. Main()
    1. 2.1. config
    2. 2.2. package.json
    3. 2.3. modules
    4. 2.4. { scripts:
    5. 2.5. { version:
    6. 2.6. package-lock.json
  3. 3. Reference

Defined

为转型成现代化前端开始的一条路. 以Manual形式为以后自己作参考

Main()

config

展示所有配置 list可缩写为ls

npm config list -l

获取具体配置 类似于js取属性

npm config get prefix

列出所有module local/global 目录深度 --depth=x

npm list –global –depth=1

package.json

未注明这个tag写的都是json一级对象的内容.

本地建立 --y为一律yes

npm init

这里涉及到相关license,简图 link

modules

json path:

{ dependencies: && { devDependencies:

在work folder下安装各种pack时会自动创建dependencies或者devDependencies对象用于各种平台适配, 保留了各种version.
dev类似于debug模式, 生产模式禁用.

安装

npm install –save[-dev] xxx

卸载

npm uninstall xxx

检查更新 - wanted表示compatible版本(不需要改动原有代码保证兼容性)

npm outdated

更新 link2

npm update [--save/-S]

关于更新有必要提到更多: 涉及到兼容性问题: 即如果不确定pack的稳定性优先使用~

  • ~1.1.1 Tilde Dependence Range: 1.1.1 <= Version < 1.2.0
  • ^1.1.1 Caret Dependence Range: 1.1.1 <= Version < 2.0.0
  • –save 和~可以一起理解为, 如果minor(1.2.x) 版本测试通过, 可以通过该符号表示最低兼容版本升级.

搜索

npm search xxx

{ scripts:

优点: 定制化 有点像linux里面的alias

1
2
3
4
5
scripts: {
"start": "http-server",
"test": "node test.js"
"dev": "node dev.js"
},

执行script (start可以省略run)

npm run dev

{ version:

Semantic Versioning 个人项目精简一下:
version x.y.z
初始版本应该是0.1.0 因为刚开发出来都是没有bug(暂时没发现)的版本. pre-release就不写了

  • z 基本bug修复
  • y 向下兼容的新功能添加
  • x 大版本更新, 例如大规模API更新/替换等会破坏原有代码结构.

package-lock.json

npm5.x.x版本的产物, 搜索的时候看来是模仿的yarn lock file.作用如下

Everything You Wanted To Know About package-lock.json But Were Too Afraid To Ask
The Goal
The purpose of the package-lock is to avoid the situation described above, where installing modules from the same package.json results in two different installs. Package-lock.json was added in npm version 5.x.x, so if you are using major version 5 or higher, you will see it generated unless you disabled it.

这个json的出现是为了保证在任何开发环境下node_modules永远一致, 不会因为像package.json一样每次安装会出现不同的包而开发出来的文件.

如下文所说:

Because the package-lock specifies a version, location and integrity hash for every module and each of its dependencies, the install it creates will be the same, every single time. It won’t matter what device you are on, or when in the future you install, it should give you the same result every time, which is very useful.

还有一个比较重要的点, 平滑过渡所需要做的:

No more deleting that package-lock just to run npm install and regenerate it

Reference

npm 入门教程 | 众成翻译
Everything You Wanted To Know About package-lock.json But Were Too Afraid To Ask || Medium

文章目录
  1. 1. Defined
  2. 2. Main()
    1. 2.1. config
    2. 2.2. package.json
    3. 2.3. modules
    4. 2.4. { scripts:
    5. 2.5. { version:
    6. 2.6. package-lock.json
  3. 3. Reference