Mac npm -g 全局安装时报错

Mac npm -g 全局安装时出现未找到路径的error

今天学习typescript时,执行 npm install -g typescript时出现了一段报错
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
Error: EACCES: permission denied, access '/usr/local/lib/node_modules'

全部报错信息如下:

PM18896500037:~ 18896500037$ npm i -g typescript
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
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!   stack: "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!     /Users/18896500037/.npm/_logs/2020-08-17T07_41_52_675Z-debug.log

我发现大概是没有写的权限

我是这样解决的:

  1. 先在系统目录下创建一个新的目录文件夹

mkdir ~/.npm-global

  1. 将npm对应的配置和这个文件夹绑定 ,到这一步就可以进行正常的npm -g操作,但是想使用还得继续

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

  1. 查看系统用户根目录下有无.bash_profile文件

open 〜/.bash_profile

  1. 如果有这个文件就会自动打开,若没有(显示〜/.bash_profile does not exist.),输入

touch .bash_profile新建这个文件

  1. 然后再打开

open ~/.bash_profile

  1. 将这段话填入这个文件

export PATH=$PATH:/Users/[username]/.npm-packages/bin
[username]为你的用户名

  1. 保存文件并且重启项目就ok了

第2步执行后,npm -g安装好后发现还会出现 XXX:command not found 的报错

这时候3~7的过程是必不可少的

你可能感兴趣的:(node.js,macos,前端)