如何解决不能在 mikroorm 的模块外使用 import 语句
我将 Mikro-orm 与 postgresql 和 nodejs 一起使用,当我运行我的项目时出现此错误:不能在模块外使用导入语句。我不知道要解释发生了什么我认为问题出在 tsconfig.ts 我将 Mikro-orm 与 postgresql 和 nodejs 一起使用,当我运行我的项目时出现此错误:无法在模块外使用导入语句。我不知道要解释发生了什么我认为问题出在 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"
]
}
解决方法
我遇到了同样的问题并通过在 root 中创建一个扩展的 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"
]
}
希望能解决问题
,问题在于编译器将您的代码理解为单独的 commonscript 文件。您需要将其声明为模块文件。
将此添加到 package.json 的顶部
// package.json
{
"type": "module"
}
如 nodejs 文档中所述https://nodejs.org/docs/latest-v13.x/api/esm.html#esm_package_json_type_field
,您需要在编译为 JS 以及模式后使用迁移文件夹的正确路径更新 MikroORM 配置。
migrations: {
path: `${__dirname}/migrations`,pattern: /^[\w-]+\d+\.(js|ts)$/,}
,
按如下方式更新 tsconfig.json 中的 lib:
"lib": [
"dom","es6","es2017","esnext.asynciterable"
]
希望有效?
,我遇到了同样的问题。安装 ts-node
软件包对我有用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。