Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations that use ESM format can't run via the migrations API #5323

Open
GuyAtFinhaven opened this issue Sep 8, 2022 · 11 comments
Open

Migrations that use ESM format can't run via the migrations API #5323

GuyAtFinhaven opened this issue Sep 8, 2022 · 11 comments

Comments

@GuyAtFinhaven
Copy link

Environment

Knex version: latest
Database + version: PostgreSQL latest
OS: linux

Bug

  1. When trying to run knex.migrate() (programmatically using migrations API NOT via CLI ) on migrations that use ES6 ESM import/export (migrations generated with -x ts) you get "Uncaught SyntaxError: Cannot use import statement outside a module" (This is not a Typescript issue but a ES6 ESM issue ). These migrations do work in the CLI.

  2. "Uncaught SyntaxError: Cannot use import statement outside a module"

Thank you

@atlanteh
Copy link

atlanteh commented Oct 2, 2022

Any solution or workaround on this?

@Naddiseo
Copy link
Contributor

Usually "Uncaught SyntaxError: Cannot use import statement outside a module" means you'll need "type": "module" in your package.json, or change your migration file extensions to .mjs

@jukkaleh-atoz
Copy link

jukkaleh-atoz commented Dec 27, 2022

Happened to stumble on this while testing migration to vitest. This was a dealbreaker so stayed with jest

Usually "Uncaught SyntaxError: Cannot use import statement outside a module" means you'll need "type": "module" in your package.json, or change your migration file extensions to .mjs

Does not work with vitest and typescript. After changing "type": "module" error changes from Cannot use import statement to unknown file extension .ts.

@Naddiseo
Copy link
Contributor

@jukkaleh-atoz if you're using ts (with inline types) AND modules, you'll need to pass everything through either babel or ts-node. I'm not sure how to do that. However, if you're using "typescript" where you aren't using the types (or using @type comments) you can change the file extension to .mjs or .js depending on you package "type"

@jukkaleh-atoz
Copy link

jukkaleh-atoz commented Dec 28, 2022

@Naddiseo transpiler worked when package.json type was not set to module. I was using vitest to with babel preset so that might have something to with it

you can change the file extension to .mjs or .js
Is not a thing that typescript project can do. Only extensions are .ts or .tsx

@cpf-hse
Copy link

cpf-hse commented Feb 3, 2023

Happened to stumble on this while testing migration to vitest. This was a dealbreaker so stayed with jest

Usually "Uncaught SyntaxError: Cannot use import statement outside a module" means you'll need "type": "module" in your package.json, or change your migration file extensions to .mjs

Does not work with vitest and typescript. After changing "type": "module" error changes from Cannot use import statement to unknown file extension .ts.

Same problem here. Now all my tests run on vitest except my db integration tests 😣

@flodlc
Copy link

flodlc commented Mar 29, 2023

I have same issues, here is an alternative with good typescript support.
I suggest to use knex as the db client and pg-mate as the migrations orchestrator:
https://github.com/flodlc/pg-mate

@Naddiseo
Copy link
Contributor

You also might be able to get away running the process with NODE_OPTIONS='--loader ts-node/esm'

For CLI migrations (not what OP wanted) I've got typescript ESM migrations working here and you might be able to adapt it to work with programmatic migrations

@javierguzman
Copy link

Another one here with the same problem. I can run migrations from the CLI, however, if I call knex migrations from the test it throws:

/blabla/20230726133056_initial_creation.ts:1 seems to be an ES Module but shipped in a CommonJS package. To fix this issue, change the file extension to .mjs or add "type": "module" in your package.json.

If I do that then the project starts breaking down. I am going to give Jest a try because I am using vitest.

@Seiikatsu
Copy link

Similar issue here: we replaced our testing framework node-tap with vitest.
All tests work without problems except our db integration tests, they all fail with:

Module /[.......]/knex/migrations/20230802111830_v1-init.ts:1 seems to be an ES Module but shipped in a CommonJS package. To fix this issue, change the file extension to .mjs or add "type": "module" in your package.json.

Has anyone found a solution yet?

@mattapet
Copy link

mattapet commented Nov 16, 2023

When running migrations programatically (as part of integration test setup) we ended up switching from knex.migrate() to simply spawning a CLI version that does the job (we use vitest)

beforeAll(async () => {
  await new Promise((resolve, reject) => {
    const p = spawn('yarn', ['knex', 'migrate:latest', '--env', 'local']);
    p.on('close', resolve);
    p.on('error', reject);
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants