“不能在模块外使用导入语句"在运行 nestjs 应用程序时在 typeorm 迁移中 [英] "Cannot use import statement outside a module" in typeorm migration when run nestjs app
问题描述
我已经创建了 nestjs 应用程序.在根应用程序文件夹中,我有以下子文件夹:
I have created the nestjs app. In the root app folder I have these subfolders:
- 分布
- 迁移
- 源代码
- 测试
迁移文件夹包含 typeorm 迁移.
使用 npm run start:dev 运行应用程序时出现此错误:
The migration folder contains typeorm migrations.
When run application with npm run start:dev I have this error:
import {MigrationInterface, QueryRunner} from "typeorm";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:891:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Module.require (internal/modules/cjs/loader.js:848:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Function.PlatformTools.load (C:\Users\dakru1\Documents\employo\employo-api\node_modules\typeorm\platform\PlatformTools.js:114:28)
at C:\Users\dakru1\Documents\employo\employo-api\node_modules\typeorm\util\DirectoryExportedClassesLoader.js:39:69
at Array.map (<anonymous>)
at Object.importClassesFromDirectories (C:\Users\dakru1\Documents\employo\employo-api\node_modules\typeorm\util\DirectoryExportedClassesLoader.js:39:10)
我了解错误消息,并且知道如何解决与应用程序代码相关的问题.
I understand the error message and I know how to fix it when it relates to application's code.
但是,我的问题是这个错误来自 typeorm 迁移文件:[app-root-folder]\migration\1587067680466-Init.ts
应用程序运行时不应使用.
However, my problem is that this error come from typeorm migration file: [app-root-folder]\migration\1587067680466-Init.ts
which shouldn't be used when application runs.
为什么 nestjs 使用迁移文件.运行 nestjs 应用程序时如何忽略迁移文件夹?
Why nestjs uses migration files. How can I ignore migration folder when running nestjs app?
推荐答案
要解决它,只需将以下代码放在您的脚本"中即可在 package.json:
to solve it just put the following code on your "scripts" at package.json:
"typeorm": "ts-node-dev ./node_modules/typeorm/cli.js",
之后,您就可以运行 typeorm 迁移:运行 :)
After that you'll be able to run your typeorm migration:run :)
这篇关于“不能在模块外使用导入语句"在运行 nestjs 应用程序时在 typeorm 迁移中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!