npm

npm使用

Posted by Vison on October 17, 2017

npm常用命令

  • npm init: 运行构建新项目的向导
  • npm install module_name: 在项目中安装一个模块
  • npm install -g module_name: 全局方式安装一个模块
  • npm install module_name –save: 在项目中安装一个模块,并把此模块添加到项目配置文件package.json中,作为项目依赖
  • npm install module_name –save-dev: 在项目中安装一个模块,并把此模块添加到项目配置文件package.json中,作为项目开发依赖(devDependency)
  • npm list: 列出项目中已安装的所有模块
  • npm list -g: 列出系统中全局安装的所有模块
  • npm remove module_name: 从项目中移除已安装的模块
  • npm remove -g module_name: 从系统的全局安装中移除已安装的模块
  • npm remove module_name –save: 从项目中移除已安装的模块,并从配置依赖中移除依赖关系
  • npm remove module_name –save-dev: 从项目中移除已安装的模块,并从配置依赖中移除开发依赖(devDependency)关系
  • npm update module_name: 更新指定的已安装模块的版本
  • npm update -g module_name: 更新指定的全局安装模块的版本
  • npm -v: 显示npm包管理器的当前版本
  • npm adduser username: 在npmjs.org创建一个账户
  • npm whoami: 显示你在npmjs.org上的账户详细信息
  • npm publish: 发布自己开发的模块到npmjs.org,要发布模块必须先有账户

npm换源

由于npm的源在国外,经常安装模块失败,所以国内用户还需要翻墙使用,各种不方便。所以就需要换源了,现在国内比较好的有淘宝镜像、cnpmjs镜像等

  • 淘宝镜像:http://registry.npm.taobao.org/
  • cnpmjs镜像:http://r.cnpmjs.org/

使用方法:

// 直接换源
npm config set registry https://registry.npm.taobao.org

// 使用nrm -- NPM registry 管理工具
$ npm install -g nrm
$ nrm ls
* npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
  taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/
$ nrm use taobao

npm scripts

npm 允许在package.json文件里面,使用scripts字段定义脚本命令。

{
 // ...
 "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js --env=jsdom",
    "lint": "eslint ./src",
    "lint-fix": "eslint --fix ./src",
    "build:test": "BUILD_ENV=test node scripts/build.js",
    "build:pre": "BUILD_ENV=pre node scripts/build.js",
    "validate": "npm ls"
  }
}

这些定义在package.json里面的脚本,就称为 npm 脚本,它有很多优点:

  • 项目的相关脚本,可以集中在一个地方;
  • 不同项目的脚本命令,只要功能相同,就可以有同样的对外接口。用户不需要知道怎么运行你的项目,只要运行npm start即可;
  • 可以利用 npm 提供的很多辅助功能;

package.json变量

将package.json字段加入到npm_package_前缀中。所以,例如,如果你{“name”:”foo”, “version”:”1.2.5”}在你的package.json文件中, 那么你的包脚本会把 npm_package_name环境变量设置为“foo”, npm_package_version设置为“1.2.5”

脚本定义和执行

上面代码是package.json文件的一个片段,里面的scripts字段是一个对象。它的每一个属性,对应一段脚本。比如,build命令对应的脚本是node scripts/build.js。 命令行下使用npm run命令,就可以执行这段脚本。

$ npm run build
# 等同于执行
$ node scripts/build.js

默认脚本

npm 会提供一些默认脚本, 默认脚本可以在scripts里面重写

"start": "node server.js":

// If there is a server.js file in the root of your package, then npm will default the start command to node server.js.

"install": "node-gyp rebuild":

// If there is a binding.gyp file in the root of your package and you haven't defined your own install or preinstall scripts,
 npm will default the install command to compile using node-gyp.

参数传递

npm scripts参数传递的命令行分割符是’- -‘。 比如npm run build – –name hello,即可将后续参数添加到process.env.argv数组中

使用npm发布自己的模块

  • 编写模块 index.js
function isObject(value) {
    const type = typeof value
    return value !== null && (type === 'object' || type === 'function')
}

function isArray(value) {
    const _isArray = Array.isArray || (_arg => Object.prototype.toString.call(_arg) === '[object Array]')
    return _isArray(value)
}

function isEmpty(value) {
    if (value === null || value === undefined) return true
    if (isObject(value)) return Object.keys(value).length === 0
    if (isArray(value)) return value.length === 0

    return false
}

export default {
    isObject,
    isArray,
    isEmpty,
}
  • 初始化package.json
$ npm init

{
  "name": "wecheck",
  "version": "1.0.2",
  "description": "check object",
  "main": "index.js",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/vison123/wecheck.git"
  },
  "keywords": [
    "check"
  ],
  "author": "weisen",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/vison123/wecheck/issues"
  },
  "homepage": "https://github.com/vison123/wecheck#readme"
}

  • 登陆npm账号

若没有账号需要去 https://www.npmjs.com 进行注册,请确认npm的源是否为正式源,若切换为淘宝镜像、cnpm镜像则会登陆失败

$ npm login

  • 发布应用
$ npm publish

  • 常见报错
npm ERR! you do not have permission to publish "your module name". Are you logged in as the correct user?
// 这说明该module name已经被占用,所以在publish之前先在npm上面搜索module name,如果被占用就换个名字吧

You cannot publish over the previously published version 1.0.2.
// 发布应用需要注意维护package.json里面的version,需要大于原版本,否者会报出该错误