无法在 MacOS 上安装 TypeScript - 终端错误

Posted

技术标签:

【中文标题】无法在 MacOS 上安装 TypeScript - 终端错误【英文标题】:Can't install TypeScript on MacOS - Terminal Error 【发布时间】:2018-12-09 12:55:03 【问题描述】:

我的 Mac 上有 Visual Studio Code,并想为 Angular 安装 TypeScript。我认为我之前已经安装了 Node.js 或 Git。如果 TypeScript 网站上的这个命令在 Mac 终端中运行,它应该会安装 TypeScript,但它不会:

npm install -g typescript

如果我将其粘贴到终端并按回车,则此错误代码会出现在终端中:(这是什么问题?我真的不明白,对终端一无所知,我不是电脑亲。如果您有一些针对初学者的简单提示,那将是一个很大的帮助。谢谢!!)

MacBook-Pro:~ Max$ npm install -g typescript
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
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 ERR!   [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
npm ERR!   stack:
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! 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/Max/.npm/_logs/2018-06-30T14_50_35_633Z-debug.log
MacBook-Pro:~ Max$ clear

    MacBook-Pro:~ Max$ npm install -g typescript
    npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
    npm ERR! path /usr/local/lib/node_modules
    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 ERR!   [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
    npm ERR!   stack:
    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! 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/Max/.npm/_logs/2018-06-30T14_58_29_869Z-debug.log
    MacBook-Pro:~ Max$ clear

    MacBook-Pro:~ Max$ npm install -g typescript
    npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
    npm ERR! path /usr/local/lib/node_modules
    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 ERR!   [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
    npm ERR!   stack:
    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! 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).

【问题讨论】:

你需要使用sudo 【参考方案1】:

npm WARN checkPermissions 缺少对 /usr/local/lib/node_modules 的写入权限

意味着您需要权限才能下载到该位置,它需要 root 权限的原因可能是因为您使用 root 权限安装了 nodejs/npm。

sudo npm install -g typescript

然后输入你的密码

【讨论】:

我还没有足够的支持声望,但非常感谢您的帮助。似乎它奏效了。我认为终端的响应应该没问题:/usr/local/bin/tsc -> /usr/local/lib/node_modules/typescript/bin/tsc /usr/local/bin/tsserver -> /usr/local/lib/node_modules/typescript/bin/tsserver + typescript@2.9.2 added 1 package from 1 contributor in 1.177s MacBook-Pro:~ 如果你想解决它,请参考这个答案:) ***.com/questions/33725639/… 你是什么意思,你能说得更详细一点吗? :-)(我对***和编码还是很陌生) 永远不要对节点包使用 sudo。特别是使用 npm 或此类库。

以上是关于无法在 MacOS 上安装 TypeScript - 终端错误的主要内容,如果未能解决你的问题,请参考以下文章

无法在终端 macOS 10.15.7 上安装 cocoapods

Xcode 无法安装在“macOS Sierra”上,因为需要 macOS 版本 10.14.3 或更高版本

无法在 MacOS 上使用 pyenv Python 安装 tkinter

无法在 Macos 错误上安装 Scikit Learn 包 [重复]

无法在 macOS X 上安装 ghostscript

无法在 MacOS 上通过 pip install 安装 psycopg2 包