Skip to content
  • package 中声明 bin 命令
json
{
  "bin": {
    "rain": "./bin/www"
  },
}
  • 对应文件增加 #! 指定环境
sh
#! /usr/bin/env_node
  • npm link

  • 切换到官方 npm 源下(nrm use npm)

  • 登录账号 npm addUser

  • npm publish

  • npm i -g

  • npm unpublish

开发依赖、生产依赖、同版本依赖、捆绑依赖(打包依赖)、可选依赖

npm i foo --save-dev 或 npm i foo -D 表示添加进开发依赖 npm i foo --save 或 npm i foo -s 或 npm i foo 表示添加进生产依赖

npm i 表示安装所有依赖 npm i --production 表示只安装生产依赖 npm pack 表示打包成压缩包,里面没有 node_modules,如果要捆绑依赖,在 bundledDependencies 中配置。 同版本依赖指定是

json
{
  "dependencies": {

  },
  "peerDependencies": {

  },
  "devDependencies": {

  },
  "bundledDependencies": {

  }
}

package-lock.json

锁定包的版本,保证不同设备安装的结果完全一样。

版本

major.minor.patch

  • ^2.0.0

    2 版本以上,3 版本以下。

  • ~2.1.0

    2.1 版本以上,2.2 版本以下。

  • =

  • <=

2.1 => 2.2 只是增加 API 2.1.1 => 2.1.2 只是修改 Bug

script

npm run xxx 会将当前 node_modules 文件夹的 bin 目录下命令添加到环境变量中。 npx xxx 直接运行当前 node_modules 文件夹的 bin 目录下命令,多了下载功能,用完就删除。