npm的介绍

npm 的运行环境是 nodejs ,默认 nodejs 安装之后就可以执行 npm 命令, nodejs 的安装教程可以站内搜索或者百度搜索。
Node安装及修改默认全局模块安装路径 秋楪祈
如果想在一台电脑上同时管理多个 npm 版本,可以安装 nvm。

npm安装与发布模块

npm官网地址
npm 模块安装机制简介

分分钟教会你搭建企业级的 npm 私有仓库

一些常用的库

  • chalk:控制台字符样式

    配置文件package.json

    peerDependencies

    假设有一个 peer 库,在 package.json 中配置:
    1. {
    2. // ...
    3. "peerDependencies": {
    4. "lodash": "^4.17.15"
    5. },
    6. // ...
    7. }
    然后通过 npm publish 发布。在 test 中 npm install peer
    image.png
    会提示:
    image.png
    peer 中不会下载 lodash ,但是会提示需要安装 lodash。

讲了这么多,那么 peerDependencies 有什么用呢?
假设你的项目中安装了 core 库,这时你要安装 pluginA 和 pluginB 库,这两个库都依赖 core 库,如果把 core 库写在 dependencies 中而不是 peerDependencies 中,那么最终目录会变成:

  1. ├── test
  2. └── node_modules
  3. ├── core
  4. ├── pluginA
  5. └── nodule_modules
  6. └── core
  7. └── pluginB
  8. └── nodule_modules
  9. └── core

这样 core 库会被重复安装。 peerDependencies 可以避免类似的核心依赖库被重复下载的问题。
如果把 pluginA 和 pluginB 中的 core 写到 peerDependencies 中:

  1. ├── test
  2. └── node_modules
  3. ├── core
  4. ├── pluginA
  5. └── pluginB

相关文章