gpt4 book ai didi

node.js - 无法在 mikroorm 中的模块外部使用 import 语句

转载 作者:行者123 更新时间:2023-12-02 02:25:38 25 4
gpt4 key购买 nike

我将 Mikro-orm 与 postgresql 和 nodejs 一起使用,当我运行我的项目时出现此错误:无法在模块外部使用 import 语句。我不知道如何解释发生了什么,我认为问题出在 tsconfig.ts 中我将 Mikro-orm 与 postgresql 和 nodejs 一起使用,当我运行我的项目时出现此错误:无法在模块之外使用 import 语句。我不知道如何解释发生了什么,我认为问题出在 tsconfig.json

错误

import { Migration } from '@mikro-orm/migrations';
^^^^^^

SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Migrator.resolve (/home/logorcehab/Documents/Dev/altevibes/altevibe-server/node_modules/@mikro-orm/migrations/Migrator.js:141:27)
at Object.customResolver (/home/logorcehab/Documents/Dev/altevibes/altevibe-server/node_modules/@mikro-orm/migrations/Migrator.js:43:44)
at Migration.migration (/home/logorcehab/Documents/Dev/altevibes/altevibe-server/node_modules/umzug/lib/migration.js:60:38)
at /home/logorcehab/Documents/Dev/altevibes/altevibe-server/node_modules/umzug/lib/migration.js:121:37
at Generator.next (<anonymous>)
at asyncGeneratorStep (/home/logorcehab/Documents/Dev/altevibes/altevibe-server/node_modules/umzug/lib/migration.js:9:103)
at _next (/home/logorcehab/Documents/Dev/altevibes/altevibe-server/node_modules/umzug/lib/migration.js:11:194)
at /home/logorcehab/Documents/Dev/altevibes/altevibe-server/node_modules/umzug/lib/migration.js:11:364
at new Promise (<anonymous>)

mikro-orm.config.ts

import { MikroORM } from "@mikro-orm/core";
import { __PROD__ } from "./constants";
import { Users } from "./entities/Users";
import path from "path"
export default {
migrations: {
tableName: 'mikro_orm_migrations', // migrations table name
path:'./src/migrations', // path to folder with migration files
pattern: /^[\w-]+\d+\.[t]s$/, // how to match migration files
transactional: true, // run each migration inside transaction
disableForeignKeys: true, // try to disable foreign_key_checks (or equivalent)
allOrNothing: true, // run all migrations in current batch in master transaction
emit: 'ts', // migration generation mode
},
entities:[Users],
dbName:'altevibes',
user:'dev_main',
password:'login@Logout64',
type:'postgresql',
debug: !__PROD__
} as Parameters<typeof MikroORM.init>[0];

index.ts

import {MikroORM } from "@mikro-orm/core"
import 'reflect-metadata'
import {__PROD__} from "./constants"
//import { Post } from "./entities/Post"
import express from 'express'
import { ApolloServer } from 'apollo-server-express'
import { buildSchema } from 'type-graphql'
//import { UsersResolver } from "./resolvers/user"
import { Users } from "./entities/Users"
import microConfig from './mikro-orm.config'
const main = async () => {
const orm = await MikroORM.init(microConfig)
await orm.getMigrator().up()
const posts = orm.em.create(Users, {username: 'mfn'})
await orm.em.persistAndFlush(posts)
}

console.log("Hello World")

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

package.json

{
"name": "altevibe-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"watch": "tsc -w",
"dev": "nodemon dist/src/index.js",
"start": "node dist/src/index.js",
"start2": "ts-node src/index.ts",
"create:migration": "mikro-orm migration:create"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@mikro-orm/cli": "^4.3.4",
"@mikro-orm/core": "^4.3.4",
"@mikro-orm/migrations": "^4.3.4",
"@mikro-orm/mongodb": "^4.3.4",
"@mikro-orm/postgresql": "^4.3.4",
"@sindresorhus/is": "^4.0.0",
"apollo-server-express": "^2.19.1",
"argon2": "^0.27.1",
"class-validator": "^0.12.2",
"express": "^4.17.1",
"graphql": "^15.4.0",
"mikro-orm": "^4.3.4",
"mongodb": "^3.6.3",
"nodemon": "^2.0.6",
"pg": "^8.5.1",
"reflect-metadata": "^0.1.13",
"run-rs": "^0.7.4",
"ts-node": "^9.1.1",
"type-graphql": "^1.1.1"
},
"mikro-orm": {
"useTsNode": false,
"configPaths": [
"./src/mikro-orm.config.ts",
"./dist/src/mikro-orm.config.js"
]
},
"devDependencies": {
"@types/express": "^4.17.9",
"typescript": "^4.1.3"
}
}

tsconfig.json

{
"compilerOptions": {
"target": "es6",
"lib": [
"es6"
],
"sourceMap": true,
"outDir": "./dist/src",
"moduleResolution": "node",
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"module": "commonjs",
"noUnusedParameters": true,
"noImplicitReturns": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"baseUrl": "."
},
"exclude": [
"node_modules"
],
"include": [
"./src/**/*.tsx",
"./src/**/*.ts"
]
}

最佳答案

我遇到了同样的问题,并通过在根目录中创建扩展的 tsconfig.mikro-orm.json 文件并添加到其中来解决它

// tsconfig.mikro-orm.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs" // this line is important
}
}

// in package.json file add
"mikro-orm": {
"useTsNode": true,
"tsConfigPath": "./tsconfig.mikro-orm.json", // and let the compiler know where the file is present.
"configPaths": [
"./src/mikro-orm.config.ts",
"./dist/mikro-orm.config.js"
]
}

希望这能解决问题

关于node.js - 无法在 mikroorm 中的模块外部使用 import 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65679578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com