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

The migration:check command exits with ERR_IMPORT_ASSERTION_TYPE_MISSING #5497

Closed
5 tasks done
piotr-cz opened this issue Apr 22, 2024 · 3 comments
Closed
5 tasks done

Comments

@piotr-cz
Copy link
Contributor

piotr-cz commented Apr 22, 2024

Describe the bug

Affects only 6.2.2, both Windows and Linux
Affects only commands and only related to snapshots (not programmatic usage)

It looks like that in 7e8409b the support to require json files have been removed together with a jest workaround.

Partially reverting commit fixes the issue

static async dynamicImport(id) {
    if (id.endsWith('.json')) {
        return require(id);
    }

    //...
}

Context

The dynamicImport method by default uses config's

dynamicImportProvider: /* istanbul ignore next */ (id: string) => import(id),

which produces the error.

Node's dynamic import() for json files should be used along with

Another thing is that the json file is imported in the default namespace.

As as result, such fix is possible:

async dynamicImportProvider(id) {
  if (id.endsWith('.json')) {
    const { default: data } = await import(id, { assert: { type: 'json' }, with: { type: 'json' } })

    return data
  }

  return import(id)
}

Reproduction

Prepare project as in docs > Creating a new project

Execute

npx mikro-orm-esm migration:check

See output

Check if migrations are needed. Useful for bash scripts.
...

TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///xxx/src/migrations/.snapshot.json" needs an import assertion of type "json"
    at new NodeError (node:internal/errors:405:5)
    at validateAssertions (node:internal/modules/esm/assert:95:15)
    at defaultLoad (node:internal/modules/esm/load:91:3)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at xxx\node_modules\ts-node\src\esm.ts:255:45
    at async addShortCircuitFlag (xxx\node_modules\ts-node\src\esm.ts:409:15)
    at async nextLoad (node:internal/modules/esm/loader:163:22)
    at async ESMLoader.load (node:internal/modules/esm/loader:603:20)
    at async ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:11)
    at async link (node:internal/modules/esm/module_job:68:21) {
  code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING'
}

What driver are you using?

@mikro-orm/sqlite

MikroORM version

6.2.2

Node.js version

18.18.2

Operating system

Windows

Validations

@B4nan
Copy link
Member

B4nan commented Apr 22, 2024

I knew this would break at least something. Good old require, I miss you.

@B4nan
Copy link
Member

B4nan commented Apr 22, 2024

I think I will just replace the import call with a simple readJSONSync from fs-extra.

@B4nan B4nan closed this as completed in 096dcee Apr 22, 2024
@piotr-cz
Copy link
Contributor Author

Yes, the readFileSync is probably best fit for this case

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

2 participants