4

《胖虎全栈之路 ES6》2.1 npm介绍

 1 year ago
source link: https://www.daguanren.cc/post/npm.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

npm是什么?

想了解npm是什么,给先了解它是为谁服务的,为什么需要它?npm是为NodeJS服务的,是随同NodeJS一起安装的包管理工具。也就是说安装了NodeJS就默认安装了npm。是不是很密切?没有错,NodeJS是服务器脚本语言,和它类似的语言有java、asp.net、php等。npm类似他们的工具包,把具有共性的代码做成模块(比如:时间格式化工具、微信支付代码等具有共性的代码),存放并上传到npm中,供其他程序员一起使用。当任何人需要调用它的时候,从npm下载下来使用即可。

npm有以下特性:

  • 允许用户从NPM服务器下载别人编写的模块到本地使用。
  • 允许用户将自己编写的模块上传到NPM服务器供别人使用。

npm的安装

npm的常规命令

1.npm自身管理篇

显示npm安装的版本号

$ npm -v

更新安装好的npm到最新版本

$ sudo npm install npm -g

2.使用别人的模块篇(以安装express为例)

模糊搜索npm网上的某个模块

$ npm search express

安装查询到的模块

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

$ npm install express

(全局安装)会将安装包放在 /usr/local/lib/node_modules下。

$ npm install express -g

查看已安装的模块

全局安装的模块可以在 /usr/local/lib/node_modules下查看 在当前目录下安装的模块,可以在当前目录下的package.json中的dependencies中查看

查看已安装的模块

卸载当前目录模块

$ npm uninstall express

卸载全局模块

$ npm uninstall express -g

更新已安装的模块

更新当前目录模块

$ npm update express

更新全局模块

$ npm update express -g

3.发布自己的模块篇

$ npm initThis 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 fieldsand exactly what they do.Use `npm install <pkg> --save` afterwards to install a package andsave it as a dependency in the package.json file.Press ^C at any time to quit.name: (node_modules) test # 模块名version: (1.0.0) description: 描述 # 描述entry point: (index.js) test command: make testgit repository: # Github 地址keywords: author: license: (ISC) About to write to ……/node_modules/package.json: # 生成地址 "name": "test", "version": "1.0.0", "description": "描述",Is this ok? (yes) yesapplescript
上传自己的模块

1.到npm官网注册一个账号 https://www.npmjs.com/signup

2.本地终端登录账号信息

$ npm adduserUsername: xuhePassword:Email: (this IS public) [email protected]groovy

3.发布当前目录下的文件

$ npm publish

4.查看自己发布的包

$ npm search 包名

模块版本号规范

使用NPM下载和发布代码时都会接触到版本号。NPM使用语义版本号来管理代码,这里简单介绍一下。 语义版本号分为X.Y.Z三位,分别代表主版本号、次版本号和补丁版本号。当代码变更时,版本号按以下原则更新。 如果只是修复bug,需要更新Z位。 如果是新增了功能,但是向下兼容,需要更新Y位。 如果有大变动,向下不兼容,需要更新X位。 版本号有了这个保证后,在申明第三方包依赖时,除了可依赖于一个固定版本号外,还可依赖于某个范围的版本号。例如"argv": "0.0.x"表示依赖于0.0.x系列的最新版argv。

在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK