Azure DevOps Server 的连接源(Artifacts):七、NPM 基础知识

1. 镜像服务器

在继续后面使用Azure DevOps Server使用npm连接源之前,需要首先熟悉npm的一些基本操作知识。

  • 得到原本的镜像地址
npm get registry
  • 将镜像服务器修改为淘宝
npm config set registry http://registry.npm.taobao.org/
  • 将镜像服务器修改为官方地址
npm config set registry http://registry.npmjs.org/

注意,连接国内淘宝服务器速度较快

  • 查询本地全局包的地址
npm config get prefix

C:\Users\zhang\AppData\Roaming\npm

2. 初始化项目

npm init

cmd中进入到该目录下。
开始初始化项目:npm init
这样初始化之后,项目目录下会自动生成一个package.json文件。
注意的是,npm init命令后,npm会询问你一系列问题,当你填入答案后才会正式结束初始化,如果不太想自定义一些关于项目的描述,可以不敲npm init,而是直接敲npm init --yes

命令行中将会提示 package.json 字段中需要你输入的值。
名称(name) 和 版本(version) 这两个字段是必填的。
你还需要输入 入口文件字段(main) 字段,默认值是 index.js。

  • Package.json 属性说明
    • name - 包名。
    • version - 包的版本号。
    • description - 包的描述。
    • homepage - 包的官网 url 。
    • author - 包的作者姓名。
    • contributors - 包的其他贡献者姓名。
    • dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。
    • repository - 包代码存放的地方的类型,可以是 git 或 svn,git 可在 Github 上。
    • main - main 字段指定了程序的主入口文件,require('moduleName') 就会加载这个文件。这个字段的默认值是模块根目录下面的 index.js。
    • keywords - 关键字

3. npm install 下载安装和卸载依赖包

当前项目

  • 示例,在当前目录中安装和卸载jquery(本地)
npm install jquery --save

jquery下载到了当前目录的node_modules目录下
同时,在项目文件package.json中添加了对应的依赖

  "dependencies": {
    "jquery": "^3.6.0"
  }

在当前项目目录中,运行下面的命令,将从当前目录node_modules删除依赖包,并在项目文件package.json中移除对应的依赖代码

npm uninstall jquery
  • 示例,查看当前项目的依赖包
D:\temp\npm-sample>npm ls
npm-sample@1.0.0 D:\temp\npm-sample
+-- express@4.17.3
| +-- accepts@1.3.8
| | +-- mime-types@2.1.34
| | | `-- mime-db@1.51.0
| | `-- negotiator@0.6.3
| +-- array-flatten@1.1.1
| +-- body-parser@1.19.2
| | +-- bytes@3.1.2
| | +-- content-type@1.0.4 deduped
| | +-- debug@2.6.9 deduped
| | +-- depd@1.1.2 deduped
| | +-- http-errors@1.8.1
| | | +-- depd@1.1.2 deduped
| | | +-- inherits@2.0.4
| | | +-- setprototypeof@1.2.0 deduped
| | | +-- statuses@1.5.0 deduped
| | | `-- toidentifier@1.0.1
| | +-- iconv-lite@0.4.24
| | | `-- safer-buffer@2.1.2
| | +-- on-finished@2.3.0 deduped
| | +-- qs@6.9.7 deduped
| | +-- raw-body@2.4.3
| | | +-- bytes@3.1.2 deduped
| | | +-- http-errors@1.8.1 deduped
| | | +-- iconv-lite@0.4.24 deduped
| | | `-- unpipe@1.0.0 deduped
| | `-- type-is@1.6.18 deduped
| +-- content-disposition@0.5.4
| | `-- safe-buffer@5.2.1 deduped
| +-- content-type@1.0.4
| +-- cookie@0.4.2
| +-- cookie-signature@1.0.6
| +-- debug@2.6.9
| | `-- ms@2.0.0
| +-- depd@1.1.2
| +-- encodeurl@1.0.2
| +-- escape-html@1.0.3
| +-- etag@1.8.1
| +-- finalhandler@1.1.2
| | +-- debug@2.6.9 deduped
| | +-- encodeurl@1.0.2 deduped
| | +-- escape-html@1.0.3 deduped
| | +-- on-finished@2.3.0 deduped
| | +-- parseurl@1.3.3 deduped
| | +-- statuses@1.5.0 deduped
| | `-- unpipe@1.0.0
| +-- fresh@0.5.2
| +-- merge-descriptors@1.0.1
| +-- methods@1.1.2
| +-- on-finished@2.3.0
| | `-- ee-first@1.1.1
| +-- parseurl@1.3.3
| +-- path-to-regexp@0.1.7
| +-- proxy-addr@2.0.7
| | +-- forwarded@0.2.0
| | `-- ipaddr.js@1.9.1
| +-- qs@6.9.7
| +-- range-parser@1.2.1
| +-- safe-buffer@5.2.1
| +-- send@0.17.2
| | +-- debug@2.6.9 deduped
| | +-- depd@1.1.2 deduped
| | +-- destroy@1.0.4
| | +-- encodeurl@1.0.2 deduped
| | +-- escape-html@1.0.3 deduped
| | +-- etag@1.8.1 deduped
| | +-- fresh@0.5.2 deduped
| | +-- http-errors@1.8.1 deduped
| | +-- mime@1.6.0
| | +-- ms@2.1.3
| | +-- on-finished@2.3.0 deduped
| | +-- range-parser@1.2.1 deduped
| | `-- statuses@1.5.0 deduped
| +-- serve-static@1.14.2
| | +-- encodeurl@1.0.2 deduped
| | +-- escape-html@1.0.3 deduped
| | +-- parseurl@1.3.3 deduped
| | `-- send@0.17.2 deduped
| +-- setprototypeof@1.2.0
| +-- statuses@1.5.0
| +-- type-is@1.6.18
| | +-- media-typer@0.3.0
| | `-- mime-types@2.1.34 deduped
| +-- utils-merge@1.0.1
| `-- vary@1.1.2
`-- jquery@3.6.0

其他对依赖包的目录,参考如下:

  • 查看全局目录中安装的依赖包:npm ls -g
  • 查看特定的依赖包:npm ls <package name>
  • 更新依赖包:npm update <package name>

全局目录

  • 示例,将express安装到全局目录中(全局)、卸载
    下面的命令将依赖包安装到了全局目录中:C:\Users\zhang\AppData\Roaming\npm\node_modules
npm install express -g

下面的命令将从全局目录中删除依赖包express

npm uninstall express –g
熟悉了上面的这些基本操作以后,我们就可以继续后面的内容了。

4. 相关文章

如果需要,你还可以从微软Azure DevOps Server 的在线文档,查询更多的权威资料,也欢迎通过下面的联系方式与我沟通,相互学习,相互提高!


https://www.cnblogs.com/danzhang
Azure DevOps MVP 张洪君
在这里插入图片描述


posted on 2022-03-13 11:14  danzhang  阅读(161)  评论(0编辑  收藏  举报

导航