初学Node.js,npm cnpm nvm nrm,有木有傻傻分不清楚,做个简单的学习笔记,For me and For you。

  • npm node.js的包管理器。
  • cnpm 中国的node.js的包管理器(npm镜像源为阿里的镜像源)。
  • nvm node.js版本管理的软件,支持你下载不同的版本及切换。(类似的还有一个n,二者的具体的区别看这里
  • nrm 用来切换npm镜像源的一个node.js模块包。

npm

简介

NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种:

  • 允许用户从NPM服务器下载别人编写的第三方包到本地使用。
  • 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用。
  • 允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用。

由于新版的nodejs已经集成了npm,所以之前npm也一并安装好了。同样可以通过输入 “npm -v” 来测试是否成功安装。命令如下,出现版本提示表示安装成功:

npm -v

资源

w3c菜鸟教程:传送门
npm中文文档:传送门

安装

安装Node.Js查看我之前的文章:传送门

使用

npm install express          # 本地安装
npm install express -g   # 全局安装
npm list -g	#查看所有全局安装的模块
npm list  <模块名>	#查看模块的版本号
npm uninstall <模块名> #卸载模块
npm ls #在node_modules目录下执行,查看node_moudles目录下的模块列表
npm update  <模块名> #更新模块
npm search <模块名> #搜索模块
npm --registry https://registry.npm.taobao.org install  <模块名> #临时设置镜像源为淘宝镜像并下载包
npm config set registry https://registry.npm.taobao.org #持久设置镜像源为淘宝镜像
npm config set registry http://www.npmjs.org #切换到官方源,切换其他镜像源站点可以访问https://www.runoob.com/w3cnote/npm-switch-repo.html

关于npm install Xnpm install X --savenpm install X –save-dev的区别参考这里:传送门

创建模块,package.json 文件是必不可少的。我们可以使用 NPM 生成 package.json 文件,生成的文件包含了基本的结果

npm init #创建模块
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (node_modules) runoob                   # 模块名
version: (1.0.0) 
description: Node.js 测试模块(www.runoob.com)  # 描述
entry point: (index.js) 
test command: make test
git repository: https://github.com/runoob/runoob.git  # Github 地址
keywords: 
author: 
license: (ISC) 
About to write to ……/node_modules/package.json:      # 生成地址

{
  "name": "runoob",
  "version": "1.0.0",
  "description": "Node.js 测试模块(www.runoob.com)",
  ……
}


Is this ok? (yes) yes

Package.json 属性说明

  • name - 包名。
  • version - 包的版本号。
  • description - 包的描述。
  • homepage - 包的官网 url 。
  • author - 包的作者姓名。
  • contributors - 包的其他贡献者姓名。
  • dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。
  • repository - 包代码存放的地方的类型,可以是 git 或 svn,git 可在 Github 上。
  • main - main 字段指定了程序的主入口文件,require(‘moduleName’) 就会加载这个文件。这个字段的默认值是模块根目录下面的 index.js。
  • keywords - 关键字

cnpm

简介

大家都知道国内直接使用 npm 的官方镜像是非常慢的,这里推荐使用淘宝 NPM 镜像。
淘宝 NPM 镜像是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同步。

资源

淘宝镜像:传送门

安装

npm install -g cnpm --registry=https://registry.npm.taobao.org

使用

cnpm install <包名>

nvm

简介

在开发中,有时候对node的版本有要求,有时候需要切换到指定的node版本来重现问题等。遇到这种需求的时候,我们需要能够灵活的切换node版本。这里我们使用nvm工具来管理多版本node。

资源

nvm Github地址:传送门

安装

本文主要针对Linux系统,我这里使用的是Ubuntu16.0.4

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

The script clones the nvm repository to ~/.nvm and adds the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).
更新一下

source ~/.profile

配置

使用之前最好再配置一下,要么下载node.js时会很慢。vi ~/.bashrc,插入如下内容:

export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node
export NVM_IOJS_ORG_MIRROR=https://npm.taobao.org/mirrors/iojs

修改完记得source ~/.bashrc生效一下。

使用

nvm install <version> ## 安装指定版本,可模糊安装,如:安装v4.4.0,既可nvm install v4.4.0,又可nvm install 4.4
nvm uninstall <version> ## 删除已安装的指定版本,语法与install类似
nvm use <version> ## 切换使用指定的版本node
nvm ls ## 列出所有安装的版本
nvm ls-remote ## 列出所以远程服务器的版本(官方node version list)
nvm current ## 显示当前的版本
nvm alias <name> <version> ## 给不同的版本号添加别名
nvm unalias <name> ## 删除已定义的别名
nvm reinstall-packages <version> ## 在当前版本node环境下,重新全局安装指定版本号的npm包
nvm cache clear #清理.nvm目录下的.cache目录中的文件,减少存储占用

例子:

nvm install stable #安装最新稳定版 node,现在是 5.0.0
nvm install 4.2.2 #安装 4.2.2 版本
nvm install 0.12.7 #安装 0.12.7 版本

nrm

简介

nrm can help you easy and fast switch between different npm registries, now include: npm, cnpm, taobao, nj(nodejitsu)
简单来说,nrm就是用来一键切换npm镜像源的。

资源

nrm GitHub地址:传送门

安装

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/
  skimdb -- https://skimdb.npmjs.com/registry

切换镜像源

$ nrm use cnpm  //switch registry to cnpm

    Registry has been set to: http://r.cnpmjs.org/

命令大全:

Usage: nrm [options] [command]

  Commands:

    ls                                    List all the registries(列出所有的镜像源)
    current                               Show current registry name(显示当前的镜像源)
    use <registry>                        Change registry to registry(切换镜像源)
    add <registry> <url> [home]           Add one custom registry(增加一个自定义的镜像源)
    set-auth <registry> [value]           Set authorize information for a custom registry with a base64 encoded string or username and pasword
      -a  --always-auth                     Set is always auth
      -u  --username <username>             Your user name for this registry
      -p  --password <password>             Your password for this registry
    set-email <registry> <value>          Set email for a custom registry
    set-hosted-repo <registry> <value>    Set hosted npm repository for a custom registry to publish packages
    del <registry>                        Delete one custom registry(删除自定义的镜像源)
    home <registry> [browser]             Open the homepage of registry with optional browser(打开镜像源的官网)
    test [registry]                       Show the response time for one or all registries
    publish [<tarball>|<folder>]          Publish package to current registry if current registry is a custom registry.  if you\'re not using custom registry, this command will run npm publish directly
      -t --tag [tag]                        Add tag
      -a --access <public|restricted>       Set access
      -o --otp [otpcode]                    Set otpcode
      -dr --dry-run                         Set is dry run
    help                                  Print this help

  Options:

    -h, --help     output usage information
    -V, --version  output the version number
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐