微网站 报价,企业网站信息管理系统,建设网站代理商,小程序api是什么day2#xff1a;Node.js 环境准备 文章目录 day2#xff1a;Node.js 环境准备安装 Node.js 和 npm验证 Node.js 和 npm使用淘宝 NPM 镜像npm 包管理器的基本使用**NPM 常用命令**小结 准备一台linux服务器 [rootnode3 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.…day2Node.js 环境准备 文章目录 day2Node.js 环境准备安装 Node.js 和 npm验证 Node.js 和 npm使用淘宝 NPM 镜像npm 包管理器的基本使用**NPM 常用命令**小结 准备一台linux服务器 [rootnode3 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)安装 Node.js 和 npm
打开终端并使用root权限登录系统。执行以下命令安装Node.js相关的依赖软件包
yum install -y gcc gcc-c openssl-devel执行以下命令从Node.js官方网站下载适用于CentOS的Node.js软件包
curl -sL https://rpm.nodesource.com/setup_16.x | bash安装Node.js软件包 yum install -y nodejs验证 Node.js 和 npm
[rootnode3 ~]# node -v
v16.18.1[rootnode3 ~]# npm -v
8.19.2使用淘宝 NPM 镜像
由于国内直接使用 npm 的官方镜像是非常慢的这里推荐使用淘宝 NPM 镜像。
淘宝 NPM 镜像是一个完整 npmjs.org 镜像你可以用此代替官方版本(只读)同步频率目前为 10分钟 一次以保证尽量与官方服务同步。
你可以使用淘宝定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:
$ npm install -g cnpm --registryhttps://registry.npmmirror.com这样就可以使用 cnpm 命令来安装模块了
$ cnpm install [name]npm 包管理器的基本使用
NPM是随同NodeJS一起安装的包管理工具能解决NodeJS代码部署上的很多问题常见的使用场景有以下几种
允许用户从NPM服务器下载别人编写的第三方包到本地使用。允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用。允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用。
NPM安装模块
以下实例我们使用 npm 命令安装常用的 Node.js web框架模块 express:
$ mkdir nodejs
$ cd nodejs
$ npm install express[rootnode3 nodejs]# ll
total 108
drwxr-xr-x 69 root root 4096 Oct 12 14:04 node_modules全局安装与本地安装
npm 的包安装分为本地安装local、全局安装global两种从敲的命令行来看差别只是有没有-g而已比如
npm install express # 本地安装
npm install express -g # 全局安装如果出现以下错误
npm err! Error: connect ECONNREFUSED 127.0.0.1:8087 解决办法为
$ npm config set proxy null本地安装 将安装包放在 ./node_modules 下运行 npm 命令时所在的目录如果没有 node_modules 目录会在当前执行 npm 命令的目录下生成 node_modules 目录。 可以通过 require() 来引入本地安装的包。
全局安装 将安装包放在 /usr/local 下或者你 node 的安装目录。 可以直接在命令行里使用。
查看安装信息
你可以使用以下命令来查看所有全局安装的模块
[rootnode3 nodejs]# npm list express -g
/usr/local/lib
└── (empty)
如果要查看某个模块的版本号可以使用命令如下
[rootnode3 nodejs]# npm list express
helloworld1.0.0 /root/nodejs
└── express4.18.2使用 package.json
package.json 位于模块的目录下用于定义包的属性。接下来让我们来看下 express 包的 package.json 文件位于 node_modules/express/package.json 内容
[rootnode3 nodejs]# cat package.json
{dependencies: {express: ^4.18.2,mysql: ^2.18.1},scripts: {start: node server.js},name: helloworld,version: 1.0.0,main: helloworld.js,keywords: [helloworld],author: xiaohaibing,license: ISC,description:
}
Package.json 属性说明
name - 包名。version - 包的版本号。description - 包的描述。homepage - 包的官网 url 。author - 包的作者姓名。contributors - 包的其他贡献者姓名。dependencies - 依赖包列表。如果依赖包没有安装npm 会自动将依赖包安装在 node_module 目录下。repository - 包代码存放的地方的类型可以是 git 或 svngit 可在 Github 上。main - main 字段指定了程序的主入口文件require(‘moduleName’) 就会加载这个文件。这个字段的默认值是模块根目录下面的 index.js。keywords - 关键字
NPM卸载模块
我们可以使用以下命令来卸载 Node.js 模块。
$ npm uninstall express卸载后你可以到 /node_modules/ 目录下查看包是否还存在或者使用以下命令查看
$ npm lsNPM更新模块
我们可以使用以下命令更新模块
$ npm update expressNPM搜索模块
使用以下来搜索模块
$ npm search expressNPM创建模块
创建模块package.json 文件是必不可少的。我们可以使用 NPM 生成 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 sensible 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: (node_modules) runoob # 模块名
version: (1.0.0)
description: Node.js 测试模块(www.runoob.com) # 描述
entry point: (index.js)
test command: make test
git repository: https://github.com/runoob/runoob.git # Github 地址
keywords:
author:
license: (ISC)
About to write to ……/node_modules/package.json: # 生成地址{name: runoob,version: 1.0.0,description: Node.js 测试模块(www.runoob.com),……
}Is this ok? (yes) yes以上的信息你需要根据你自己的情况输入。在最后输入 “yes” 后会生成 package.json 文件。
NPM发布模块
接下来我们可以使用以下命令在 npm 资源库中注册用户使用邮箱注册
$ npm adduser
Username: mcmohd
Password:
Email: (this IS public) mcmohdgmail.com接下来我们就用以下命令来发布模块
$ npm publishNPM 常用命令
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 可以撤销发布自己发布过的某个版本代码。
小结
如果你遇到了使用 npm 安 装node_modules 总是提示报错报错: npm resource busy or locked…。
可以先删除以前安装的 node_modules :
npm cache clean
npm install