Axios related errors encountered when building React applications using vite
P粉675258598
P粉675258598 2024-04-05 13:20:02
0
1
574

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.

P粉675258598
P粉675258598

reply all(1)
P粉477369269

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 _global in your project (I guess it may be window, globalNode objects, etc., because axios is a library that can run on both browsers and servers)

Also, depending on your build goals, deleting the _global definition in the vite.config file 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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template