node.js 第三方模块如何安装(使用npm)及介绍

本文详细介绍了Node.js中的第三方模块及其安装、使用方法,包括如何通过npm工具进行模块查找、安装与卸载,并以实际案例展示了常用第三方模块如formidable、connect、express、socket.io和mongodb的安装与基本使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写java代码,遇到问题首先去找相关的第三方APi,已经成为习惯了,java的第三方api真的是太多了。不过也不用羡慕java,node.js也不少,看看官网的介绍:

官方文档的最下面 Appendix 1: Recommended Third-party Modules ,这里是关于第三方模块的介绍,第一段:There are many third party modules for Node. At the time of writing, August 2010, the master repository of modules is the wiki page. 打开这个链接你会发现相当多的第三方模块,上百个是少不了的。

要安装使用这些模块需要使用npm(Node Package Manager)工具,该工具node.js已经集成,这个工具给我感觉像maven。打开命令行,执行npm -h(通常命令行应用程序都可以这样查看帮助,或者是/?,--help),结果如下:


发现有install和uninstall命令,这个就应该是安装和卸载的命令。根据说明以下方式可以查到具体帮助

npm install -h 结果如下:


这个帮助比较简单,具体的可以使用

npm help install 结果如下:




网页有该命令非常详细的介绍。uninstall也同样使用上述方法。


通常在网上看到安装是使用的这种方式 npm install connect,即 npm install <name>。那为什么只是给了name它就能安装,安装地址在哪啊?

install文档中提到了 a <name>@<version> that is published on the registry with (c)

那就看以下registry 的说明,文档下方SEE ALSO中有链接

registry文档中有下面一段话 To resolve packages by name and version, npm talks to a registry website 

that implements the CommonJS Package Registry specification for reading package info.

可以看出名称是由它来解析的,那地址在哪了,下面还有一句

The registry URL is supplied by the registry config parameter. See config(1) for more on managing npm's configuration.

我们在看一下 config(1) 链接,查看config文档,它就是一个全局设置,文档中有下面这段话:

registry

  • Default: https://registry.npmjs.org/
  • Type: url

The base URL of the npm package registry.

看到这个默认地址了吧,就是从这来的。这个就是官方地址:

The official public npm registry is at http://registry.npmjs.org/, It is powered by a CouchDB database at http://isaacs.iriscouch.com/registry.

基本上了解了。

在看一下安装到哪,cmd输入 npm help npm 查看npm文档,有这段描述:

See folders(1) to learn about where npm puts stuff.

In particular, npm has two modes of operation:

  • global mode:
    npm installs packages into the install prefix at prefix/lib/node_modules and bins are installed in prefix/bin.
  • local mode:
    npm installs packages into the current project directory, which defaults to the current working directory. Packages are installed to ./node_modules, and bins are installed to ./node_modules/.bin.

Local mode is the default. Use --global or -g on any command to operate in global mode instead.

意思很简单,我就不翻译了,点击folders(1)链接,查看folders文档,有以下内容:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值