Node.js npm
Node程序包管理器(NPM)提供了以下两个主要功能:
在线存储库的Node.js包/模块,可搜索 search.nodejs.org
命令行实用程序来安装Node.js的包,做版本管理和Node.js包依赖管理。
NPM捆绑v0.6.3版本在一起以后,Node.js可直接安装。为了验证一致性,打开控制台,然后输入以下命令,看到的结果:
$ npm --version 2.7.1
如果您正在运行旧版本npm,那么可以将其更新到最新版本。只要从root使用以下命令:
$ sudo npm install npm -g /usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js npm@2.7.1 /usr/lib/node_modules/npm
使用NPM安装模块
有一个简单安装任何Node.js模块,语法如下:
$ npm install <Module Name>
例如,下面是安装一个著名的Node.jsweb框架模块的命令叫 express:
$ npm install express
现在,你可以在js文件中使用此模块如下:
var express = require('express');
全局VS本地安装
默认情况下,NPM安装任何依赖在本地模式。在这里,本地模式是指包安装在node_modules目录位于的地方节点应用程序存在的文件夹中。本地部署的包都可以通过 require()方法进行访问。例如,当我们安装Express模块,它安装Express模块当前目录中创建node_modules目录。
$ ls -l total 0 drwxr-xr-x 3 root root 20 Mar 17 02:23 node_modules
另外,您可以使用npm ls命令列出了所有本地安装的模块。
全局范围内已安装的软件包/依赖性都存储在系统目录中。这种依赖关系可以在任何Node.js的CLI(命令行界面)功能可以使用,但不能直接使用require()的Node应用程序中导入。 现在,让我们尝试使用全局安装Express模块。
$ npm install express -g
这将产生类似的结果,模块将在全局范围内安装。在这里,第一行讲述了在那里得到安装模块版本和它的位置。
express@4.12.2 /usr/lib/node_modules/express ├── merge-descriptors@1.0.0 ├── utils-merge@1.0.0 ├── cookie-signature@1.0.6 ├── methods@1.1.1 ├── fresh@0.2.4 ├── cookie@0.1.2 ├── escape-html@1.0.1 ├── range-parser@1.0.2 ├── content-type@1.0.1 ├── finalhandler@0.3.3 ├── vary@1.0.0 ├── parseurl@1.3.0 ├── content-disposition@0.5.0 ├── path-to-regexp@0.1.3 ├── depd@1.0.0 ├── qs@2.3.3 ├── on-finished@2.2.0 (ee-first@1.1.0) ├── etag@1.5.1 (crc@3.2.1) ├── debug@2.1.3 (ms@0.7.0) ├── proxy-addr@1.0.7 (forwarded@0.1.0, ipaddr.js@0.1.9) ├── send@0.12.1 (destroy@1.0.3, ms@0.7.0, mime@1.3.4) ├── serve-static@1.9.2 (send@0.12.2) ├── accepts@1.2.5 (negotiator@0.5.1, mime-types@2.0.10) └── type-is@1.6.1 (media-typer@0.3.0, mime-types@2.0.10)
您可以使用下面的命令来检查所有全局安装的模块:
$ npm ls -g
使用package.json
package.json是存在于任何Node应用程序/模块的根目录和用于定义一个包的属性。让我们打开package.json在当前 node_modules/express/
{
"name": "express",
"description": "Fast, unopinionated, minimalist web framework",
"version": "4.11.2",
"author": {
"name": "TJ Holowaychuk",
"email": "tj@vision-media.ca"
},
"contributors": [
{
"name": "Aaron Heckmann",
"email": "aaron.heckmann+github@gmail.com"
},
{
"name": "Ciaran Jessup",
"email": "ciaranj@gmail.com"
},
{
"name": "Douglas Christopher Wilson",
"email": "doug@somethingdoug.com"
},
{
"name": "Guillermo Rauch",
"email": "rauchg@gmail.com"
},
{
"name": "Jonathan Ong",
"email": "me@jongleberry.com"
},
{
"name": "Roman Shtylman",
"email": "shtylman+expressjs@gmail.com"
},
{
"name": "Young Jae Sim",
"email": "hanul@hanul.me"
}
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/strongloop/express"
},
"homepage": "http://expressjs.com/",
"keywords": [
"express",
"framework",
"sinatra",
"web",
"rest",
"restful",
"router",
"app",
"api"
],
"dependencies": {
"accepts": "~1.2.3",
"content-disposition": "0.5.0",
"cookie-signature": "1.0.5",
"debug": "~2.1.1",
"depd": "~1.0.0",
"escape-html": "1.0.1",
"etag": "~1.5.1",
"finalhandler": "0.3.3",
"fresh": "0.2.4",
"media-typer": "0.3.0",
"methods": "~1.1.1",
"on-finished": "~2.2.0",
"parseurl": "~1.3.0",
"path-to-regexp": "0.1.3",
"proxy-addr": "~1.0.6",
"qs": "2.3.3",
"range-parser": "~1.0.2",
"send": "0.11.1",
"serve-static": "~1.8.1",
"type-is": "~1.5.6",
"vary": "~1.0.0",
"cookie": "0.1.2",
"merge-descriptors": "0.0.2",
"utils-merge": "1.0.0"
},
"devDependencies": {
"after": "0.8.1",
"ejs": "2.1.4",
"istanbul": "0.3.5",
"marked": "0.3.3",
"mocha": "~2.1.0",
"should": "~4.6.2",
"supertest": "~0.15.0",
"hjs": "~0.0.6",
"body-parser": "~1.11.0",
"connect-redis": "~2.2.0",
"cookie-parser": "~1.3.3",
"express-session": "~1.10.2",
"jade": "~1.9.1",
"method-override": "~2.3.1",
"morgan": "~1.5.1",
"multiparty": "~4.1.1",
"vhost": "~3.0.0"
},
"engines": {
"node": ">= 0.10.0"
},
"files": [
"LICENSE",
"History.md",
"Readme.md",
"index.js",
"lib/"
],
"scripts": {
"test":"mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/","test-cov":"istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/","test-tap":"mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/","test-travis":"istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/"},"gitHead":"63ab25579bda70b4927a179b580a9c580b6c7ada","bugs":{"url":"https://github.com/strongloop/express/issues"},"_id":"express@4.11.2","_shasum":"8df3d5a9ac848585f00a0777601823faecd3b148","_from":"express@*","_npmVersion":"1.4.28","_npmUser":{"name":"dougwilson","email":"doug@somethingdoug.com"},"maintainers":[{"name":"tjholowaychuk","email":"tj@vision-media.ca"},{"name":"jongleberry","email":"jonathanrichardong@gmail.com"},{"name":"shtylman","email":"shtylman@gmail.com"},{"name":"dougwilson","email":"doug@somethingdoug.com"},{"name":"aredridel","email":"aredridel@nbtsc.org"},{"name":"strongloop","email":"callback@strongloop.com"},{"name":"rfeng","email":"enjoyjava@gmail.com"}],"dist":{"shasum":"8df3d5a9ac848585f00a0777601823faecd3b148","tarball":"http://registry.npmjs.org/express/-/express-4.11.2.tgz"},"directories":{},"_resolved":"https://registry.npmjs.org/express/-/express-4.11.2.tgz","readme":"ERROR: No README data found!"}
Package.json的属性
name - 包的名称
version - 包的版本
description - 包的描述
homepage - 包主页
author - 包的作者
contributors - 贡献者到包的名字
dependencies - 依赖关系的列表。NPM自动安装所有在这里的包node_module文件夹中提到的依赖关系。
repository - 包的库类型和URL
main - 包的入口点
keywords - 关键字
卸载模块
使用下面的命令卸载Node.js的模块。
$ npm uninstall express
一旦NPM卸载包,可以通过查看/node_modules/目录下的内容或验证键入以下命令:
$ npm ls
更新模块
更新package.json并改变其被更新并运行以下命令依赖的版本。
$ npm update express
搜索模块
搜索使用NPM包名。
$ npm search express
创建模块
创建模块的需要要生成的package.json。让我们使用NPM产生package.json,这将产生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 sane 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: (webmaster)
需要提供有关你的模块所需的所有信息。 你可以从上述的package.json文件的帮助,了解所要求的各种信息的含义。 一旦的package.json被产生。使用下面的命令将一个有效的电子邮件地址在NPM库网站上注册自己信息。
$ npm adduser Username: mcmohd Password: Email: (this IS public) mcmohd@gmail.com
现在,发布你的模块的时间:
$ npm publish
如果你的模块正常使用,然后将它发表在库中并且能够使用NPM就像任何其他其他Node.js的模块安装。
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:易百教程 [http:/www.yiibai.com]
本文标题:Node.js npm
本文地址:http://www.yiibai.com/nodejs/nodejs_npm.html
Node.js npm的更多相关文章
- Latest node.js & npm installation on Ubuntu 12.04
转自:https://rtcamp.com/tutorials/nodejs/node-js-npm-install-ubuntu/ Compiling is way to go for many b ...
- 自制node.js + npm绿色版
自制node.js + npm绿色版 Node.js官网有各平台的安装包下载,不想折腾的可以直接下载安装,下面说下windows平台下如何制作绿色版node,以方便迁移. 获取node.exe下载 ...
- Mac 下搭建环境 homebrew/git/node.js/npm/vsCode...
主要记录一下 homebrew/git/node.js/npm/mysql 的命令行安装 1. 首先安装 homebrew 也是一个包管理工具: mac 里打开终端命令行工具,粘下面一行回车安装br ...
- Node.js npm基础安装配置&创建第一个VUE项目
使用之前,我们先来明白这几个东西是用来干什么的. node.js: 一种javascript的运行环境,能够使得javascript脱离浏览器运行.Node.js的出现,使得前后端使用同一种语言,统一 ...
- Node.js NPM Package.json
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json Nod ...
- Node.js NPM 包(Package)
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json 包是打 ...
- Node.js NPM 管理包
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json 根据安 ...
- Node.js NPM 作用
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json NPM ...
- Node.js NPM 教程
NPM是Node.js的包(或模块)管理器,是Node.js应用程序开发的核心. www.npmjs.com上有海量的Node.js包,供免费下载使用. 当安装Node.js时,NPM程序会被同时安装 ...
随机推荐
- java.lang.RuntimeException: Missing type parameter
程序中用到了gson的new typeToken,结果打包成apk发布时,发现抛出异常,但不通过打包apk时发现一切正常,百思不得其解,最初怀疑没有将gson-1.7.1.JAR打包进去,后来经过测试 ...
- poj 3294 Life Forms(后缀数组)
题意:给你最多100个字符串,求最长的且是一半以上的字符串的公共子串,如果有多个,按字典序输出. 思路:先把各个串拼起来,中间加上一个之前未出现过的字符,然后求后缀.然后根据h数组和sa数组,求出最长 ...
- Linux下Postfix的配置和使用
Postfix为何物,详见:http://zh.wikipedia.org/wiki/Postfix 0.关于Postfix postfix的产生是为了替代传统的sendmail.相较于sendmai ...
- Mac OS命令行运行Sublime Text
Opening Sublime Text on command line as subl on Mac OS? Mac OS subl http://www.phodal.com/blog/mac-o ...
- css背景图与html插入img的区别
一直以来都认为css背景图与直接插入img图片的效果是差不多的,直到最近拜读了一位大神的作品,发现大部分图片都是通过背景图形式显示的,于是通过搜索各相关资料,在此总结了下二者的区别: 1. css中的 ...
- javascript新的原生态API
以下是最新的w3c标准的javascript,目前支持运行在firefox, chrome,IE9以上版本的浏览器 参考资料:https://developer.mozilla.org/ru/docs ...
- 怎么让自己的java系统使用支付接口
昨天花了好久的时间学习了支付接口的教,我看了前7集,就够用了,大家上网搜索一下传智播客在线支付还不错. 1.一开始有一个form表单 2.这个表单是他帮你写好的,有很多银行,银行的name都是特定的 ...
- Log4j(1.2.17) - hello world
1. Maven 依赖 <dependencies> <dependency> <groupId>log4j</groupId> <artifac ...
- Shell case正则匹配法
Shell case正则匹配法 case $BOOLEAN in [yY][eE][sS]) echo 'Thanks' $BOOLEAN ;; [yY]|[nN]) echo 'Thanks' ...
- IOC之Unity
项目最近用到,查找到比较好的资料,记录哈: Unity 3(一):简介与示例: Unity 3(二):Unity在AOP方面的应用