npm常用命令使用指南

总结自己常用的npm命令;

查看已安装的全局的包 npm list -g --depth 0

安装webpack npm install webpack -g | npm install -g webpack

卸载webpack npm uninstall webpack -g | npm uninstall -g webpack 卸载后,重新打开终端,输入查看已安装包命令即可

查看mac本的全局安装包和当前文件夹下的安装包:

ZBMAC-C02VX3K0H:~ zhangnan12$ which npm /usr/local/bin/npm

sudo npm install -g nornj-cli 全局包安装在 /usr/local/lib 下面的 node_modules sudo npm uninstall -g nornj-cli

ZBMAC-C02VX3K0H:~ zhangnan12$ npm ls -g --depth 0 /usr/local/lib ├── @vue/cli@3.9.2 ├── asar@0.14.3 ├── cc-cli@1.0.13 ├── create-react-app@2.1.3 ├── es-checker@1.4.1 ├── install@0.12.2 ├── n@4.1.0 ├── nornj-cli@0.4.1 ├── npm@6.10.0 └── webpack@4.35.2

ZBMAC-C02VX3K0H:~ zhangnan12$ npm ls --depth 0 /Users/zhangnan12 ├── @babel/core@7.3.4 └── list@2.0.18

  1. 全局包 安装在 /usr/local/lib 下面的 node_modules下 该文件夹的权限是 admin ,所以安装的时候总是提示 “errno -13” ZBMAC-C02VX3K0H:~ zhangnan12$ npm i -g npm npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/npm npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules npm ERR! path /usr/local/lib/node_modules/npm npm ERR! code EACCES npm ERR! errno -13 npm ERR! syscall access npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules/npm' npm ERR! { [Error: EACCES: permission denied, access '/usr/local/lib/node_modules/npm'] npm ERR! stack: npm ERR! 'Error: EACCES: permission denied, access '/usr/local/lib/node_modules/npm'', npm ERR! errno: -13, npm ERR! code: 'EACCES', npm ERR! syscall: 'access', npm ERR! path: '/usr/local/lib/node_modules/npm' } 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 (though this is not recommended).

npm ERR! A complete log of this run can be found in: npm ERR! /Users/zhangnan12/.npm/_logs/2019-07-08T11_34_08_170Z-debug.log

解决方法: 将 /usr/local/lib 下面的 node_modules下,定义在 有权限的 /user/zhangnan/lib 下的node_modules 就方便了,后续安装的时候不用 sudo

创建全局的npm包安装路径 .npm-global 是隐藏文件夹 lib是显性文件夹 ,通过shift+command+.来 切换隐藏和显示 mkdir ~/.npm-global | 或者 mkdir ~/lib

配置npm使用新的路径

npm config set prefix '~/.npm-global'

打开或者新建~/.profile文件并添加如下行

export PATH=~/.npm-global/bin:$PATH

更新刚才添加的环境变量

source ~/.profile

测试一下ok啦

npm install -g brower-sync


3)修改全局npm包的路径

ZBMAC-C02VX3K0H:~ zhangnan12$ ls -ltra|grep .npm drwxr-xr-x 8 zhangnan12 360BUYAD\Domain Users 256 1 28 10:46 .npm -rw------- 1 zhangnan12 360BUYAD\Domain Users 63 7 9 09:49 .npmrc

.npmrc 文件是存在 npm config 命令的内容的;

ZBMAC-C02VX3K0H:~ zhangnan12$ npm config list

; cli configs metrics-registry = "https://registry.npm.taobao.org/" scope = "" user-agent = "npm/6.10.0 node/v10.15.0 darwin x64"

; userconfig /Users/zhangnan12/.npmrc prefix = "/Users/zhangnan12/.npm-global" registry = "https://registry.npm.taobao.org/"

; node bin location = /usr/local/bin/node ; cwd = /Users/zhangnan12 ; HOME = /Users/zhangnan12 ; "npm config ls -l" to show all defaults.

ZBMAC-C02VX3K0H:~ zhangnan12$ cat .npmrc registry=https://registry.npm.taobao.org/ prefix=~/.npm-global

这个命令是修改全局npm安装包的存储本地的路径的; npm config set prefix '~/lib'

修改路径后,添加到mac环境变量中进行生效 , $PATH存放在如下隐藏文件中。 .bash_profile

---查看 存放的地方, 在NPM_HOME路径下的 node_modules中。 ZBMAC-C02VX3K0H:~ zhangnan12$ npm root /Users/zhangnan12/node_modules ZBMAC-C02VX3K0H:~ zhangnan12$ npm root -g /Users/zhangnan12/.npm-global/lib/node_modules