Nodejs的npm包管理器快速入门

简介: 介绍Node.js的npm包管理器的快速入门,包括包的概念、结构、描述文件、npm常用命令如安装、搜索、移除软件包,以及如何处理常见的npm工具部署错误。

作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.包(package)概述

1>.package概述

  CommonJS的包规范允许我们将一组相关的模块组合到一起,形成一组完整的工具。我们可以将package理解为增强的模块。

  CommonJS的包规范由包结构和包描述文件两个部分组成。
    包结构:
      用于组织包中的各种文件。
    包描述文件:
      描述包的相关信息,以供外部读取分析。

2>.包结构概述

  包实际上就是一个压缩文件,解压以后还原为目录。  

  符合规范的目录,应该包含如下文件:
    package.json:  
      包描述的文件
    bin:  
      存放可执行二进制文件的目录
    lib:  
      存放依赖的js代码的目录
    doc:  
      存放包的文档信息的目录
    test:  
      存放单元测试的目录

3>.包描述文件

  包描述文件用于表达非代码相关的信息,它是一个JSON格式的文件,即"package.json",其位于包的根目录下,是包的重要组成部分。

  "package.json"中的包含以下常用的字段:
    name:  
      包的标识,即包名称。  
    description:  
      描述包的用途。    
    version:  
      包的版本信息。  
    keywords:  
      为包定义的关键字,程序员可以基于关键字来搜索与之匹配的包名。
    maintainers:  
      主要的贡献者信息,通常会将开发者的姓名和邮箱用多个object对象封装到一个数组中。  
    contributors:  
      开发的贡献者成员,非主力开发人员,但这些人的确是有参与该项目的哟~  
    bugs:  
      会提供bugs的提交地址,这样开发者可以基于你所提交的bugs进行及时的修复。
    licenses:  
      许可证授权信息,例如"MIT"。  
    repository:  
      存储库相关的信息。  
    dependencies:  
      当前包所依赖的插件信息。
    homepage:  
      包的主页信息。通常是某个包提供商的官网信息。  
    os:  
      操作系统相关的信息。  
    cpu:  
      CPU相关的信息  
    engine:  
      指向引擎的相关信息。  
    builtin:  
      构建工具信息。
    directories:  
      目录结构信息。  
    implements:  
      工具信息。  
    scripts:  
      脚本命令的相关信息。  
    author:
      作者信息  
    bin:  
      可执行程序信息。  
    main:  
      定义主文件信息,该文件在包中是真实存在的。例如: "main": "./index"  
    devDependencies:  
      开发环境依赖的信息,通常记录了开发环境所依赖的软件版本信息。在实际生产环境中,开发环境可能并不被需要!只需提供运行环境即可~  

  温馨提示:  
    不建议在json文件中写注释信息!可能导致错误出现!

二.包管理工具(英文全称: Node Package Manager,简称"NPM")快速入门

1>.NPM概述

  CommonJS包规范是理论,NPM(Node Package Manager)是其中一种实践。对于NodeJS而言,NPM帮助其完成了第三方模块的发布、安装和依赖等。  

  程序员可以借助NPM,Node与第三方模块之间形成了很好的一个生态系统,然而NPM就是强有力的推手,甚至有些程序员安装NodeJS环境并不是为了编写NodeJS代码,而是为了使用NMP工具,甚至有的小伙伴觉得NPM工具的价值已经超过NodeJS本身了。  

  综上所述,我们无需安装NPM工具,因为NodeJS内部已经集成了NPM工具,接下来我们来介绍一下nmp包管理器的常用命令。

2>.查看NodeJS版本

C:\Users\yinzhengjie>npm -v
6.14.10

C:\Users\yinzhengjie>

C:\Users\yinzhengjie>npm -v

3>.查看NodeJS内置模块组件的版本

C:\Users\yinzhengjie>npm version
{
  npm: '6.14.10',
  ares: '1.16.1',
  brotli: '1.0.9',
  cldr: '37.0',
  icu: '67.1',
  llhttp: '2.1.3',
  modules: '83',
  napi: '7',
  nghttp2: '1.41.0',
  node: '14.15.4',
  openssl: '1.1.1i',
  tz: '2020a',
  unicode: '13.0',
  uv: '1.40.0',
  v8: '8.4.371.19-node.17',
  zlib: '1.2.11'
}

C:\Users\yinzhengjie>

C:\Users\yinzhengjie>npm version

4>.搜索模块包

C:\Users\yinzhengjie\demo>npm search math
NAME                      | DESCRIPTION          | AUTHOR          | DATE       | VERSION  | KEYWORDS
math                      | Mathematical…        | =kzh            | 2011-09-19 | 0.0.3    |
Math                      | This package name…   | =npm            | 2016-06-14 | 0.0.1-s… |
math-expression-evaluator | A flexible math…     | =bugwheels94    | 2020-12-20 | 1.3.7    | math expression evaluator p
simple-statistics         | Simple Statistics    | =tmcw           | 2020-12-14 | 7.4.0    | descriptive linear math pro
mathjs                    | Math.js is an…       | =josdejong      | 2021-01-16 | 9.0.0    | math mathematics functions
katex                     | Fast math…           | =edemaine…      | 2020-07-12 | 0.12.0   |
node-int64                | Support for…         | =broofa…        | 2015-04-04 | 0.4.0    | math integer int64
is-number                 | Returns true if a…   | =doowb…         | 2018-07-04 | 7.0.0    | cast check coerce coercion
dompurify                 | DOMPurify is a…      | =cure53         | 2020-12-18 | 2.2.6    | dom xss html svg mathml sec
long                      | A Long class for…    | =dcode          | 2018-02-03 | 4.0.0    | math
nerdamer                  | javascript…          | =brosnanyuen…   | 2020-12-16 | 1.1.6    | math matrix complex number
jsbn                      | The jsbn library is… | =andyperlitch   | 2017-02-13 | 1.1.0    | biginteger bignumber big in
big-integer               | An arbitrary length… | =peterolson     | 2019-11-11 | 1.6.48   | math big bignum bigint bigi
hull.js                   | JavaScript library…  | =andriiheonia   | 2020-10-24 | 1.0.1    | geometry concave-hull math
math.log1p                | An…                  | =ljharb         | 2020-12-25 | 1.0.1    | Math.log1p math log1p log s
math-random               | math-random is an…   | =michaelrhodes  | 2020-08-07 | 2.0.1    |
math.js                   | A simple math…       | =toubou         | 2020-02-04 | 1.1.46   | math mathematics functions
mathml                    | MathML allows to…    | =physikerwelt   | 2020-11-12 | 1.1.12   | mathml math xml mathml3
red-agate-math            | red-agate math…      | =shellyln       | 2020-10-24 | 0.5.0    | finite field galois field f
@wiris/mathtype-html-inte | Allows to integrate… | =wiris          | 2020-12-09 | 1.4.5    | chem chemistry chemtype edi
gration-devkit            |                      |                 |            |          |

C:\Users\yinzhengjie\demo>

C:\Users\yinzhengjie\demo>npm search math

5>.自定义安装的模块存储路径(若不指定默认会安装到npm应用程序的相关目录中)

C:\Users\yinzhengjie\demo>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 6C28-75D5

 C:\Users\yinzhengjie\demo 的目录

2021/01/19  23:52    <DIR>          .
2021/01/19  23:52    <DIR>          ..
               0 个文件              0 字节
               2 个目录  9,305,300,992 可用字节

C:\Users\yinzhengjie\demo>
C:\Users\yinzhengjie\demo>npm init  # 只需该命令后,我们可以直接一路回车(需要注意的是包名不支持驼峰命名法,但可以使用下划线连接各个单词哟~),初始化成功后,会生成"package.json"文件
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 init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (demo)
version: (1.0.0)
description: My local warehouse
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to C:\Users\yinzhengjie\demo\package.json:

{
  "name": "demo",
  "version": "1.0.0",
  "description": "My local warehouse",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes)

C:\Users\yinzhengjie\demo>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 6C28-75D5

 C:\Users\yinzhengjie\demo 的目录

2021/01/19  23:51    <DIR>          .
2021/01/19  23:51    <DIR>          ..
2021/01/19  23:51               218 package.json
               1 个文件            218 字节
               2 个目录  9,306,783,744 可用字节

C:\Users\yinzhengjie\demo>

C:\Users\yinzhengjie\demo>npm init # 只需该命令后,我们可以直接一路回车(需要注意的是包名不支持驼峰命名法,但可以使用下划线连接各个单词哟~),初始化成功后,会生成"package.json"文件

C:\Users\yinzhengjie\demo>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 6C28-75D5

 C:\Users\yinzhengjie\demo 的目录

2021/01/19  23:51    <DIR>          .
2021/01/19  23:51    <DIR>          ..
2021/01/19  23:51               218 package.json
               1 个文件            218 字节
               2 个目录  9,308,332,032 可用字节

C:\Users\yinzhengjie\demo>
C:\Users\yinzhengjie\demo>
C:\Users\yinzhengjie\demo>type package.json
{
  "name": "demo",
  "version": "1.0.0",
  "description": "My local warehouse",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

C:\Users\yinzhengjie\demo>

C:\Users\yinzhengjie\demo>type package.json

6>.由于我们上面使用了"npm init"配置了本地软件源仓库,因此本次安装会存储在上面指定的目录中哟~

C:\Users\yinzhengjie\demo>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 6C28-75D5

 C:\Users\yinzhengjie\demo 的目录

2021/01/19  23:51    <DIR>          .
2021/01/19  23:51    <DIR>          ..
2021/01/19  23:51               218 package.json
               1 个文件            218 字节
               2 个目录  9,306,763,264 可用字节

C:\Users\yinzhengjie\demo>
C:\Users\yinzhengjie\demo>npm install math  # 安装math软件包
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN demo@1.0.0 No repository field.

+ math@0.0.3
added 1 package from 1 contributor and audited 1 package in 2.3s
found 0 vulnerabilities


C:\Users\yinzhengjie\demo>
C:\Users\yinzhengjie\demo>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 6C28-75D5

 C:\Users\yinzhengjie\demo 的目录

2021/01/19  23:57    <DIR>          .
2021/01/19  23:57    <DIR>          ..
2021/01/19  23:57    <DIR>          node_modules
2021/01/19  23:57               283 package-lock.json
2021/01/19  23:57               263 package.json
               2 个文件            546 字节
               3 个目录  9,306,484,736 可用字节

C:\Users\yinzhengjie\demo>
C:\Users\yinzhengjie\demo>dir node_modules
 驱动器 C 中的卷没有标签。
 卷的序列号是 6C28-75D5

 C:\Users\yinzhengjie\demo\node_modules 的目录

2021/01/19  23:57    <DIR>          .
2021/01/19  23:57    <DIR>          ..
2021/01/19  23:57    <DIR>          math
               0 个文件              0 字节
               3 个目录  9,306,484,736 可用字节

C:\Users\yinzhengjie\demo>
C:\Users\yinzhengjie\demo>dir node_modulesmath
 驱动器 C 中的卷没有标签。
 卷的序列号是 6C28-75D5

 C:\Users\yinzhengjie\demo\node_modules\math 的目录

2021/01/19  23:57    <DIR>          .
2021/01/19  23:57    <DIR>          ..
2011/09/19  09:38                52 AUTHORS
2011/09/19  09:38               671 Cakefile
2011/09/19  09:38             2,059 math.coffee
2011/09/19  09:38             2,902 math.js
2021/01/19  23:57             1,301 package.json
               5 个文件          6,985 字节
               2 个目录  9,306,320,896 可用字节

C:\Users\yinzhengjie\demo>
C:\Users\yinzhengjie\demo>

C:\Users\yinzhengjie\demo>npm install math # 在当期目录安装math软件包

  温馨提示:
    (1)install也可以简写为"i"选项,例如: "npm i math"  
    (2)安装软件源时也可以声明在全局(global)模式安装包,通常情况下,需要全局安装的包基本上都是一些工具。一般只会在计算机中使用,并不会在项目中使用,比如一些打包工具,通常情况下不经常使用"-g"选项,如: "npm install webpack -g"  
    (3)安装包并添加到依赖(注意对比观察package.json文件中的"dependencies"选项,应该是有类似于"math": "0.0.3"的信息)中,这种安装方式在开发中经常使用,如: "npm i math --save"  
    (4)下载当前项目所依赖的包,我们可以直接只需命令"npm install",他会根据package.json文件中的"dependencies"选项下载对应软件的依赖包,下载成功后我们才能正常运行别人开发的项目哟~

7>.移除软件包

C:\Users\yinzhengjie\demo>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 6C28-75D5

 C:\Users\yinzhengjie\demo 的目录

2021/01/19  23:57    <DIR>          .
2021/01/19  23:57    <DIR>          ..
2021/01/19  23:57    <DIR>          node_modules
2021/01/19  23:57               283 package-lock.json
2021/01/19  23:57               263 package.json
               2 个文件            546 字节
               3 个目录  9,304,813,568 可用字节

C:\Users\yinzhengjie\demo>
C:\Users\yinzhengjie\demo>dir node_modules
 驱动器 C 中的卷没有标签。
 卷的序列号是 6C28-75D5

 C:\Users\yinzhengjie\demo\node_modules 的目录

2021/01/19  23:57    <DIR>          .
2021/01/19  23:57    <DIR>          ..
2021/01/19  23:57    <DIR>          math
               0 个文件              0 字节
               3 个目录  9,304,813,568 可用字节

C:\Users\yinzhengjie\demo>
C:\Users\yinzhengjie\demo>npm remove math
npm WARN demo@1.0.0 No repository field.

removed 1 package in 10.151s

C:\Users\yinzhengjie\demo>
C:\Users\yinzhengjie\demo>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 6C28-75D5

 C:\Users\yinzhengjie\demo 的目录

2021/01/20  00:14    <DIR>          .
2021/01/20  00:14    <DIR>          ..
2021/01/20  00:14                67 package-lock.json
2021/01/20  00:14               240 package.json
               2 个文件            307 字节
               2 个目录  9,304,707,072 可用字节

C:\Users\yinzhengjie\demo>

C:\Users\yinzhengjie\demo>npm remove math

  温馨提示:  
    (1)remove也可以简写为"r"选项,例如: npm r math

8>.查看NodeJS的帮助信息

C:\Users\yinzhengjie>npm help

Usage: npm <command>

where <command> is one of:
    access, adduser, audit, bin, bugs, c, cache, ci, cit,
    clean-install, clean-install-test, completion, config,
    create, ddp, dedupe, deprecate, dist-tag, docs, doctor,
    edit, explore, fund, get, help, help-search, hook, i, init,
    install, install-ci-test, install-test, it, link, list, ln,
    login, logout, ls, org, outdated, owner, pack, ping, prefix,
    profile, prune, publish, rb, rebuild, repo, restart, root,
    run, run-script, s, se, search, set, shrinkwrap, star,
    stars, start, stop, t, team, test, token, tst, un,
    uninstall, unpublish, unstar, up, update, v, version, view,
    whoami

npm <command> -h  quick help on <command>
npm -l            display full usage info
npm help <term>   search for help on <term>
npm help npm      involved overview

Specify configs in the ini-formatted file:
    C:Usersyinzhengjie.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config

npm@6.14.10 D:\Language\node\jsnode_modules\npm

C:\Users\yinzhengjie>

C:\Users\yinzhengjie>npm help

三.部署NMP工具常见的报错信息处理

1>.npm ERR! No search sources available

  如上图所示,说是没有可用的搜索资源,这个时候我们先将软件源还原到NodeJS的软件源,就可以搜索到啦!  

  具体命令如下所示:  
    C:\Users\yinzhengjie>npm config set registry https://registry.npmjs.org

目录
相关文章
|
13天前
|
JavaScript
Nodejs的cnpm包管理器快速入门
介绍Node.js的cnpm包管理器,包括cnpm的实现原理、如何安装cnpm、使用cnpm安装软件包,以及Node.js搜索包的流程。
18 2
Nodejs的cnpm包管理器快速入门
2071 verbose node v16.6.0 2072 verbose npm v7.19.1或者 no such file or directory, lstat ‘D:\wor
该博客文章提供了解决在使用npm版本7.19.1时出现的"no such file or directory"错误的具体方法,建议通过降级npm到6.14.8版本来解决问题,并确认了该方法可以成功安装node_modules。
2071 verbose node v16.6.0 2072 verbose npm v7.19.1或者 no such file or directory, lstat ‘D:\wor
|
1月前
|
JavaScript
【Deepin 20系统】Jupyter notebook解决ValueError: Please install Node.js and npm before continuing installa
文章讨论了在Deepin 20系统上安装Jupyter Notebook的debug插件时出现的"ValueError: Please install Node.js and npm before continuing installation"错误,并提供了使用conda安装Node.js的解决方法。
67 1
|
1月前
Mac卸载 Node npm,升级 Node
Mac卸载 Node npm,升级 Node
43 0
|
1月前
NPM——Electron failed to install correctly, please delete node_modules/electron and try
NPM——Electron failed to install correctly, please delete node_modules/electron and try
81 0
|
1月前
|
开发工具 git
IDEA——npm install 没有生成node_modules目录
IDEA——npm install 没有生成node_modules目录
114 0
|
1月前
|
缓存
Node——npm ERR! cb() never called!
Node——npm ERR! cb() never called!
38 0
|
4月前
|
JavaScript
npm install没问题,但npm run dev的时候报Node Sass version 6.0.1 is incompatible with ^4.0.0 ^5.0.0
npm install没问题,但npm run dev的时候报Node Sass version 6.0.1 is incompatible with ^4.0.0 ^5.0.0
46 0
|
3月前
|
前端开发
windows10 安装node npm 等前端环境 并配置国内源
windows10 安装node npm 等前端环境 并配置国内源
224 3
|
4月前
|
资源调度 JavaScript Linux
nvm, node.js, npm, yarn 安装配置
nvm, node.js, npm, yarn 安装配置
223 1

热门文章

最新文章