程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了导入 CommonJS 默认导出为命名导出/无法加载 ES 模块大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决导入 CommonJS 默认导出为命名导出/无法加载 ES 模块?

开发过程中遇到导入 CommonJS 默认导出为命名导出/无法加载 ES 模块的问题如何解决?下面主要结合日常开发的经验,给出你关于导入 CommonJS 默认导出为命名导出/无法加载 ES 模块的解决方法建议,希望对你解决导入 CommonJS 默认导出为命名导出/无法加载 ES 模块有所启发或帮助;

我正在使用 TypeScript 使用 discord.Js 制作机器人,但在尝试运行生成的 JavaScript 时出现以下错误。

import { ClIEnt,FrIEndlyError,sqliteProvIDer } from 'discord.Js-commando';
                 ^^^^^^^^^^^^^
SyntaxError: named export 'FrIEndlyError' not found. The requested module 'discord.Js-commando' is a Commonjs module,which may not support all module.exports as named exports.
Commonjs modules can always be imported via the default export,for example using:

import pkg from 'discord.Js-commando';
const { ClIEnt,sqliteProvIDer } = pkg;
@H_674_6@

进行这些修改后,我收到此错误:

internal/process/esm_loader.Js:74
    internalBinding('errors').triggeruncaughtException(
                              ^

Error [ERR_requIRE_ESM]: Must use import to load ES Module: C:\Users\Eliqn\projects\He2\dist\commands\general\information.Js
require() of ES modules is not supported.
require() of C:\Users\Eliqn\projects\He2\dist\commands\general\information.Js from C:\Users\Eliqn\projects\He2\node_modules\require-all\index.Js is an ES module file as it is a .Js file whose nearest parent package.Json contains "type": "module" which defines all .Js files in that package scope as ES modules.
Instead rename information.Js to end in .cJs,change the requiring code to use import(),or remove "type": "module" from C:\Users\Eliqn\projects\He2\package.Json.
@H_674_6@

information.Js@H_674_6@:

export default class information extends Command {
    /* ... */
}
@H_674_6@

information.Js@H_674_6@ 正在与 .registerCommandsIn(join(dirname(fileURLtopath(import.Meta.url)),'commands'));@H_674_6@ 一起导入。所以我尝试了两种解决方案:如果我按照消息的建议删除 "type": "module"@H_674_6@,我会得到 SyntaxError: CAnnot use import statement outsIDe a module@H_674_6@,如果我将扩展名更改为 .cJs@H_674_6@,它最终会加载文件,但我没有不明白为什么这是必要的,也不知道如何将特定文件输出为 .cJs@H_674_6@ 而不是 .Js@H_674_6@。尽管如此,该命令仍未正确注册。

如果有用,这是我的tsconfig.Json@H_674_6@:

{
    "compilerOptions": {
        "allowJs": true,"allowSyntheticDefaultimports": true,"esModuleInterop": true,"module": "ES2020","moduleResolution": "Node","noImplicitAny": true,"noImplicitThis": true,"Strictnullchecks": true,"outDir": "dist","target": "ES2020"
    }
}
@H_674_6@

我认为应该有一种更直接的方法来做到这一点,但我发现模块非常混乱。

解决方法

您在代码中使用 ESM 有什么原因吗?你可以像这样配置 TypeScript 来使用 CommonJS 模块:

{
    "compilerOptions": {
        "allowJs": true,"allowSyntheticDefaultImports": true,"esModuleInterop": true,"module": "CommonJS",// CommonJS
        "moduleResolution": "Node","noImplicitAny": true,"noImplicitThis": true,"StrictNullchecks": true,"outDir": "dist","target": "ES2020"
    }
}
@H_674_6@

那应该会消除错误。

大佬总结

以上是大佬教程为你收集整理的导入 CommonJS 默认导出为命名导出/无法加载 ES 模块全部内容,希望文章能够帮你解决导入 CommonJS 默认导出为命名导出/无法加载 ES 模块所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。