Albert Johanson
Albert Johanson

Reputation: 1

npx sequelize-cli db:migrate ERROR: Cannot use import statement outside a module

Recently, I've been working on a project with Node.js, TypeScript, Sequelize, and PostgreSQL. I want to make some migrations to add data just to test the endpoints.

But when I run the following command:

npx sequelize-cli db:migrate

I get this error:

ERROR: Cannot use import statement outside a module

Does anyone know why I'm getting this error? Below, I'll leave the tsconfig.json, the migration file, the config.json, and also the .sequelizerc:

tsconfig.json:

{
  "compilerOptions": {
    "lib": ["ESNext", "DOM"],
    "target": "ESNext",
    "module": "ESNext",
    "moduleDetection": "force",
    "jsx": "react-jsx",
    "allowJs": true,
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "verbatimModuleSyntax": true,
    "noEmit": true,
    "strict": true,
    "skipLibCheck": true,
    "noFallthroughCasesInSwitch": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noPropertyAccessFromIndexSignature": false
  }
}

config.json:

{
  "development": {
    "username": "your-username",
    "password": "your-password",
    "database": "your-database",
    "host": "your-host",
    "dialect": "postgres"
  },
  "test": {
    "username": "your-username",
    "password": "your-password",
    "database": "your-database",
    "host": "your-host",
    "dialect": "postgres"
  },
  "production": {
    "username": "your-username",
    "password": "your-password",
    "database": "your-database",
    "host": "your-host",
    "dialect": "postgres"
  }
}

Migration File (xxxxxxxxxxxxxxxx-add-events-table.ts):

import type { QueryInterface } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.bulkInsert('Events', [
    {
      title: 'Concert at Central Park',
      description: 'An amazing concert in the heart of NYC.',
      ubicacionId: 1,
      createdAt: new Date(),
      updatedAt: new Date(),
    },
    {
      title: 'Art Exhibition in Paris',
      description: 'Explore the finest art at the Louvre.',
      ubicacionId: 2,
      createdAt: new Date(),
      updatedAt: new Date(),
    },
    {
      title: 'Sydney Opera House Tour',
      description: 'A guided tour of the iconic Sydney Opera House.',
      ubicacionId: 3,
      createdAt: new Date(),
      updatedAt: new Date(),
    },
  ]);
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.bulkDelete('Events', {
    title: [
      'Concert at Central Park',
      'Art Exhibition in Paris',
      'Sydney Opera House Tour',
    ],
  });
}

.sequelizerc:

const path = require('path');

module.exports = {
  'config': path.resolve('src', 'config', 'config.json'),
  'migrations-path': path.resolve('src', 'migrations'),
  'models-path': path.resolve('src', 'models'),
  'seeders-path': path.resolve('src', 'seeders')
};

My Question:

How can I resolve the "Cannot use import statement outside a module" error when running Sequelize migrations with TypeScript? Is there a better way to handle TypeScript migrations in a project that uses Sequelize and PostgreSQL?

Any help would be greatly appreciated!

Upvotes: 0

Views: 40

Answers (0)

Related Questions