Axios works perfectly in production, but when building the application I get this error.
> react-app@0.0.0 build
> vite build
vite v4.4.7 正在为生产环境构建...
✓ 118 个模块已转换。
✓ 1.88s 内构建完成
[commonjs--resolver] C:/Users/application/client/node_modules/axios/lib/utils.js 中出现意外的标记 (714:2)
文件: C:/Users/application/client/node_modules/axios/lib/utils.js:714:2
712: toFiniteNumber,
713: findKey,
714: {}: _global,
^
715: isContextDefined,
716: ALPHABET,
构建过程中发生错误:
SyntaxError: C:/Users/application/client/node_modules/axios/lib/utils.js 中出现意外的标记 (714:2)
at pp.raise
Try updating npm, removing the node module and reinstalling dependencies, as well as updating all dependencies including axios.
Add the following content to your vite.config.js:
define: { _global: ({}), }This will define your global object as an empty object.
This part of the vite configuration file performs a search and replace operation, and it seems that your build tool cannot find the definition of
_globalin your project (I guess it may be window,globalNode objects, etc., becauseaxiosis a library that can run on both browsers and servers)Also, depending on your build goals, deleting the
_globaldefinition in thevite.configfile may also solve your problem. I don't have enough information to help you further, the above are just my assumptions based on your misinformation. It may be helpful to provide information such as your vite configuration node version.