Node.js/npm

来自康健生活
Kangkk​(留言 | 贡献)2025年7月18日 (五) 20:04的版本 (→版本)

引言

目录

全局

查看

[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/

指令

安装路径

路径

[jian@kang /home/kwy]$
npm root -g
/usr/local/lib/node_modules

环境

[jian@kang /home/kwy]$
npm config ls
; 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.

prefix

查询

jian@kang:/mnt/c/kangkk
npm config get prefix
/home/kangjian/.nvm/versions/node/v14.16.1

设置

 
npm config set prefix *

全局

普通用户

在 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

包升级工具

ls

kangkk@LAPTOP-S5B1JMCV:/mnt/d/gitee/k/ask-go-pc$
npm ls vue
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

管理员

授权用户

/home/kangkk
npm owner add<user>[<@scope>/]<pkg>
npm owner rm<user>[<@scope>/]<pkg>
npm owner ls [<@scope>/]<pkg>

命令

config

[jian@kang /home/kangkk]$
npm config edit
;;;;
; npm userconfig file: /Users/kangjian/.npmrc
// ...
...

//

[jian@kang /home/kangkk]$
npm who am i
kangjian

//

[jian@kang /home/kangkk]$
npm config set strict-ssl false

//

[jian@kang /home/kangkk]$
npm cache clean--force

version

修复或小的模块逻辑更改。

/home/kangkk
npm version patch

功能逻辑变更。

/home/kangkk
npm version minor

大版本改动,不再向下兼容。

/home/kangkk
npm version major

ls

查看命令。如查看某组件的版本信息。

[jian@kang /home/kangkk]$
npm ls express
kangkk-check-imported-file@0.0.6 /Users/kangjian/360/dora/td-deadfile
└── express@4.18.2

除此之外还可以通过添加额外的参数检查深度。

[jian@kang /home/kangkk]$
npm ls --depth=0
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

other

设置 python 环境的指向。

npm config set python "/Library/Frameworks/Python.framework/Versions/2.7/bin/python"

分析工具

  1. pkg-size.dev
  2. bundlephobia.com

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版本太高了