Node.js(CommonJS)中Axios报Cannot read properties of undefined (reading 'get')的解决
Node.js 16.15.0 + Axios 1.1.3: 解决"Cannot read properties of undefined (reading 'get')"错误
在Node.js 16.15.0环境下使用CommonJS规范引入Axios 1.1.3时,调用axios.get出现Cannot read properties of undefined (reading 'get')错误,可通过以下几种方式排查解决:
1. 重新安装Axios,确保依赖完整
可能是Axios安装不完整导致模块导出异常,执行以下命令重新安装指定版本:
npm uninstall axios npm install axios@1.1.3
2. 调整Axios的引入方式
CommonJS环境下Axios的导出结构可能因版本细节存在差异,尝试以下两种引入写法:
写法一:单独提取default实例
const axios = require("axios"); const axiosInstance = axios.default; async function test(){ const data = await axiosInstance.get("https://www.google.com") return data } test().then((res) => console.log(res.data))
写法二:直接解构get方法
const { get } = require("axios"); async function test(){ const data = await get("https://www.google.com") return data } test().then((res) => console.log(res.data))
3. 排查模块引入冲突
确认项目根目录下不存在名为axios.js、axios的文件夹,避免Node.js错误地引入本地文件而非npm安装的依赖包。
内容的提问来源于stack exchange,提问作者syber man
相关产品推荐
相关产品推荐

