Importing ‘countdown’ module causes error when building React app with Vite

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

Importing 'countdown' module causes error when building React app with Vite

问题

I'm using the 'countdown' module in my React app, which I import like this:

import countdown from 'countdown';

When I run the app in dev mode with Vite, everything works fine. However, when I try to build the app, I get the following error:

RollupError: "default" is not exported by "node_modules/.pnpm/countdown@2.6.0/node_modules/countdown/countdown.js", imported by "src/App.tsx".

I've tried changing the import statement to * as countdown instead, but then I get the following error:

DEFAULTS" is not exported by "node_modules/.pnpm/countdown@2.6.0/node_modules/countdown/countdown.js", imported by "src/App.tsx".
Cannot call a namespace ("countdown").

Here is the MRE.

Does anyone know what might be causing these errors and how I can fix them? I'm fairly new to React and Vite, so any help would be appreciated.

Thanks in advance!

英文:

I'm using the 'countdown' module in my React app, which I import like this:

import countdown from 'countdown';

When I run the app in dev mode with Vite, everything works fine. However, when I try to build the app, I get the following error:

RollupError: "default" is not exported by "node_modules/.pnpm/countdown@2.6.0/node_modules/countdown/countdown.js", imported by "src/App.tsx".

I've tried changing the import statement to * as countdown instead, but then I get the following error:

DEFAULTS" is not exported by "node_modules/.pnpm/countdown@2.6.0/node_modules/countdown/countdown.js", imported by "src/App.tsx".
Cannot call a namespace ("countdown").

Here is the MRE

Does anyone know what might be causing these errors and how I can fix them? I'm fairly new to React and Vite, so any help would be appreciated.

Thanks in advance!

答案1

得分: 1

尽管我对这个解决方案并不特别喜欢,但它确实有效。基本上,我复制了库代码和类型声明(countdown.jscountdown.d.ts),将 countdown.js 转成了一个 ESM 模块,并对 countdown.d.ts 做了一个小修改来导出模块。

let countdown: countdown.CountdownStatic;
export default countdown;

经过这些更改,一切都按预期运行和构建。

英文:

Although I'm not particularly fond of this solution, it does work. Essentially, I copied the library code and type declarations (countdown.js and countdown.d.ts), turned countdown.js into an esm module, and made a small edit to countdown.d.ts to export the module.

let countdown: countdown.CountdownStatic;
export default countdown;

With these changes, everything runs and builds as expected.

答案2

得分: 0

你应该使用命名导入:

import * as countdown from 'countdown';

如果仍然出现错误"cannot call a namespace",你可以尝试像这样访问countdown模块的函数:

const remainingTime = countdown.default(new Date(2023, 4, 20));
英文:

you should use the named import

import * as countdown from 'countdown';

if this still gives you the error "cannot call a namespace", you can try accessing the functions of the countdown module like

const remainingTime = countdown.default(new Date(2023, 4, 20));

huangapple
  • 本文由 发表于 2023年4月13日 21:03:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005777.html
匿名

发表评论

匿名网友

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

确定