首页 > 解决方案 > 未捕获的 SyntaxError:意外的标记 '<' | 在“纱线添加 xlsx”之后 | 反应打字稿

问题描述

我正在用 React 开发项目(类型脚本)。我想为导出/导入 excel 文件添加新功能,我决定使用 xlsx 库https://www.npmjs.com/package/xlsx 所以我的问题是我运行了“yarn add xlsx”并且我的应用程序给了我错误“未捕获的语法错误:意外的令牌 '<' |”。我没有做任何导入,只是 yarn add xlsx 并且出现了这个错误。[![在此处输入图像描述][1]][1] 编译成功,没有其他错误。

奇怪的是,我发现我什么时候会在我的项目中做这个 hack:

npm install xlsx
yarn install

然后一切正常。我在不同的计算机上尝试了在同一个项目中添加 xlsx,但它在任何地方都不起作用......但后来我决定使用 npm 在本地添加的 xlsx 实现一些基本功能,并将我的项目部署在没有“npm install xlsx”的 linux docker 上,并且它在那里工作!

也许文件系统或 fs 库有问题?

编辑: 新信息 事实证明,我什么时候会做这样的场景

一切正常

但如果我愿意

出现错误!

所以在运行 yarn install 之后,安装了一些依赖项,这就是它失败的原因这不好,因为当我提交我的更改时,其他开发人员将使用“yarn install”安装依赖项

标签: reactjstypescriptxlsxyarnpkgreact-scripts

解决方案


I had unexpected token because I used %PUBLIC_PATH% variable in my index.html so I have to define this variable in .env file and of course I have it there

PUBLIC_PATH:/my-app/

But I didn't have this variable in .env.development file and everything was just fine until I updated react-scripts. So in react-scripts 3.2.0 it was by default "/" and after I updated react scripts to 3.4.0 it was failing with unexpected token error. I simply added

PUBLIC_PATH:/ 

to my .env.development file and everything works now


推荐阅读