【yarn for MAC】报错“-bash: yarn: command not found“ 则安装yarn

安装yarn

以下安装yarn方法有一个不行就直接换它

法一

前提条件:电脑已安装node才能使用这个方法

node -v

全局安装:

npm install yarn -g

如果输入:yarn --version能查看版本号说明,安装成功。

报错问题

如果遇上以下报错情况:

cengguilideMacBook-Pro:~ zengguili$ npm install yarn -g
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/yarn
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/yarn'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/yarn'] {
    
    
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules/yarn'
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/zengguili/.npm/_logs/2023-03-13T07_48_40_378Z-debug.log

解决方案一

sudo npm i yarn -g

解决方案二

(仅限电脑权限管控严格情况下,无法获取权限)

  1. 查看npm 全局文件安装地址
XXX@CN_CXXXMD6M ~ % npm list -g                    
/usr/local/lib
├── @quasar/cli@1.3.2
├── corepack@0.14.1
├── npm@8.19.2
└── yarn@1.22.18
  1. 从其他人电脑上同样的目录,拷贝出来两份文件,一个是命令的快捷方式文件,一个是环境变量所指的路径文件,粘贴到自己电脑相同位置
  2. 打开环境变量配置文件,配置环境变量
vim ~/.bash_profile
  1. 把下面内容粘贴到环境变量文件里,
export PATH=/usr/local/lib/node_modules/yarn/bin/:$PATH
  1. 保存.bash_profile的编辑,执行一下文件
source ~/.bash_profile
  1. 问题解决,大功告成,使用以下命令行重新打开,就可以使用yarn
yarn -v

解决方案三

  1. 利用这两个命令打开一个文件
touch ~/.bash_profile   //touch的意思是没有就创建;.bash_profile这是系统临时的环境变量, 

sudo open -e ~/.bash_profile
  1. 然后把export PATH=/usr/local/lib/node_modules/yarn/bin/:$PATH写进打开的文件里面,保存,再次执行命令:
source ~/.bash_profile
  1. 再查看一下版本
yarn --version

在这里插入图片描述

法二

使用mac 脚本安装

curl -o- -L https://yarnpkg.com/install.sh | bash  

PS:简单快速,强烈建议尝试,尤其适用在国内网络环境下适用

法三

使用homebrew安装

前提:安装了brew

brew install yarn 

PS:建议在良好的网络环境下尝试

猜你喜欢

转载自blog.csdn.net/qiqizgl777/article/details/129496430