【问题标题】:Error.. [ERR_REQUIRE_ESM]: Must use import to load ES Module错误.. [ERR_REQUIRE_ESM]:必须使用导入来加载 ES 模块
【发布时间】:2022-04-04 19:23:51
【问题描述】:

这些是我的代码。

index.ts

import { MikroORM } from "@mikro-orm/core"
import { __prod__ } from "./constants";
import { Post } from "./entities/Post";
import microConfig from "./mikro-orm.config";

const main = async () => {
    const orm = await MikroORM.init(microConfig);

    const post = orm.em.create(Post, {title: "my first post"});
    await orm.em.persistAndFlush(post);
    console.log("--------------sql2---------------");
    await orm.em.nativeInsert(Post, {title:"my first Post 2"});
};

main().catch((err) => {
    console.log(err);
});

mikro-orm.config.ts

import { __prod__ } from "./constants";
import { Post } from "./entities/Post";
import { MikroORM } from "@mikro-orm/core";
import path from "path";


export default {
    migrations: {
        path: path.join(__dirname, './migrations'), // path to the folder with migrations
        pattern: /^[\w-]+\d+\.[tj]s$/, // regex pattern for the migration files
    },
    entities: [Post],
    dbName: "lireddit",
    type: "postgresql",
    debug: !__prod__,
} as Parameters<typeof MikroORM.init>[0];

当我执行这个命令时,我得到了这个错误

npx mikro-orm migration:create

--> SyntaxError: Cannot use import statement outside a module

然后我尝试通过在 package.json 中添加这个来修复这个错误,但是,我得到了一个新的错误......

"type": "module",

--> [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\forgit\fullstack-practice\src\mikro-orm.config.ts

https://www.youtube.com/watch?v=I6ypD7qv3Z8 代码与本视频中的完全相同... ( ~ 32:00 )

但在我的电脑上,它不起作用..

我应该怎么做才能解决这个问题?

【问题讨论】:

  • 创建一个最小的复制案例。你的代码甚至没有编译,那为什么有 20 多行呢?首先编写“hello world”,编译并运行那个,然后从那里开始构建。

标签: node.js reactjs typescript postgresql


【解决方案1】:

可能是节点版本有问题

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2021-06-18
  • 2020-09-06
  • 2022-12-17
  • 2021-12-01
  • 1970-01-01
  • 2020-10-13
  • 2021-09-06
  • 2021-09-26
  • 2021-07-30
相关资源
最近更新 更多