npm install/i -g/-S/-D命令总结

本文详细介绍了npm install命令的各种用法,包括本地安装、全局安装、指定版本安装等,并解释了如何使用简写形式及各种选项参数,如--save、--save-dev等。

cmd查看npm install的帮助:

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]
命令&参数的完整写法简写含义
npm install xxxnpm i xxx本地安装xxx包
–global-g全局安装,安装后可直接使用相应包里面提供的命令
–save-S本地安装,安装版本写入package.jsondependencies
–save-dev-D本地安装,安装版本写入package.jsondevDependencies
[gitlab-runner@c ~]$ npm install gitbook-cli -g npm WARN deprecated q@1.5.0: You or someone you depend on is using Q, the JavaScript Promise library that gave JavaScript developers strong feelings about promises. They can almost certainly migrate to the native JavaScript promise now. Thank you literally everyone for joining me in this bet against the odds. Be excellent to each other. npm WARN deprecated npm WARN deprecated (For a CapTP with native promises, see @endo/eventual-send and @endo/captp) [..................] - fetchMetadata: WARN deprecated q@1.5.0: You or someone you depend on is using Q, the JavaScript Promise library that gave JavaScript developers strong feelings about promises. They can almost certainly migrate to the native JavaScri [..................] | fetchMetadata: WARN deprecated q@1.5.0: You or someone you depend on is using Q, the JavaScript Promise library that gave JavaScript developers strong feelings about promises. They can almost certainly migrate to the native JavaScri [..................] \ fetchMetadata: WARN deprecated q@1.5.0: You or someone you depend on is using Q, the JavaScript Promise library that gave JavaScript developers strong feelings about promises. They can almost certainly migrate to the native JavaScri npm WARN checkPermissions Missing write access to /usr/local/node/lib/node_modules/gitbook-cli/node_modules/npmi/node_modules/npm/node_modules/ansistyles npm WARN checkPermissions Missing write access to /usr/local/node/lib/node_modules/gitbook-cli/node_modules/npmi/node_modules/npm/node_modules/path-is-inside npm WARN checkPermissions Missing write access to /usr/local/node/lib/node_modules/gitbook-cli npm WARN checkPermissions Missing write access to /usr/local/node/lib/node_modules/gitbook-cli/node_modules/npmi/node_modules/npm/node_modules npm WARN checkPermissions Missing write access to /usr/local/node/lib/node_modules npm ERR! code EACCES npm ERR! syscall access npm ERR! path /usr/local/node/lib/node_modules/gitbook-cli/node_modules/npmi/node_modules/npm/node_modules/ansistyles npm ERR! errno -13 npm ERR! Error: EACCES: permission denied, access '/usr/local/node/lib/node_modules/gitbook-cli/node_modules/npmi/node_modules/npm/node_modules/ansistyles' npm ERR! [Error: EACCES: permission denied, access '/usr/local/node/lib/node_modules/gitbook-cli/node_modules/npmi/node_modules/npm/node_modules/ansistyles'] { npm ERR! errno: -13, npm ERR! code: 'EACCES', npm ERR! syscall: 'access', npm ERR! path: '/usr/local/node/lib/node_modules/gitbook-cli/node_modules/npmi/node_modules/npm/node_modules/ansistyles' npm ERR! } npm ERR! npm ERR! The operation was rejected by your operating system. npm ERR! It is likely you do not have the permissions to access this file as the current user npm ERR! npm ERR! If you believe this might be a permissions issue, please double-check the npm ERR! permissions of the file and its containing directories, or try running npm ERR! the command again as root/Administrator. npm ERR! A complete log of this run can be found in: npm ERR! /home/gitlab-runner/.npm/_logs/2025-08-14T09_56_46_078Z-debug.log
08-15
<think> 我们正在回答用户关于npm install -g命令-g参数的含义的问题 根据引用[1]:-g参数用于全局安装,包将被安装在系统的全局目录中,其可执行文件会被添加到系统的PATH中,从而可以从任何位置调用。 引用[2]:-g是--global的简写,表示全局安装。 引用[3]:npm i module_name -g 全局安装,并且在package.json里面找不到这个包(因为全局安装不写入项目的package.json)。 引用[4]:全局安装通常用于安装一些命令行工具,这些工具在项目开发过程中作为工具使用,而不是项目运行所必需的依赖。 引用[5]:不加--save或--save-dev参数,则不会保存到package.json中,而全局安装本身就不写入项目的package.json。 因此,我们可以总结如下: 1. 全局安装的位置是系统的全局目录(可以通过`npm root -g`查看)。 2. 全局安装的包提供的命令(如果有)会被添加到系统的PATH环境变量中,因此可以在任何目录下直接运行。 3. 全局安装的包不会写入项目的package.json文件中(即不会作为项目的依赖)。 使用场景:全局安装通常用于安装一些需要在命令行中直接使用的工具,例如vue-cli、create-react-app等脚手架工具。 注意:在项目中使用的依赖(无论是运行依赖还是开发依赖)都应该使用本地安装(不加-g),并通过-S或-D参数保存到package.json中,以便于项目的协作和部署。 下面我们按照用户要求,使用中文清晰回答,并在末尾添加相关问题。 </think>`npm install -g` 命令中的 **`-g` 参数**表示**全局安装(Global Install)**,其核心含义和特点如下: ### 一、核心含义 1. **安装位置不同** - 默认(无 `-g`):安装到**当前项目的 `node_modules` 目录** - 带 `-g`:安装到**系统的全局目录**(通过 `npm root -g` 可查看路径,如 `/usr/local/lib/node_modules`) ```bash # 查看全局安装路径 npm root -g ``` 2. **环境作用域** - 全局安装的包会被加入系统的 **PATH 环境变量**,其命令行工具可在**任意目录直接调用** ```bash # 例如全局安装 Vue CLI 后,可在任何位置执行 vue create my-project ``` 3. **依赖关系管理** | 参数 | 写入文件 | 使用场景 | |------------|-------------------|------------------------| | 无 `-g` | 不写入任何文件 | 临时测试 | | `-S`/`--save` | `package.json` 的 `dependencies` | 项目运行必需依赖(如 React) | | `-D`/`--save-dev` | `package.json` 的 `devDependencies` | 开发工具(如 ESLint) | | `-g` | **不写入 `package.json`** | 独立工具(如脚手架) | ### 二、典型使用场景 1. **安装全局命令行工具** ```bash # 安装 Vue 脚手架(全局使用) npm install -g @vue/cli ``` - 安装后可直接在终端执行 `vue --version`[^1][^2] 2. **避免重复安装常用工具** 如 `nodemon`(热更新工具)、`http-server`(本地服务器)等只需全局安装一次[^4]。 3. **与项目依赖隔离** 适用于**非项目代码直接依赖**的工具(如构建工具 gulp、打包工具 webpack-cli)[^4]。 ### 三、注意事项 1. **权限问题** 在 Linux/macOS 可能需要 `sudo`: ```bash sudo npm install -g eslint ``` 2. **版本管理建议** - 项目依赖:**禁止**使用 `-g`(应通过 `-S`/`-D` 写入 `package.json`)[^3][^5] - 全局工具:推荐用 `npx` 临时调用避免版本冲突(如 `npx create-react-app`) > 通过 `npm list -g --depth=0` 可查看所有全局安装的包[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值