【前端系列】学习 NPM

3,154 阅读6分钟
原文链接: ie8384.com
0

npm 是每一位前端工程师都必备的技能。入门前端,就从入门npm开始吧。

一、什么NPM

NPM是随同NodeJS一起安装的包管理工具,常见的有以下几种使用场景:

  • 允许用户从NPM服务器下载别人编写的第三方包到本地使用;

  • 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用;

  • 允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用;

二、安装NPM

所以,首先安装NodeJs,下载地址 https://nodejs.org/en/download/ 打开NodeJS官网时,会发现有两个版本:LTS(Long Term Support)和Current。官方也提示了,大多数用户建议使用LTS,如果想体验最新的特性可以使用Current版本。每个版本包含的NPM版本也有提示。

在Mac上可以采用Homebrew安装。

Homebrew是什么? 可以理解为对应Red hat上的yum,Ubuntu的apt-get。不过Homebrew不是苹果的,是第三方的,官网 https://brew.sh/index_zh-cn.html

1、安装Homebrew,官网有命令:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2、安装node

brew install node

3、查看node安装成功与否

node -v

4、node安装成功后,npm也一起安装了,查看npm版本

npm -v

三、npm 命令

输入npm,即可看到npm 支持的命令

lufeis-MacBook-Pro:bin lufei$ npmUsage: npm <command>where <command> is one of:    access, adduser, bin, bugs, c, cache, completion, config,    ddp, dedupe, deprecate, dist-tag, docs, doctor, edit,    explore, get, help, help-search, i, init, install,    install-test, it, link, list, ln, login, logout, ls,    outdated, owner, pack, ping, prefix, profile, prune,    publish, rb, rebuild, repo, restart, root, run, run-script,    s, se, search, set, shrinkwrap, star, stars, start, stop, t,    team, test, token, tst, un, uninstall, unpublish, unstar,    up, update, v, version, view, whoaminpm <command> -h     quick help on <command>npm -l           display full usage infonpm help <term>  search for help on <term>npm help npm     involved overviewSpecify configs in the ini-formatted file:    /Users/lufei/.npmrcor on the command line via: npm <command> --key valueConfig info can be viewed via: npm help confignpm@5.6.0 /usr/local/lib/node_modules/npm

下面着重说几个常用的命令: 1、安装命令,如下命令即安装weex-toolkit,-g表示全局,不加-g表示安装到当前路径

npm install -g weex-toolkit

在Linux 或 Mac,全局安装路径 默认是用户主目录下的nodemodules,非全局安装路径就是命令运行所在路径下的nodemodules。

实际上,模块安装后,本地其实保存了两份,一份在 ~/.npm,一份在nodemodules目录解压后的代码。运行 npm install的时候,只会检查nodemodules目录,而不会检查~/.npm,也就是说,如果一个模块在~/.npm下有压缩包,但是没有安装在node_modules目录中,npm 依然会从远程仓库下载一次新的压缩包。

2、安装并写入package.json,

npm install <name> --save

安装的同时,将信息写入package.json中。 项目路径中如果有package.json文件时,直接使用npm install方法就可以根据dependencies配置安装所有的依赖包,这样代码提交到github时,就不用提交node_modules这个文件夹了。

3、更新模块

npm update <name>

它会先到远程仓库查询最新版本,然后查询本地版本。如果本地版本不存在,或者远程版本较新,就会安装。

4、强制安装

npm install <name> -f

如果你希望,一个模块不管是否安装过,npm 都要强制重新安装,可以使用-f或–force参数

5、卸载模块

npm uninstall <name>

6、搜索模块

npm search express

模块仓库

1、 npm install 如何知道去哪里获取模块?

npm 配置里有仓库地址,所有install时,都会从这个仓库地址里去找模块。 默认的仓库地址是https://registry.npmjs.org/ 可以通过nrm来管理npm的仓库。首先安装nrm,

npm install -g nrm

使用 “nrm ls” 查看所有仓库

lufeis-MacBook-Pro:~ lufei$ 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/* showjoy  http://npm.showjoy.net/

使用 use 来选择当前使用那个仓库

$ nrm use taobao

维护镜像源

nrm del <registry> ### deletenrm add  <registry> <url> [home]  ### addnrm test <name> ###测试某个镜像源的速度,全部测试用 nrm test

2、npm update 如何知道每个模块的最新版本呢?

npm 模块仓库提供了一个查询服务,叫做 registry 。以 npmjs.org 为例,它的查询服务网址是 https://registry.npmjs.org/ 。

这个网址后面跟上模块名,就会得到一个 JSON 对象,里面是该模块所有版本的信息。比如,访问 https://registry.npmjs.org/weex-toolkit,就会看到 weex-toolkit 模块所有版本的信息。

registry 网址的模块名后面,还可以跟上版本号,用来查询某个具体版本的信息。比如访问https://registry.npmjs.org/weex-toolkit/0.6.1 返回的JSON对象里,dist节点下面的tarball属性,是该版本压缩包的网址

"dist": {"shasum": "7d0413b9a761c374f9b0c4a2413e8661921d8408","tarball": "http://registry.npmjs.org/weex-toolkit/-/weex-toolkit-0.6.1.tgz"},

使用淘宝 NPM 镜像

大家都知道国内直接使用 npm 的官方镜像是非常慢的,这里推荐使用淘宝 NPM 镜像。详情见:https://npm.taobao.org/

淘宝 NPM 镜像是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同步。

你可以使用淘宝定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:

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

安装模块

cnpm install [name]

创建模块

Node.js模块就是发布到npm的代码包。 首先创建Module的目录

mkdir demo && cd demo

然后创建package.json,这个过程中命令行会逐步提示你输入这个模块的信息, 其中模块的名字和版本号是必填项

npm init

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 – 关键字

然后编写入口的index.js,示例:

exports.test = function () {  console.log("This is a test");};

要在www.npmjs.org注册一个账号,这个账号会被添加到npm本地的配置中,用来发布module用。

  • 官方注册 https://www.npmjs.com/signup

  • 还有通过命令 npm adduser ,根据提示注册用户

然后登录

npm login

发布,发布过程会把整个目录发布,不想发布的内容模块, 可以通过 .gitignore 或 .npmignore 文件忽略。发布的时候,仓库要设置回 http://registry.npmjs.org

npm publish

第二次修改后,再更新内容和版本号,执行

npm version <版本号>

包安装后的引用,示例

var test = require('toniqian-test-module');test.test();

感谢所有参考文章的作者。

参考链接:

npm 模块安装机制简介

NPM 使用介绍

nodejs npm常用命令

手把手教你创建你的第一个npm包

0