如何在 ubuntu 20.04.1 LTS 上安装 yeoman 命令行工具?

问题描述

我尝试在 Ubuntu 20.04.1 LTS 中安装 yeoman 命令行工具以进行 ASP.NET Core Web 应用程序开发。打字后 npm install -g yo 在终端中,出现错误

错误描述如下。

npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated cross-spawn-async@2.2.5: cross-spawn no longer requires a build toolchain,use it instead
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN checkPermissions Missing write access to /usr/local/lib
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied,access '/usr/local/lib'
npm ERR!  { [Error: EACCES: permission denied,access '/usr/local/lib']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied,access \'/usr/local/lib\'',npm ERR!   errno: -13,npm ERR!   code: 'EACCES',npm ERR!   syscall: 'access',npm ERR!   path: '/usr/local/lib' }
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/shashankshekhar/.npm/_logs/2020-12-21T11_02_14_859Z-debug.log```

解决方法

全局模块安装的 EACCES 错误问题非常普遍,以至于 npman entire web page dedicated to solving just this issue

既然你安装的是yeoman,我想这是一台开发机器。在这种情况下,最好的办法可能是安装 node/npmnvm 之类的软件包管理器。

不想想做但人们通常会建议的一件事是使用 npm 运行 sudo 命令。人们推荐它是因为它简单。但是如果你这样做,你正在安装的任何依赖项的所有生命周期脚本(例如,postinstall)也将作为 root 运行。这可能是很多root 身份运行的不受信任的代码。