npm 常用指令介绍

2023-11-18

npm usage

what is npm?

实现js代码的复用,分享和管理

npm consists of three distinct components:

1. the website

通过 www.npmjs.com 网址查找需要的 包

2. the Command Line Interface (CLI)
通过命令行界面 进行包的安装…

3. the registry

我的理解就是一个 js 模块的登记处,这里就是一个 js 模块的公共库。我们可以从这里下载自己需要的模块,同时也可以分享js 模块到这里,供别人使用

Use the website to discover packages, set up profiles, and manage other aspects of your npm experience. For example, you can set up Orgs (organizations) to manage access to public or private packages.

The CLI runs from a terminal. This is how most developers interact with npm.

The registry is a large public database of JavaScript software and the meta-information surrounding it.

npm 有专门针对 个人,组织,公司的账户,需要付费,可以协助多人开发工作

npm command line interface

一、 npm –help && npm install

C:\Users\by>npm --help

Usage: npm <command>

where <command> is one of:
    access, adduser, bin, bugs, c, cache, ci, completion,
    config, ddp, dedupe, deprecate, dist-tag, docs, doctor,
    edit, explore, get, help, help-search, i, init, install,
    install-test, it, link, list, ln, login, logout, ls,
    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:\Users\by\.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config

npm@5.8.0 C:\Users\by\AppData\Roaming\npm\node_modules\npm

where <command> is one of: 这里告诉我们 npm 有哪些执行可以使用的,感觉好多,后面介绍几个常用指令

npm <command> -h: quick help on ,在 cmd interface 查看相关命令的简洁使用方法

C:\Users\by>npm install -h

npm install (with no args, in package dir)
npm install [<@scope>/]<pkg>
npm install [<@scope>/]<pkg>@<tag>
npm install [<@scope>/]<pkg>@<version>
npm install [<@scope>/]<pkg>@<version range>
npm install <folder>
npm install <tarball file>
npm install <tarball url>
npm install <git:// url>
npm install <github username>/<github project>

aliases: i, isntall, add
common options: [--save-prod|--save-dev|--save-optional] [--save-exact] [--no-save]

<@scope> 表示域: –local(一般省略) 和 –global 之分

<pkg> 表示js包名: npm install jquery 中 moment 就是包名

@<version> 表示 包的版本号: npm install jquery@1.8.1

npm help <term>: 这个用来本地查看某个指令的详细用法,比如: npm help install 就是本地打开 file:///C:/Users/by/AppData/Roaming/npm/node_modules/npm/html/doc/cli/npm-config.html这个指令和 npm <command> -h 是成对的

npm help npm: 这个指令是本地打开 npm 的介绍文档file:///C:/Users/by/AppData/Roaming/npm/node_modules/npm/html/doc/cli/npm.html

aliases: i, isntall, add 表示 install 这个指令的别名,如:npm i jquery 等价于 npm install jquery

common options: 表示可选项, 比如:

  • npm install jquery --save
  • npm install jquery --save-dev

二、package.json 文件

创建一个 package.json 文件

指令: npm init [--yes]

执行 npm init 一路 enter 键 ,等价于 npm init --yes

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

官网案例


{
  "name": "my_package",
  "description": "",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/ashleygwilliams/my_package.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/ashleygwilliams/my_package/issues"
  },
  "homepage": "https://github.com/ashleygwilliams/my_package"
}
package.json常用 介绍

name: 表示当前目录的名字

version, main: 一般不用管

scripts: 可以通过 npm run + 指令名字 在node 平台 执行js 文件

{
  // ...
    "scripts": {
    "start": "node ./js/demo.js",
    "demo": "node ./js/demo.js"
  },
  // ...
}

因为start 是默认值 所以可以直接: npm start 来启动一个 node application

demo 指令需要这样调用: npm run demo,同样可以用来启动一个 node application

默认配置修改

npm config set init.author.email "wombat@npmjs.com"
npm config set init.author.name "ag_dubs"
npm config set init.license "MIT"

使用 命令 npm config list 都是可以查看到设置了哪些 默认项

三、npm install

本地安装
不保存到 package.json 文件中

npm install [--local] moment

  1. 后面 –local 全部省略,下载 moment.js 在 node_modules 中,可以直接在html 页面中引用
  2. 也可以使用 es6 import moment from 'moment',或者 commonJS require('moment')
保存到 package.json 文件中

npm install <packageName> [--save|--save-dev]

执行命令: npm install moment --savenpm install lodash --save-dev,对应 package.json 文件增加了两个属性成员


"dependencies": {
    "moment": "^2.22.0"
  },
  "devDependencies": {
    "lodash": "^4.17.5"
  }

dependecies: 表示生产环境依赖的模块

devDependencies: 表示开发环境依赖的模块

全局安装

一般作为工具使用的模块我们使用 全局安装

如vue的脚手架工具: npm install --global vue-cli

vue -V 可以查看是否安装成功

四、npm uninstall

要修改 package.json 文件 需要使用参数 --save

本地卸载(删除)模块

三个命令:

1.npm uninstall <pk name>
- 从 node_modules 中卸载(删除) 对应包名模块,不会修改package.json 文件 如 npm uninstall moment

2.npm uninstall <pk name> --save
- 从 node_modules 中卸载模块并且 删除 package.json 文件中生产环境依赖, 如npm uninstall moment --save

3.npm uninstall <pk name> --save-dev
- 从 node_modules 中卸载模块并且 删除 package.json 文件中 开发环境依赖,如: npm uninstall lodash --save-dev

最终,node_modules 文件夹空了(注意一定一定要打开文件夹查看,编辑器不准),然后 package.json 对应的改变:

{
  // ...
 "dependencies": {},
  "devDependencies": {}
  // ...
}
全局卸载模块

npm uninstall --global <pk name>

比如我们删除 vue-cli 工具: npm uninstall --global vue-cli

五、npm list

使用这条指令可以 展示本地或者全局的 已安装包的列表

本地 已安装包 列表

不一定在 json 文件中保存,node_modules中的模块

npm list --depth 0 一般深度为零,不然会展示所有的依赖模块树结构


PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list --depth 0
npm_usage_f_youtube@1.0.0 C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
+-- lite-server@2.3.0 extraneous
+-- lodash@4.17.5
`-- moment@2.22.0

npm list <pkg>: 查看本地某个模块是否存在


PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list jquery
npm_usage_f_youtube@1.0.0 C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
`-- (empty)
全局 已安装包 列表

npm list --global true --depth 0


PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list --global true --depth 0
C:\Users\MSI-PC\AppData\Roaming\npm
+-- browser-sync@2.18.8
+-- cnpm@5.2.0
+-- csslint@1.0.5
+-- eslint@4.18.2
+-- gulp-cli@1.2.2
+-- http-server@0.9.0
+-- i5ting_toc@1.1.4
+-- jshint@2.9.4
+-- less@2.7.2
+-- live-server@1.2.0
+-- nodemon@1.11.0
+-- npm@4.1.1
+-- protractor@5.1.1
+-- stylus@0.54.5
+-- vue-cli@2.9.3
+-- webpack@2.2.1
+-- xg-htmlhint@0.1.0
`-- yarn@0.22.0

查看某个模块是否存在: npm list --global true <pkg> --depth 0


PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list --global true webpack --depth 0
C:\Users\MSI-PC\AppData\Roaming\npm
`-- webpack@2.2.1

六、npm install package@version

版本构成

npm install pkg@version

version: 一般由3个数字构成
- 第一个数字 major version: 表示模块有非常巨大的功能特色变化,比如 angular2,angular4
- 第二个数字 minor version: 表示模块有些小的特色功能变化。
- 第三个数字 patch version: 表示模块 bug 补丁修复

几种常用的版本指令

npm install lodash --save

这个命令会安装对应的包的 lastest stable version

{
// ...
"dependencies": {
    "lodash": "^4.17.5"
  }
// ...    
}

npm install lodash@3.3.0 --save

这个命令会下载 版本为3.3.0 的lodash包


{
// ...
"dependencies": {
     "lodash": "^3.3.0"
  }
// ...    
}

npm install lodash@4.14 --save

这个命令会下载 4.14, latest && stable patch version


{
// ...
"dependencies": {
     "lodash": "^4.14.2"
  }
// ...    
}

npm install lodash@4 --save

这个命令会下载 lodash 4., latest && stable minor , patch version


{
// ...
"dependencies": {
     "lodash": "^4.17.5"
  }
// ...    
}

七、npm install from a package.json

主要讲述了怎么从 一个package.json 文件安装符合我们要求的包版本

npm 包在发布的时候是可以指定版本范围的

感觉这个是针对于项目的发布者

上尖括号语法
{
    // ...
     "dependencies": {
    "lodash": "^4.14.1"
  },
    // ...
}

上尖括号,在执行 npm install 后,会下载 latest version of minor && patch

PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm install
npm_usage_f_youtube@1.0.0 C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
`-- lodash@4.17.5
波浪线语法
{
    // ...
 "dependencies": {
    "lodash": "~4.14.1"
  }
    // ...
}

波浪线,在执行 npm install 后会下载 latest patch version


PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm install
npm_usage_f_youtube@1.0.0 C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
`-- lodash@4.14.2
星号语法

官网说: major release ,下载的时候现在了最新的包

{
    // ...
  "dependencies": {
    "lodash": "*"
  },
    // ...

}
下载指定版本
{
    // ...
  "dependencies": {
    "lodash": "3.0.1"
  },
    // ...

}

显然下载了 指定版本

PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm install
npm_usage_f_youtube@1.0.0 C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
`-- lodash@3.0.1
官方指导

If you were starting with a package 1.0.4, this is how you would specify the ranges:

Patch releases: 1.0 or 1.0.x or ~1.0.4
Minor releases: 1 or 1.x or ^1.0.4
Major releases: * or x

八、npm update

一般 npm update 更新本地包即可

至关重要的是: npm outdated [–local] 指令可以用来查看哪些指令过期

npm update pkg --save

更新本地生产环境*指定的包并写入 pakage.json 文件

npm update --save-dev

更新本地开发环境所有的包并写入 pakage.json 文件

npm update --global [pkg]

  1. npm update --global 更新所有的全局工具
  2. npm update --global pkg 更新 指定的 全局工具
npm 自身更新

npm install --global npm@latest

九、npm prune

用来删除 本地package.json 文件中没有保存,但是我们下载过的模块

也就是我们项目不用依赖的模块

{
  // ...
   "dependencies": {
    "lodash": "^4.17.5"
  },
  "devDependencies": {
  }
  // ...
}

PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list --depth 0
npm_usage_f_youtube@1.0.0 C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
+-- lodash@4.17.5
`-- moment@2.22.0 extraneous

prune 之后

PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm prune
- moment@2.22.0 node_modules\moment
npm WARN npm_usage_f_youtube@1.0.0 No repository field.
PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list --depth 0
npm_usage_f_youtube@1.0.0 C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
`-- lodash@4.17.5

十、npm shortcuts

传送门

The following shorthands are parsed on the command-line:

-v: –version
-h, -?, –help, -H: –usage
-s, –silent: –loglevel silent
-q, –quiet: –loglevel warn
-d: –loglevel info
-dd, –verbose: –loglevel verbose
-ddd: –loglevel silly
-g: –global
-C: –prefix
-l: –long
-m: –message
-p, –porcelain: –parseable
-reg: –registry
-f: –force
-desc: –description
-S: –save
-P: –save-prod
-D: –save-dev
-O: –save-optional
-B: –save-bundle
-E: –save-exact
-y: –yes
-n: –yes false

npm cli 官方参考文档(英)

传送门

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

npm 常用指令介绍 的相关文章

随机推荐

  • springframework.scheduling.quartz工作调度器使用(一):多定时任务共用quartz

    实现场景一 多个频率H1 5分钟 任务 H1 10分钟 任务 定时执行 且每个任务执行所需时间在频率时间段内 配置文件
  • VirtualBox与windows网络不通处理

    VirtualBox设置桥接 另 vmware类似问题 2022 2 1 更新 nat网络 新的虚拟机创建后 ip 如图 在windows上ping 虚拟机 显示网络不通 在对应虚拟机上右键 gt 设置 选择网络 将原来的 网络地址转换 N
  • mysql替换字段的域名

    需求 网站域名更换 需要一键替换全部域名或者替换原有的图片域名 使用sql update table set 字段名 replace 字段名 a xxxx cn bvvv bbbbbbb com
  • 疲劳驾驶监测方案_盘点疲劳驾驶的几种检测方法

    随着人们生活水平的逐渐提高以及各大城市道路交通系统的不断完善 我国的汽车保有总量也在不断增加 这在为人们的出行带来便捷的同时也导致了交通事故的频频发生 对驾驶员和行人的生命财产安全构成了巨大的威胁 根据我国交通部相关调查信息显示 当今诱发交
  • iphone14到手了?你还需要一个专职管家!

    现在距离苹果秋季新品发布会已过去月余 新iPhone 14系列和新版的iOS 16操作系统也如约与我们见面了 相信大家在9月初抢购的iPhone 14也基本到手了 但随之到来的数据资料备份迁移却是一件令人头大的事情 使用官方提供的iTune
  • k8s功能介绍和常用命令

    一 Node篇 kubectl get nodes 查看所有node信息 kubectl get nodes owide 查看所有node的详细信息 kubectl get node o yaml 查看所有node的yaml文件 kubec
  • python: 获取 后缀名(扩展名) / 文件名

    method 使用 os path splitext file 0 可获得 文件名 使用 os path splitext file 1 可获得以 开头的 文件后缀名 code import os file Hello py 获取前缀 文件
  • [YOLO专题-26]:YOLO V5 - ultralytics代码解析-detect.py程序的流程图与对应的plantUML源码

    作者主页 文火冰糖的硅基工坊 文火冰糖 王文兵 的博客 文火冰糖的硅基工坊 CSDN博客 本文网址 https blog csdn net HiWangWenBing article details 122443972 目录 第1章 det
  • 海南大学软件工程原理835考研

    海南大学835考研 2023考研已经落幕 我从2022 05 2023 03 用了大概一年的时间 目前已被海南大学计算机科学与技术学院录取 下面来谈谈我的学习感想 希望能给大家带来帮助 对于专业课 我用的是张海藩的软件工程导论第六版 和史济
  • MySQL数据库中随机获取一条或多条记录

    在开发过程中遇到了一个问题 使用MySQL数据库 用SQL语句在表中随机获取一条或多条数据 看似简单 但是往深层研究的话还是很有深度的 查了好多资料 接下来给大家分享一下 1 随机获取单条数据 SELECT FROM table name
  • springboot学习(一)——helloworld

    以下内容 如有问题 烦请指出 谢谢 springboot出来也很久了 以前零散地学习了不少 不过很长时间了都没有在实际中使用过了 忘了不少 因此要最近准备抽时间系统的学习积累下springboot 给自己留个根 因为以前学过一些 这里就主要
  • IDEA学习(一)——IDEA的安装

    最近IDE从Eclipse转到了IDEA 抽时间熟悉了一下IDEA相关的东西 在此记录一下说不定可以帮到有需要的同学 我们就先从IDEA的安装说起吧 需要说明一点的是IDEA是比较吃内存的 所以在安装IDEA之前最好确认的内存不要太小 最好
  • JavaScript运算符

    1 JavaScript运算符 算数运算符 运算符也叫做操作符 通过运算符可以对一个或则多个值进行运算 并获取运算结果 算数运算符 加 可以对两个值进行加法运算 如果是两个字符串则进行字符串拼接 任何值和字符串做加法运算 都会先转为字符串
  • 数据结构如何计算复杂度

    在学习具体的数据结构和算法之前 每一位初学者都要掌握一个技能 即善于运用时间复杂度和空间复杂度来衡量一个算法的运行效率 通过算法所编写出的程序的运行效率 程序的运行效率具体可以从 2 个方面衡量 分别为 程序的运行时间 程序运行所需内存空间
  • 熵值法计算权重有异常值_熵权法评价估计详细原理讲解

    写在前面 熵权法也属于一种综合评价方法 没有主观性 可与前面几篇文章提到的方法联合使用 目录 一 熵权法概述 1 1 信息论基础 1 2 熵权法介绍 二 熵权法赋权步骤 2 1数据标准化 2 2 求各指标在各方案下的比值 2 3 求各指标的
  • 一篇关于LLM指令微调的综述

    深度学习自然语言处理 原创作者 cola 指令微调 IT 是提高大型语言模型 LLM 能力和可控性的关键技术 其本质是指在由 INSTRUCTION OUTPUT 对组成的数据集上以监督的方式进一步训练LLM的过程 它弥合了LLM的下一个词
  • 线程连接池

    第一种 Executors newCacheThreadPool 可缓存线程池 先查看池中有没有以前建立的线程 如果有 就直接使用 如果没有 就建一个新的线程加入池中 缓存型池子通常用于执行一些生存期很短的异步型任务 package tes
  • FFPlay视频播放流程

    背景说明 FFmpeg是一个开源 免费 跨平台的视频和音频流方案 它提供了一套完整的录制 转换以及流化音视频的解决方案 而ffplay是有ffmpeg官方提供的一个基于ffmpeg的简单播放器 学习ffplay对于播放器流程 ffmpeg的
  • 机器学习 day31(baseline、学习曲线)

    语音识别的Jtrain Jcv和人工误差 对于逻辑回归问题 Jtrain和Jcv可以用分类错误的比例 这一方式来代替 单单只看Jtrain 不好区分是否高偏差 可以再计算人类识别误差 即人工误差 作为基准线来进行比较 Jtrain与base
  • npm 常用指令介绍

    npm usage what is npm 实现js代码的复用 分享和管理 npm consists of three distinct components 1 the website 通过 www npmjs com 网址查找需要的 包