GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.runoob.com — NPM 使用介绍 | 菜鸟教程
Node.js 教程
Node.js 教程
Node.js 概念
Node.js 安装配置
Node.js VS Code
Node.js NVM
Node.js 创建第一个应用
Node.js 工作机制
NPM 使用介绍
PNPM 使用介绍
Node.js REPL
Node.js 异步编程
Node.js 回调函数
Node.js Promise
Node.js async/await
Node.js 事件循环
Node.js EventEmitter
Node.js Buffer
Node.js Stream
Node.js 模块系统
Node.js 模块导入
Node.js 函数
Node.js 路由
Node.js 全局对象
Node.js 常用工具
Node.js 文件系统
Node.js 内置模块
Node.js 测验
Node.js 应用Node.js 构建简单应用 Node.js GET/POST请求 Node.js util 模块 Node.js Web 模块 Node.js Express 框架 Node.js RESTful API Node.js 多进程 Node.js JXcore 打包 Node.js MySQL Node.js MongoDB npx 入门教程NPM 使用介绍NPM(Node Package Manager)是一个 JavaScript 包管理工具,也是 Node.js 的默认包管理器。 NPM 允许开发者轻松地下载、安装、共享、管理项目的依赖库和工具。 NPM 是 Node.js 自带的包管理工具,因此,通常你只需安装 Node.js,NPM 就会自动安装在系统中。
主要功能:
由于新版的 Node.js 已经集成了 NPM,所以我们可以直接通过输入 npm -v 来测试是否成功安装,出现版本提示表示安装成功: $ npm -v 2.3.0 如果你安装的是旧版本的 npm,可以很容易得通过 npm 命令来升级,命令如下: $ sudo npm install npm -g /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js [email protected] /usr/local/lib/node_modules/npm 如果是 Window 系统使用以下命令即可: npm install npm -g 使用 npm 命令安装模块npm 安装 Node.js 模块语法格式如下: $ npm install <Module Name> 以下实例,我们使用 npm 命令安装常用的 Node.js web框架模块 express: $ npm install express 安装好之后,express 包就放在了工程目录下的 node_modules 目录中,因此在代码中只需要通过 require('express') 的方式就好,无需指定第三方包路径。 var express = require('express');
全局安装与本地安装npm 的包安装分为本地安装(local)、全局安装(global)两种,从敲的命令行来看,差别只是有没有 -g 参数。 本地安装:将包安装到 node_modules 目录,并将信息保存到 package.json 的 dependencies 中。 npm install express # 本地安装 全局安装:用于安装命令行工具或需要在多个项目中使用的包。 npm install express -g # 全局安装 如果出现以下错误: npm err! Error: connect ECONNREFUSED 127.0.0.1:8087 解决办法为: $ npm config set proxy null 本地安装
全局安装
如果你希望具备两者功能,则需要在两个地方安装它或使用 npm link。 接下来我们使用全局方式安装 express $ npm install express -g 安装过程输出如下内容,第一行输出了模块的版本号及安装位置。 [email protected] node_modules/express ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ([email protected]) ├── [email protected] ([email protected]) ├── [email protected] ([email protected], [email protected]) ├── [email protected] ([email protected]) ├── [email protected] ([email protected], [email protected]) ├── [email protected] ([email protected], [email protected]) └── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected]) 查看安装信息你可以使用以下命令来查看所有全局安装的模块: $ npm list -g ├─┬ [email protected] │ ├── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├─┬ [email protected] │ │ └── [email protected] │ ├─┬ [email protected] │ │ └── [email protected] …… 如果要查看某个模块的版本号,可以使用命令如下: $ npm list grunt projectName@projectVersion /path/to/project/folder └── [email protected] 卸载模块我们可以使用以下命令来卸载 Node.js 模块。 $ npm uninstall express
卸载后,你可以到 /node_modules/ 目录下查看包是否还存在,或者使用以下命令查看: $ npm ls 更新模块我们可以使用以下命令更新模块: $ npm update express 搜索模块使用以下来搜索模块: $ npm search express 创建模块创建模块,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 adduser Username: mcmohd Password: Email: (this IS public) [email protected] 接下来我们就用以下命令来发布模块: $ npm publish 如果你以上的步骤都操作正确,你就可以跟其他模块一样使用 npm 来安装。 版本号使用 NPM 下载和发布代码时都会接触到版本号,NPM 使用语义版本号来管理代码,这里简单介绍一下。 版本号遵循语义化版本控制(SemVer),格式为 MAJOR.MINOR.PATCH,并可附加额外标记。
额外标记
安装示例
NPM 常用命令NPM 提供了很多命令,可以使用 npm help 可查看所有命令。
除了本章介绍的部分外,NPM 还提供了很多功能,package.json 里也有很多其它有用的字段。 除了可以在 https://docs.npmjs.com/ 查看官方文档外,这里再介绍一些NPM常用命令。 使用淘宝 NPM 镜像由于国内直接使用 npm 的官方镜像是非常慢的,为了解决这个问题,我们可以使用淘宝提供的镜像(cnpm 或通过配置 NPM)来加速包的下载和安装。 淘宝 NPM 镜像是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同步。 你可以使用淘宝定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm: $ npm install -g cnpm --registry=https://registry.npmmirror.com 接下来我们就可以使用 cnpm 来替代 npm 进行包的安装和管理: $ cnpm install [name] package.json 的说明与使用package.json 是 Node.js 项目中的一个核心文件,包含了项目的元数据、依赖、脚本等信息。 package.json 文件用于描述项目的元数据和依赖关系,它通常位于项目的根目录中,并且是项目的配置文件。 package.json 文件是一个 JSON 格式的文件,包含以下基本字段:
使用方法:
一个典型的 package.json 文件结构如下: 实例
{ "name": "my-project", "version": "1.0.0", "description": "A simple Node.js project", "main": "app.js", "scripts": { "start": "node app.js", "test": "echo \"Error: no test specified\" && exit 1" }, "dependencies": { "express": "^4.17.1" }, "devDependencies": { "nodemon": "^2.0.20" }, "keywords": ["node", "npm", "example"], "author": "Your Name", "license": "MIT" } 字段说明:
使用 package.json 的好处
依赖管理dependencies:存储项目运行所需的依赖。 "dependencies": {
"express": "^4.17.1"
}
安装依赖时使用: npm install express devDependencies:存储项目开发期间需要的依赖。 "devDependencies": {
"nodemon": "^2.0.20"
}安装依赖时使用: npm install nodemon --save-dev scripts 字段scripts 字段用于定义可通过 npm run <script> 执行的命令。常见的脚本包括: "scripts": {
"start": "node app.js",
"test": "jest",
"build": "webpack --mode production",
"dev": "nodemon app.js"
}运行脚本示例: npm run dev # 执行 "nodemon app.js" npm start # 等同于 "node app.js",可以直接用 npm start 运行 版本号中的符号说明
常用命令初始化 package.json 文件: npm init 使用 npm init -y 可以快速生成默认的 package.json 文件。 查看项目依赖: npm list --depth=0 更新依赖: npm update package-name 注意事项
更多信息可以查阅:https://npmmirror.com/。
AI 思考中...
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |