执行代码
npm install -g yo generator-code
时报错
(base) zf@192 test % npm install -g yo generator-code npm ERR! code EACCES npm ERR! syscall mkdir npm ERR! path /usr/local/lib/node_modules/generator-code npm ERR! errno -13 npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/generator-code' npm ERR! [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/generator-code'] { npm ERR! errno: -13, npm ERR! code: 'EACCES', npm ERR! syscall: 'mkdir', npm ERR! path: '/usr/local/lib/node_modules/generator-code' 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/zf/.npm/_logs/2022-06-09T03_50_43_247Z-debug-0.log
一看是权限问题,执行
sudo chown -R $USER /usr/local/lib/node_modules
这时又有报错
(base) zf@192 test % npm install -g yo generator-code npm WARN deprecated har-validator@5.1.5: this library is no longer supported npm WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details. npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142 npm ERR! code EACCES npm ERR! syscall symlink npm ERR! path ../lib/node_modules/yo/lib/cli.js npm ERR! dest /usr/local/bin/yo npm ERR! errno -13 npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules/yo/lib/cli.js' -> '/usr/local/bin/yo' npm ERR! [Error: EACCES: permission denied, symlink '../lib/node_modules/yo/lib/cli.js' -> '/usr/local/bin/yo'] { npm ERR! errno: -13, npm ERR! code: 'EACCES', npm ERR! syscall: 'symlink', npm ERR! path: '../lib/node_modules/yo/lib/cli.js', npm ERR! dest: '/usr/local/bin/yo' 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/zf/.npm/_logs/2022-06-09T03_53_45_762Z-debug-0.log
执行 sudo chown -R $USER /usr/local/bin
这时就可以安装了