Webpack没有捆绑依赖。

huangapple go评论71阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月4日 09:50:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75633189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定