【问题标题】:I have this error : ERROR: Cannot use import statement outside a module我有这个错误:错误:不能在模块外使用导入语句
【发布时间】:2021-10-31 20:02:39
【问题描述】:

我需要执行种子文件,当我执行该文件时会出现此错误。我该如何解决?

错误:不能在模块外使用 import 语句

这是我的tsconfig.js

{
  "compilerOptions": {
      "module": "commonjs",
      "target": "es2019",
      "lib": [
          "dom",
          "es2019"
      ],
    //   "baseUrl": "./src",
      "moduleResolution": "node",
      "outDir": "dist",
      "sourceMap": true,
      "esModuleInterop": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "allowSyntheticDefaultImports": true,
      "typeRoots": [
          "./types",
          "./node_modules/@types"
      ],
      
  },  
  "include": [
      "src/**/*"
  ],
  "exclude": [
      "node_modules",
      ".vscode"
  ]
}

这是我的种子:

'use strict';
import { Player } from "../../app/models/player";

module.exports = {
  up: async (queryInterface, Sequelize) => {
      return Player.bulkCreate(
        [
          { name: "Inbal", email: "inbal@test.co.il",phone: "1111" },
          { name: "Tal", email: "tal@test.co.il",phone: "2222" },
          { name: "Sivan", email: "sivan@test.co.il" ,phone: "3333"}
        ],
        { hooks: true, individualHooks: true, validate: true }
      );
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.bulkDelete("players", null, {});
  }
};

当我运行npx sequelize-cli-ts db:seed:all 时出现错误。

【问题讨论】:

    标签: node.js typescript import


    【解决方案1】:

    要么使用require 而不是在你的种子文件中导入,或者你也可以尝试设置 "type": "module" 在你的package.json

    【讨论】:

      猜你喜欢
      • 2021-02-06
      • 2022-10-23
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 2022-02-12
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      相关资源
      最近更新 更多