我有以下几点
import logo from './logo.svg';
import './App.css';
import AppBar from '@mui/material/AppBar';
...
import axios from "axios";
import { useState, useEffect } from 'react';
...
function App() {
}
一切正常,但当我试着运行测试时,我得到...
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import axios from './lib/axios.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
5 | import Typography from '@mui/material/Typography';
6 | import Box from '@mui/material/Box';
> 7 | import axios from "axios";
| ^
如果我删除Axios代码,一切正常,那么为什么导入失败呢?
2条答案
按热度按时间rsaldnfx1#
要修复此问题,请将package.json中的test命令更新为:
"test": "react-scripts test --transformIgnorePatterns 'node_modules/(?!axios)/'",
。此错误是因为axios在v1.x.x和更高版本中将其模块类型从CommonJS更改为ECMAScript。w46czmvw2#
我不知道确切的,但我认为,这是Axios的新更新,你可以看到它:
https://github.com/axios/axios/issues/5101