使用Yarn安装Next.js(含旧版本)时遇空字节错误求解决方案
解决Yarn创建指定版本Next.js应用时的null字节错误
问题背景
使用yarn create next-app@latest或yarn create next-app@12时触发错误:
An unexpected error occurred: "The property 'options.env['npm_config__p_a_c_k_a_g_e___l_o_c_k_']' must be a string without null bytes. Received '\x00t\x00r\x00u\x00e\x00'"
环境:Node 19.4.0、NPM 9.7.2、Yarn 1.22.19,且无版本指定的create-next-app codeswear可正常执行。
可行解决方案
临时清除冲突环境变量
问题源于NPM传入的带null字节的环境变量,Yarn 1.x无法处理,执行命令前先清除:# Linux/macOS unset npm_config__p_a_c_k_a_g_e___l_o_c_k_ && yarn create next-app@12# Windows PowerShell Remove-Item Env:npm_config__p_a_c_k_a_g_e___l_o_c_k_ yarn create next-app@12改用NPM创建
绕开Yarn的兼容问题,直接用NPM执行:npx create-next-app@12降级NPM版本
NPM 9.x与Yarn 1.x的环境变量传递存在bug,降级到8.x稳定版:npm install -g npm@8.19.4降级后重新尝试Yarn的创建命令。
升级Yarn到3.x
Yarn 3.x对高版本Node兼容性更好,先升级Yarn:yarn set version berry再执行创建命令:
yarn create next-app@12
内容的提问来源于stack exchange,提问作者Ayan Das Karmakar
相关产品推荐
相关产品推荐

