英文:
Webpack is not bundling a dependency
问题
I have a Typescript library which uses Axios as following:
import axios from 'axios';
export class Connector { readonly axios = axios.create(); ... }
After publishing, I import the library in a new React project created with create-react-app --template=typescript
. When I run the project in the browser and instantiate a new connector such as:
import { Connector } from 'mylib';
const connector = new Connector();
I get an Uncaught TypeError: Cannot read properties of undefined
at the axios.create()
call. Inspecting in the browser, it seems webpack is not bundling anything related to Axios (which is a dependency from mylib
).
Now, If I do import Axios directly in the App.tsx
file and call axios.create()
, this last call works (but the call done inside the mylib
library still does not work).
Basically the axios
object is undefined
inside mylib
, and it seems webpack is producing an import for node_modules/axios/dist/browser/axios.cjs
, which does not exist in the bundle for some reason.
What is happening here?
Using webpack 5.
英文:
I have a Typescript library which uses Axios as following:
import axios from 'axios';
export class Connector { readonly axios = axios.create(); ... }
After publishing, I import the library in a new React project created with create-react-app --template=typescript
. When I run the project in the browser and instantiate a new connector such as:
import { Connector } from 'mylib';
const connector = new Connector();
I get an Uncaught TypeError: Cannot read properties of undefined
at the axios.create()
call. Inspecting in the browser, it seems webpack is not bundling anything related to Axios (which is a dependency from mylib
).
Now, If I do import Axios directly in the App.tsx
file and call axios.create()
, this last call works (but the call done inside the mylib
library still does not work).
Basically the axios
object is undefined
inside mylib
, and it seems webpack is producing an import for node_modules/axios/dist/browser/axios.cjs
, which does not exist in the bundle for some reason.
What is happening here?
Using webpack 5.
答案1
得分: 0
这是 Axios 1.1.3+ 中的一个错误,仍然存在:https://github.com/axios/axios/issues/5154
降级到 1.1.2 版本,问题就解决了。
英文:
This is a bug in Axios 1.1.3+, which is still broken: https://github.com/axios/axios/issues/5154
Downgrade to 1.1.2 and stuff works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论