Node.js/npm
来自康健生活
< Node.js
引言
目录
全局
查看
[jian@kang /home/kwy]$
npm ls -g --depth=0
/usr/local/lib
├── @vue/cli@4.4.6
├── n@6.5.1
├── 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
更新
/home/kangkk
npm i npm -g
权限
如果直接全局安装会报错。
[jian@kang /home/kwy]$
npm install webpack-cli -g
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
安装目录添加权限
sudo chown -R ${USER} /usr/local/lib/node_modules/
sudo chown -R ${USER} /usr/local/bin/
指令
全局
普通用户
在 Linux 和 Mac 中,普通用户如何全局安装 npm 包:
需要将 npm 包存放在当前用户的主目录 .npm-packages 中
mkdir .npm-packages
~/.npmrc
prefix=${HOME}/.npm-packages
.bashrc or .bash_profile or .zshrc
# 定义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)"
参数
| unsafe-perm | postinstall | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
legacy-peer-deps
/home/kangkk
npm i --legacy-peer-deps
view
[jian@kang /home/kangkk]$
npm view think-view versions
[
'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'
]
[jian@kang /home/kangkk]$
npm view think-view versions --json
[
"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"
]
[jian@kang /home/kangkk]$
npm view think-view@1.0.13 engines
{ node: '>=6.0.0' }
install
/home/kangkk
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
package-lock
禁止生成 package-lock.json 文件。
/home/kangkk
npm config set package-lock false
管理员
授权用户
/home/kangkk
npm owner add<user>[<@scope>/]<pkg>
npm owner rm<user>[<@scope>/]<pkg>
npm owner ls [<@scope>/]<pkg>
other
设置 python 环境的指向。
npm config set python "/Library/Frameworks/Python.framework/Versions/2.7/bin/python"
分析工具
error
- rollbackFailedOptional
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
- permission denied, mkdir '/usr/local/lib/node_modules/node-sass/.node-gyp'
sudo npm install node-sass --unsafe-perm --save-dev
- Error: ENOSPC: System limit for number of file watchers reached
- 文件监视程序的系统产生了限制,达到了默认上限。
[jian@kang /home/kwy]$
cat /proc/sys/fs/inotify/max_user_watches
8192
- 临时增加文件监听限制
sudo sysctl fs.inotify.max_user_watches=524288
- 永久增加
echo fs.inotify.max_user_watches = 524288 | sudo tee -a /etc/sysctl.conf sudo sysctl -p
- Node Sass could not find a binding for your current environment
- 重新添加 sass 依赖
npm rebuild node-sass
- Error: EACCES: permission denied, mkdir '/opt/nodejs/node-v12.18.2-linux-x64/lib/node_modules/node-sass/.node-gyp'
sudo npm install node-sass --unsafe-perm --save-dev
- 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)
rm -rf ~/.node-gyp
- wsl, Error: EACCES: permission denied, rename
/home/kangkk
npm config set unsafe-perm=true npm config set prefix="/mnt/d/npm-global"
- ERESOLVE unable to resolve dependency tree
- node版本太高了
No categories