NPM 使用介绍

导语   NPM 使用介绍  NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种:  允许用户从NPM服务器下载别人编写的第三方包到本地使用。  允许用户从NPM服务
  NPM 使用介绍

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

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

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

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

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

  1. $ npm -v 
  2. 2.3.0 

  如果你安装的是旧版本的 npm,可以很容易得通过 npm 命令来升级,命令如下:

  1. $ sudo npm install npm -g 
  2.  /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js 
  3.  npm@2.14.2 /usr/local/lib/node_modules/npm 

  如果是 Window 系统使用以下命令即可: 

  1. npm install npm -g 

  使用 npm 命令安装模块

  npm 安装 Node.js 模块语法格式如下:

  1. $ npm install 

  以下实例,我们使用 npm 命令安装常用的 Node.js web框架模块 express: 

  1. $ npm install express 

  安装好之后,express 包就放在了工程目录下的 node_modules 目录中,因此在代码中只需要通过 require('express') 的方式就好,无需指定第三方包路径。

  var express = require('express');

  全局安装与本地安装

  npm 的包安装分为本地安装(local)、全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已,比如

  1. npm install express # 本地安装 
  2. npm install express -g # 全局安装 

  如果出现以下错误:

  npm err! Error: connect ECONNREFUSED 127.0.0.1:8087

  解决办法为:

  1. $ npm config set proxy null 

  本地安装

  1. 将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modules 目录。

  2. 可以通过 require() 来引入本地安装的包。

  全局安装

  1. 将安装包放在 /usr/local 下。

  2. 可以直接在命令行里使用。

  3. 不能通过 require() 来引入本地安装的包。

  接下来我们使用全局方式安装 express

  $ npm install express -g

  安装过程输出如下内容,第一行输出了模块的版本号及安装位置。

  1. express@4.13.3 node_modules/express 
  2.  
  3.   ├── escape-html@1.0.2 
  4.  
  5.   ├── range-parser@1.0.2 
  6.  
  7.   ├── merge-descriptors@1.0.0 
  8.  
  9.   ├── array-flatten@1.1.1 
  10.  
  11.   ├── cookie@0.1.3 
  12.  
  13.   ├── utils-merge@1.0.0 
  14.  
  15.   ├── parseurl@1.3.0 
  16.  
  17.   ├── cookie-signature@1.0.6 
  18.  
  19.   ├── methods@1.1.1 
  20.  
  21.   ├── fresh@0.3.0 
  22.  
  23.   ├── vary@1.0.1 
  24.  
  25.   ├── path-to-regexp@0.1.7 
  26.  
  27.   ├── content-type@1.0.1 
  28.  
  29.   ├── etag@1.7.0 
  30.  
  31.   ├── serve-static@1.10.0 
  32.  
  33.   ├── content-disposition@0.5.0 
  34.  
  35.   ├── depd@1.0.1 
  36.  
  37.   ├── qs@4.0.0 
  38.  
  39.   ├── finalhandler@0.4.0 (unpipe@1.0.0) 
  40.  
  41.   ├── on-finished@2.3.0 (ee-first@1.1.1) 
  42.  
  43.   ├── proxy-addr@1.0.8 (forwarded@0.1.0, ipaddr.js@1.0.1) 
  44.  
  45.   ├── debug@2.2.0 (ms@0.7.1) 
  46.  
  47.   ├── type-is@1.6.8 (media-typer@0.3.0, mime-types@2.1.6) 
  48.  
  49.   ├── accepts@1.2.12 (negotiator@0.5.3, mime-types@2.1.6) 
  50.  
  51.   └── send@0.13.0 (destroy@1.0.3, statuses@1.2.1, ms@0.7.1, mime@1.3.4, http-errors@1.3.1) 

  你可以使用以下命令来查看所有全局安装的模块:

  $ npm ls -g

  使用 package.json

  package.json 位于模块的目录下,用于定义包的属性。接下来让我们来看下 express 包的 package.json 文件,位于 node_modules/express/package.json 内容:

  1. { 
  2.  
  3.  "name": "express", 
  4.  
  5.  "description": "Fast, unopinionated, minimalist web framework", 
  6.  
  7.  "version": "4.13.3", 
  8.  
  9.  "author": { 
  10.  
  11.  "name": "TJ Holowaychuk", 
  12.  
  13.  "email": "tj@vision-media.ca" 
  14.  
  15.  }, 
  16.  
  17.  "contributors": [ 
  18.  
  19.  { 
  20.  
  21.  "name": "Aaron Heckmann", 
  22.  
  23.  "email": "aaron.heckmann+github@gmail.com" 
  24.  
  25.  }, 
  26.  
  27.  { 
  28.  
  29.  "name": "Ciaran Jessup", 
  30.  
  31.  "email": "ciaranj@gmail.com" 
  32.  
  33.  }, 
  34.  
  35.  { 
  36.  
  37.  "name": "Douglas Christopher Wilson", 
  38.  
  39.  "email": "doug@somethingdoug.com" 
  40.  
  41.  }, 
  42.  
  43.  { 
  44.  
  45.  "name": "Guillermo Rauch", 
  46.  
  47.  "email": "rauchg@gmail.com" 
  48.  
  49.  }, 
  50.  
  51.  { 
  52.  
  53.  "name": "Jonathan Ong", 
  54.  
  55.  "email": "me@jongleberry.com" 
  56.  
  57.  }, 
  58.  
  59.  { 
  60.  
  61.  "name": "Roman Shtylman", 
  62.  
  63.  "email": "shtylman+expressjs@gmail.com" 
  64.  
  65.  }, 
  66.  
  67.  { 
  68.  
  69.  "name": "Young Jae Sim", 
  70.  
  71.  "email": "hanul@hanul.me" 
  72.  
  73.  } 
  74.  
  75.  ], 
  76.  
  77.  "license": "MIT", 
  78.  
  79.  "repository": { 
  80.  
  81.  "type": "git", 
  82.  
  83.  "url": "git+https://github.com/strongloop/express.git" 
  84.  
  85.  }, 
  86.  
  87.  "homepage": "http://expressjs.com/", 
  88.  
  89.  "keywords": [ 
  90.  
  91.  "express", 
  92.  
  93.  "framework", 
  94.  
  95.  "sinatra", 
  96.  
  97.  "web", 
  98.  
  99.  "rest", 
  100.  
  101.  "restful", 
  102.  
  103.  "router", 
  104.  
  105.  "app", 
  106.  
  107.  "api" 
  108.  
  109.  ], 
  110.  
  111.  "dependencies": { 
  112.  
  113.  "accepts": "~1.2.12", 
  114.  
  115.  "array-flatten": "1.1.1", 
  116.  
  117.  "content-disposition": "0.5.0", 
  118.  
  119.  "content-type": "~1.0.1", 
  120.  
  121.  "cookie": "0.1.3", 
  122.  
  123.  "cookie-signature": "1.0.6", 
  124.  
  125.  "debug": "~2.2.0", 
  126.  
  127.  "depd": "~1.0.1", 
  128.  
  129.  "escape-html": "1.0.2", 
  130.  
  131.  "etag": "~1.7.0", 
  132.  
  133.  "finalhandler": "0.4.0", 
  134.  
  135.  "fresh": "0.3.0", 
  136.  
  137.  "merge-descriptors": "1.0.0", 
  138.  
  139.  "methods": "~1.1.1", 
  140.  
  141.  "on-finished": "~2.3.0", 
  142.  
  143.  "parseurl": "~1.3.0", 
  144.  
  145.  "path-to-regexp": "0.1.7", 
  146.  
  147.  "proxy-addr": "~1.0.8", 
  148.  
  149.  "qs": "4.0.0", 
  150.  
  151.  "range-parser": "~1.0.2", 
  152.  
  153.  "send": "0.13.0", 
  154.  
  155.  "serve-static": "~1.10.0", 
  156.  
  157.  "type-is": "~1.6.6", 
  158.  
  159.  "utils-merge": "1.0.0", 
  160.  
  161.  "vary": "~1.0.1" 
  162.  
  163.  }, 
  164.  
  165.  "devDependencies": { 
  166.  
  167.  "after": "0.8.1", 
  168.  
  169.  "ejs": "2.3.3", 
  170.  
  171.  "istanbul": "0.3.17", 
  172.  
  173.  "marked": "0.3.5", 
  174.  
  175.  "mocha": "2.2.5", 
  176.  
  177.  "should": "7.0.2", 
  178.  
  179.  "supertest": "1.0.1", 
  180.  
  181.  "body-parser": "~1.13.3", 
  182.  
  183.  "connect-redis": "~2.4.1", 
  184.  
  185.  "cookie-parser": "~1.3.5", 
  186.  
  187.  "cookie-session": "~1.2.0", 
  188.  
  189.  "express-session": "~1.11.3", 
  190.  
  191.  "jade": "~1.11.0", 
  192.  
  193.  "method-override": "~2.3.5", 
  194.  
  195.  "morgan": "~1.6.1", 
  196.  
  197.  "multiparty": "~4.1.2", 
  198.  
  199.  "vhost": "~3.0.1" 
  200.  
  201.  }, 
  202.  
  203.  "engines": { 
  204.  
  205.  "node": ">= 0.10.0" 
  206.  
  207.  }, 
  208.  
  209.  "files": [ 
  210.  
  211.  "LICENSE", 
  212.  
  213.  "History.md", 
  214.  
  215.  "Readme.md", 
  216.  
  217.  "index.js", 
  218.  
  219.  "lib/" 
  220.  
  221.  ], 
  222.  
  223.  "scripts": { 
  224.  
  225.  "test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/", 
  226.  
  227.  "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/", 
  228.  
  229.  "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/", 
  230.  
  231.  "test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/" 
  232.  
  233.  }, 
  234.  
  235.  "gitHead": "ef7ad681b245fba023843ce94f6bcb8e275bbb8e", 
  236.  
  237.  "bugs": { 
  238.  
  239.  "url": "https://github.com/strongloop/express/issues" 
  240.  
  241.  }, 
  242.  
  243.  "_id": "express@4.13.3", 
  244.  
  245.  "_shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3", 
  246.  
  247.  "_from": "express@*", 
  248.  
  249.  "_npmVersion": "1.4.28", 
  250.  
  251.  "_npmUser": { 
  252.  
  253.  "name": "dougwilson", 
  254.  
  255.  "email": "doug@somethingdoug.com" 
  256.  
  257.  }, 
  258.  
  259.  "maintainers": [ 
  260.  
  261.  { 
  262.  
  263.  "name": "tjholowaychuk", 
  264.  
  265.  "email": "tj@vision-media.ca" 
  266.  
  267.  }, 
  268.  
  269.  { 
  270.  
  271.  "name": "jongleberry", 
  272.  
  273.  "email": "jonathanrichardong@gmail.com" 
  274.  
  275.  }, 
  276.  
  277.  { 
  278.  
  279.  "name": "dougwilson", 
  280.  
  281.  "email": "doug@somethingdoug.com" 
  282.  
  283.  }, 
  284.  
  285.  { 
  286.  
  287.  "name": "rfeng", 
  288.  
  289.  "email": "enjoyjava@gmail.com" 
  290.  
  291.  }, 
  292.  
  293.  { 
  294.  
  295.  "name": "aredridel", 
  296.  
  297.  "email": "aredridel@dinhe.net" 
  298.  
  299.  }, 
  300.  
  301.  { 
  302.  
  303.  "name": "strongloop", 
  304.  
  305.  "email": "callback@strongloop.com" 
  306.  
  307.  }, 
  308.  
  309.  { 
  310.  
  311.  "name": "defunctzombie", 
  312.  
  313.  "email": "shtylman@gmail.com" 
  314.  
  315.  } 
  316.  
  317.  ], 
  318.  
  319.  "dist": { 
  320.  
  321.  "shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3", 
  322.  
  323.  "tarball": "http://registry.npmjs.org/express/-/express-4.13.3.tgz" 
  324.  
  325.  }, 
  326.  
  327.  "directories": {}, 
  328.  
  329.  "_resolved": "https://registry.npmjs.org/express/-/express-4.13.3.tgz", 
  330.  
  331.  "readme": "ERROR: No README data found!" 
  332.  
  333.  } 

  Package.json 属性说明

  name - 包名。

  version - 包的版本号。

  description - 包的描述。

  homepage - 包的官网 url 。

  author - 包的作者姓名。

  contributors - 包的其他贡献者姓名。

  dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。

  repository - 包代码存放的地方的类型,可以是 git 或 svn,git 可在 Github 上。

  main - main 字段是一个模块ID,它是一个指向你程序的主要项目。就是说,如果你包的名字叫 express,然后用户安装它,然后require("express")。

  keywords - 关键字

  卸载模块

  我们可以使用以下命令来卸载 Node.js 模块。

  $ npm uninstall express

  卸载后,你可以到 /node_modules/ 目录下查看包是否还存在,或者使用以下命令查看:

  $ npm ls

  更新模块

  我们可以使用以下命令更新模块:

  $ npm update express

  搜索模块

  使用以下来搜索模块:

  $ npm search express

  创建模块

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

  $ npm init

  1. This utility will walk you through creating a package.json file. 
  2.  
  3. It only covers the most common items, and tries to guess sensible defaults. 
  4.  
  5. See `npm help json` for definitive documentation on these fields 
  6.  
  7. and exactly what they do. 
  8.  
  9. Use `npm install --save` afterwards to install a package and 
  10.  
  11. save it as a dependency in the package.json file. 
  12.  
  13. Press ^C at any time to quit. 
  14.  
  15. name: (node_modules) runoob # 模块名 
  16.  
  17. version: (1.0.0) 
  18.  
  19. description: Node.js 测试模块(www.aseoe.com) # 描述 
  20.  
  21. entry point: (index.js) 
  22.  
  23. test command: make test 
  24.  
  25. git repository: https://github.com/runoob/runoob.git # Github 地址 
  26.  
  27. keywords: 
  28.  
  29. author: 
  30.  
  31. license: (ISC) 
  32.  
  33. About to write to ……/node_modules/package.json: # 生成地址 
  34.  
  35. { 
  36.  
  37. "name": "runoob", 
  38.  
  39. "version": "1.0.0", 
  40.  
  41. "description": "Node.js 测试模块(www.aseoe.com)", 
  42.  
  43. …… 
  44.  
  45. } 
  46.  
  47. Is this ok? (yes) yes 

  以上的信息,你需要根据你自己的情况输入。在最后输入 "yes" 后会生成 package.json 文件。

  接下来我们可以使用以下命令在 npm 资源库中注册用户(使用邮箱注册):

  $ npm adduser

  Username: mcmohd

  Password:

  Email: (this IS public) mcmohd@gmail.com

  接下来我们就用以下命令来发布模块:

  $ npm publish

  如果你以上的步骤都操作正确,你就可以跟其他模块一样使用 npm 来安装。

  版本号

  使用NPM下载和发布代码时都会接触到版本号。NPM使用语义版本号来管理代码,这里简单介绍一下。

  语义版本号分为X.Y.Z三位,分别代表主版本号、次版本号和补丁版本号。当代码变更时,版本号按以下原则更新。

  如果只是修复bug,需要更新Z位。

  如果是新增了功能,但是向下兼容,需要更新Y位。

  如果有大变动,向下不兼容,需要更新X位。

  版本号有了这个保证后,在申明第三方包依赖时,除了可依赖于一个固定版本号外,还可依赖于某个范围的版本号。例如"argv": "0.0.x"表示依赖于0.0.x系列的最新版argv。

  NPM支持的所有版本号范围指定方式可以查看官方文档。

  NPM 常用命令

  除了本章介绍的部分外,NPM还提供了很多功能,package.json里也有很多其它有用的字段。

  除了可以在npmjs.org/doc/查看官方文档外,这里再介绍一些NPM常用命令。

  NPM提供了很多命令,例如install和publish,使用npm help可查看所有命令。

  NPM提供了很多命令,例如install和publish,使用npm help可查看所有命令。

  使用npm help

 
可查看某条命令的详细帮助,例如npm help install。

  在package.json所在目录下使用npm install . -g可先在本地安装当前命令行程序,可用于发布前的本地测试。

  使用npm update 可以把当前目录下node_modules子目录里边的对应模块更新至最新版本。

  使用npm update -g可以把全局安装的对应命令行程序更新至最新版。

  使用npm cache clear可以清空NPM本地缓存,用于对付使用相同版本号发布新版本代码的人。

  使用npm unpublish @可以撤销发布自己发布过的某个版本代码。

http://www.aseoe.com/ true NPM 使用介绍 http://www.aseoe.com/show-70-802-1.html report <?php echo strlen($content) / 2; ?>   NPM 使用介绍  NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种:  允许用户从NPM服务器下载别人编写的第三方包到本地使用。  允许用户从NPM服务
TAG:NPM
本站欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明: 文章转载自:爱思资源网 http://www.aseoe.com/show-70-802-1.html

[前端插件推荐] Plugin

1 2 3 4
  • jQuery实现逐字逐句显示插件l-by-l.min.js
  • jQuery带方向感知的鼠标滑过图片边框特效插件
  • jQuery HotKeys监听键盘按下事件keydown插件
  • 响应式无限轮播jQuery旋转木马插件
响应式无限轮播jQuery旋转木马插件
web前端开发
爱思资源网 Copyright 2012-2014 Www.Aseoe.Com All rights reserved.(晋ICP备13001436号-1)