0

无法在生产中迁移和运行种子。knex 尝试运行 ts 文件但失败。控制台错误:

Cannot use import statement outside a moduleD:\web-projects\product-feedback\dist\src\db\migrations\20211215210852_initial.d.ts:1
import { Knex } from 'knex';
^^^^^^

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 importFile (D:\web-projects\product-feedback\node_modules\knex\lib\migrations\util\import-file.js:12:7)
    at async D:\web-projects\product-feedback\node_modules\knex\lib\migrations\migrate\Migrator.js:87:15
    at async Promise.all (index 0)
4

1 回答 1

0

编辑您的 knex 配置选项以仅在生产中运行 .js 文件。您可以使用 loadExtensions: ['.js'] 来实现这一点,见下文:

production: {
        client: 'pg',
        connection: pgConnection,
        pool: {
            min: 2,
            max: 10,
        },
        migrations: {
            directory: './src/db/migrations',
            loadExtensions: ['.js'],
        },
        seeds: {
            directory: './src/db/seeds',
            loadExtensions: ['.js'],
        },
    },

在此处输入图像描述

于 2021-12-22T12:10:24.380 回答