Node.js/npm:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
无编辑摘要
 
(未显示同一用户的30个中间版本)
第1行: 第1行:
== 引言 ==
== 目录 ==


* [[/依赖检查|依赖检查]]
# [[/依赖检查|依赖检查]]
* [[/规范|规范]]
# [[/规范|规范]]
* [[/代理|代理]]
# [[/代理|代理]]
* [[/源|源]]
# [[/源|源]]
* [[/配置文件|配置文件]]
# [[/配置文件|配置文件]]
* [[/缓存|缓存]]
# [[/版本号码|版本号码]]
* [[/域|域]]
# [[/package.json|package.json]]
* [[/环境变量|环境变量]]
# [[/缓存|缓存]]
 
# [[/域|域]]
== 全局 ==
# [[/环境变量|环境变量]]
=== 查看 ===
# [[/指令|指令]]
{{shc_out|<syntaxhighlight lang="bash">
# [[/权限|权限]]
npm ls -g --depth=0</syntaxhighlight>|<syntaxhighlight lang="text">
# [[/工具|工具]]
/usr/local/lib
# [[/error|error]]
├── @vue/cli@4.4.6
# [[/别名|别名]]
├── n@6.5.1
# [[/token|Token]]
├── nodemon@2.0.4
├── npm@6.14.5
├── npm-check@5.9.2
├── nrm@1.2.1
├── webpack@4.43.0
└── yarn@1.22.4</syntaxhighlight>|$}}
=== 更新 ===
{{shc|<syntaxhighlight lang="bash">
npm i npm -g</syntaxhighlight>|$|/home/kangkk}}
=== 权限 ===
如果直接全局安装会报错。
{{shc_out|<syntaxhighlight lang="bash">
npm install webpack-cli -g
</syntaxhighlight>|<syntaxhighlight lang="text">
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm WARN webpack-cli@3.3.12 requires a peer of webpack@4.x.x but none is installed. You must install peer dependencies yourself.
 
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR!  errno: -13,
npm ERR!  code: 'EACCES',
npm ERR!  syscall: 'access',
npm ERR!  path: '/usr/local/lib/node_modules'
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/kangjian/.npm/_logs/2020-06-27T12_46_35_227Z-debug.log
</syntaxhighlight>|$}}
 
安装目录添加权限
{{shc|<syntaxhighlight lang="bash">
sudo chown -R ${USER} /usr/local/lib/node_modules/
sudo chown -R ${USER} /usr/local/bin/</syntaxhighlight>|kangjian@kangkk:/usr/local/lib$}}
 
== 指令 ==
 
== 安装路径 ==
=== 路径 ===
{{shc_out|<syntaxhighlight lang="bash">
npm root -g</syntaxhighlight>|<syntaxhighlight lang="text">
/usr/local/lib/node_modules</syntaxhighlight>|$}}
=== 环境 ===
{{shc_out|<syntaxhighlight lang="bash">
npm config ls</syntaxhighlight>|<syntaxhighlight lang="text">
; cli configs
metrics-registry = "https://registry.npm.taobao.org/"
scope = ""
user-agent = "npm/6.14.5 node/v12.18.1 linux x64"
 
; userconfig /home/kangjian/.npmrc
home = "https://npm.taobao.org"
registry = "https://registry.npm.taobao.org/"
 
; node bin location = /usr/local/bin/node
; cwd = /home/kangjian/tongdunol
; HOME = /home/kangjian
; "npm config ls -l" to show all defaults.
</syntaxhighlight>|$}}
 
=== prefix ===
查询
{{Shc_out|<syntaxhighlight lang="bash">
npm config get prefix
</syntaxhighlight>|<syntaxhighlight lang="bash">
/home/kangjian/.nvm/versions/node/v14.16.1
</syntaxhighlight>|jian@kang:/mnt/c/kangkk}}
设置
{{shc|<syntaxhighlight lang="bash">
npm config set prefix *</syntaxhighlight>|$}}
 
== 全局 ==
=== 普通用户 ===
在 Linux 和 Mac 中,普通用户如何全局安装 npm 包:<br>
需要将 npm 包存放在当前用户的主目录 <code>.npm-packages</code> 中
{{shc|<syntaxhighlight lang="bash">
mkdir .npm-packages</syntaxhighlight>|kangkk@LAPTOP-S5B1JMCV:~$}}
 
{{hc|~/.npmrc|<syntaxhighlight lang="javascript">
prefix=${HOME}/.npm-packages</syntaxhighlight>}}
{{hc|.bashrc or .bash_profile or .zshrc | <syntaxhighlight lang="bash">
# 定义npm存放的目录
NPM_PACKAGES="${HOME}/.npm-packages"
 
# 确保node可以找到安装的包
NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
 
# 确保可以使用安装的二进制应用和man文档
PATH="$NPM_PACKAGES/bin:$PATH"
# Unset manpath so we can inherit from /etc/manpath via the `manpath`
# command
unset MANPATH # delete if you already modified MANPATH elsewhere in your config
MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
</syntaxhighlight>}}
 
== 参数 ==
{| class="wikitable"
|-
| [[/unsafe-perm|unsafe-perm]] || postinstall || 示例
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}
=== legacy-peer-deps ===
{{shc|<syntaxhighlight lang="bash">
npm i --legacy-peer-deps</syntaxhighlight>|$|/home/kangkk}}
 
== view ==
{{Shc_out|<syntaxhighlight lang="bash">
npm view think-view versions</syntaxhighlight>|<syntaxhighlight lang="bash">
[
  '1.0.0',  '1.0.1',
  '1.0.2',  '1.0.3',
  '1.0.4',  '1.0.5',
  '1.0.7',  '1.0.8',
  '1.0.9',  '1.0.10',
  '1.0.11', '1.0.12',
  '1.0.13'
]
</syntaxhighlight>|$|/home/kangkk}}
 
{{Shc_out|<syntaxhighlight lang="bash">
npm view think-view versions --json</syntaxhighlight>|<syntaxhighlight lang="bash">
[
  "1.0.0",
  "1.0.1",
  "1.0.2",
  "1.0.3",
  "1.0.4",
  "1.0.5",
  "1.0.7",
  "1.0.8",
  "1.0.9",
  "1.0.10",
  "1.0.11",
  "1.0.12",
  "1.0.13"
]
</syntaxhighlight>|$|/home/kangkk}}
 
{{Shc_out|<syntaxhighlight lang="bash">
npm view think-view@1.0.13 engines</syntaxhighlight>|<syntaxhighlight lang="bash">
{ node: '>=6.0.0' }
</syntaxhighlight>|$|/home/kangkk}}
 
== install==
{{shc|<syntaxhighlight lang="bash">
npm install kangwuyi/git-repo.git
npm install github:kangwuyi/git-repo.git
npm install git+https://github.com/kangwuyi/git-repo.git
npm install git+ssh://github.com/kangwuyi/git-repo.git</syntaxhighlight>|$|/home/kangkk}}
 
== package-lock ==
禁止生成 package-lock.json 文件。
{{shc|<syntaxhighlight lang="bash">
npm config set package-lock false</syntaxhighlight>|$|/home/kangkk}}
== 包升级工具 ==
=== ls ===
{{shc_out|<syntaxhighlight lang="bash">
npm ls vue</syntaxhighlight>|<syntaxhighlight lang="text">
ask-go-pc@1.0.0 /mnt/d/gitee/k/ask-go-pc
├─┬ nuxt@2.14.6
│ ├─┬ @nuxt/builder@2.14.6
│ │ └─┬ @nuxt/vue-app@2.14.6
│ │  └── vue@2.6.12  deduped
│ └─┬ @nuxt/core@2.14.6
│  └─┬ @nuxt/vue-renderer@2.14.6
│    └── vue@2.6.12  deduped
└── vue@2.6.12
</syntaxhighlight>|kangkk@LAPTOP-S5B1JMCV:/mnt/d/gitee/k/ask-go-pc$}}
 
== 管理员 ==
授权用户
{{shc|<syntaxhighlight lang="bash">
npm owner add<user>[<@scope>/]<pkg>
npm owner rm<user>[<@scope>/]<pkg>
npm owner ls [<@scope>/]<pkg></syntaxhighlight>|$|/home/kangkk}}
 
== 命令 ==
=== config ===
{{Shc_out|<syntaxhighlight lang="bash">
npm config edit</syntaxhighlight>|<syntaxhighlight lang="bash">;;;;
; npm userconfig file: /Users/kangjian/.npmrc
// ...
...
</syntaxhighlight>|$|/home/kangkk}}
 
//
 
{{Shc_out|<syntaxhighlight lang="bash">
npm who am i</syntaxhighlight>|<syntaxhighlight lang="bash">kangjian
</syntaxhighlight>|$|/home/kangkk}}
//
{{Shc_out|<syntaxhighlight lang="bash">
npm config set strict-ssl false</syntaxhighlight>|<syntaxhighlight lang="bash">
</syntaxhighlight>|$|/home/kangkk}}
//
{{Shc_out|<syntaxhighlight lang="bash">
npm cache clean--force</syntaxhighlight>|<syntaxhighlight lang="bash">
</syntaxhighlight>|$|/home/kangkk}}
 
=== version ===
修复或小的模块逻辑更改。
{{shc|<syntaxhighlight lang="bash">
npm version patch</syntaxhighlight>|$|/home/kangkk}}
 
功能逻辑变更。
{{shc|<syntaxhighlight lang="bash">
npm version minor</syntaxhighlight>|$|/home/kangkk}}
 
大版本改动,不再向下兼容。
{{shc|<syntaxhighlight lang="bash">
npm version major</syntaxhighlight>|$|/home/kangkk}}
 
=== ls ===
查看命令。如查看某组件的版本信息。
 
{{Shc_out|<syntaxhighlight lang="bash">
npm ls express</syntaxhighlight>|<syntaxhighlight lang="bash">
kangkk-check-imported-file@0.0.6 /Users/kangjian/360/dora/td-deadfile
└── express@4.18.2
</syntaxhighlight>|$|/home/kangkk}}
 
除此之外还可以通过添加额外的参数检查深度。
 
{{Shc_out|<syntaxhighlight lang="bash">
npm ls --depth=0</syntaxhighlight>|<syntaxhighlight lang="bash">
kangkk-check-imported-file@0.0.6 /Users/kangjian/360/dora/td-deadfile
├── @semantic-release/changelog@6.0.1
├── @semantic-release/git@10.0.1
...
├── table@5.4.6
├── util@0.12.5
└── yargs@14.2.3
</syntaxhighlight>|$|/home/kangkk}}
 
== other ==
设置 python 环境的指向。
 
<syntaxhighlight lang="bash">
npm config set python "/Library/Frameworks/Python.framework/Versions/2.7/bin/python"
</syntaxhighlight>
 
== 版本 ==
 
{| class="wikitable"
| <nowiki>0.0.1</nowiki> || 版本 0.0.1
|-
| <nowiki>^0.0.1</nowiki> || 版本 0.0.1 兼容的最新版本
|-
| <nowiki>~0.0.1</nowiki> || 版本 0.0.2、0.0.3 等
|-
| <nowiki>~0.1</nowiki> || 版本 0.2、0.3 等
|-
| <nowiki>0.1.x</nowiki> || 版本 0.1 的任何补丁版本,<nowiki><</nowiki>、<nowiki><=</nowiki>、<nowiki>></nowiki>
|-
| <nowiki>>=0.0.1</nowiki> || 大于或等于版本 0.0.1
|-
| <nowiki>0.0.1 0.0.4</nowiki> || 版本 0.0.1、0.0.3、0.0.4
|}
 
== 分析工具 ==
# [https://github.com/privatenumber/pkg-size.dev pkg-size.dev]
# [https://github.com/pastelsky/bundlephobia bundlephobia.com]
== error ==
* rollbackFailedOptional
*: {{shc|<syntaxhighlight lang="bash">
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080
npm i
git config --global --unset http.proxy
git config --global --unset https.proxy</syntaxhighlight>|$}}
* permission denied, mkdir '/usr/local/lib/node_modules/node-sass/.node-gyp'
*: {{shc|<syntaxhighlight lang="bash">
sudo npm install node-sass --unsafe-perm --save-dev</syntaxhighlight>|$}}
* Error: ENOSPC: System limit for number of file watchers reached
*: 文件监视程序的系统产生了限制,达到了默认上限。
{{shc_out|<syntaxhighlight lang="bash">
cat /proc/sys/fs/inotify/max_user_watches
</syntaxhighlight>|<syntaxhighlight lang="javascript">
8192
</syntaxhighlight>|$}}
*: 临时增加文件监听限制
*: {{shc|<syntaxhighlight lang="bash">
sudo sysctl fs.inotify.max_user_watches=524288</syntaxhighlight>|$}}
*: 永久增加
*: {{shc|<syntaxhighlight lang="bash">
echo fs.inotify.max_user_watches = 524288 | sudo tee -a /etc/sysctl.conf
sudo sysctl -p</syntaxhighlight>|$}}
* Node Sass could not find a binding for your current environment
*: 重新添加 sass 依赖
*: {{shc|<syntaxhighlight lang="bash">
npm rebuild node-sass</syntaxhighlight>|$}}
* Error: EACCES: permission denied, mkdir '/opt/nodejs/node-v12.18.2-linux-x64/lib/node_modules/node-sass/.node-gyp'
*: <syntaxhighlight lang="bash">
sudo npm install node-sass --unsafe-perm --save-dev</syntaxhighlight>
* This is probably not a problem with npm. There is likely additional logging
* Error: connect ETIMEDOUT 104.20.23.46:443 gyp ERR! stack    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
*: <syntaxhighlight lang="bash">
rm -rf ~/.node-gyp</syntaxhighlight>
* wsl, Error: EACCES: permission denied, rename
: {{shc|<syntaxhighlight lang="bash">
npm config set unsafe-perm=true
npm config set prefix="/mnt/d/npm-global"</syntaxhighlight>|$|/home/kangkk}}
* ERESOLVE unable to resolve dependency tree
: node版本太高了

2025年12月17日 (三) 12:00的最新版本