mikro-orm: Cannot use v5.4.0 with ts-node on Node v16

Describe the bug After upgrading to v5.4.0 I can no longer run npx mikro-orm debug (or use it in any other way) if ts-node is involved. It has something to do with how dynamic imports are handled now.

I did make it work tho by running it like this:

node_modules/.bin/ts-node-esm node_modules/@mikro-orm/cli/cli debug

also like this (when running my app, not the mikro-orm’s CLI itself):

node --inspect=0.0.0.0:9229 --loader ts-node/esm bootstrap.ts

Prior to the upgrade, it did work out of the box. Everything seems to be fine outside the ts-node’s context (e.g. when running compiled JS).

CLI

npx mikro-orm debug 
Current MikroORM CLI configuration
 - dependencies:
   - mikro-orm 5.4.0
   - node 16.17.0
   - typescript 4.6.4
 - package.json found
 - ts-node enabled
 - searched config paths:
   - /path/to/my/project/config/mikro-orm.config.override.ts (not found)
   - /path/to/my/project/config/mikro-orm.config.ts (found)
   - /path/to/my/project/config/mikro-orm.config.js (not found)
   - /path/to/my/project/mikro-orm.config.ts (not found)
   - /path/to/my/project/mikro-orm.config.js (not found)
- configuration not found (Unknown file extension ".ts" for /path/to/my/project/config/mikro-orm.config.ts)

tsconfig.json

{
    "compilerOptions": {
      "target": "es2021",                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
      "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
      "lib": ["es2021", "DOM"],                 /* Specify library files to be included in the compilation. */
      "declaration": true,                      /* Generates corresponding '.d.ts' file. */
      "sourceMap": true,                        /* Generates corresponding '.map' file. */
       "outDir": "build",                       /* Redirect output structure to the directory. */
      "strict": true,                           /* Enable all strict type-checking options. */
       "strictPropertyInitialization": false,  /* Enable strict checking of property initialization in classes. */
       "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */
       "noUnusedLocals": true,                /* Report errors on unused locals. */
       "noUnusedParameters": true,            /* Report errors on unused parameters. */
       "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
       "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */
      "moduleResolution": "node",               /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
      "rootDirs": [                             /* List of root folders whose combined content represents the structure of the project at runtime. */
      ],
      "types": [                                /* Type declaration files to be included in compilation. */
          "node",
          "mocha"
      ],
      "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
      "experimentalDecorators": true,           /* Enables experimental support for ES7 decorators. */
      "emitDecoratorMetadata": true,            /* Enables experimental support for emitting type metadata for decorators. */
      "resolveJsonModule": true,
      "forceConsistentCasingInFileNames": true
    }
  }
  

Additional context I’m not actually using ESM in my app. This app remembers NodeJS v8 😃

Versions

Dependency Version
node 16.17.0
typescript 4.6.4
mikro-orm 5.4.0
your-driver MySQL

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 18
  • Comments: 40 (25 by maintainers)

Commits related to this issue

Most upvoted comments

Ouch, I thought this is ESM only issue, totally forgot to test the CLI. WIll fix it in a moment.

Since v5.4 its possible to provide your own dynamic import implementation, so you can also do this explicitly (in CJS projects):

dynamicImportProvider: id => require(id)

(This is exactly how I am going to fix it, enforcing this when initializing the ORM from CLI context.)

I was able to work around this issue by setting NODE_OPTIONS="--loader ts-node/esm" and wound up putting it in my script definition until this issue is sorted:

"orm:migrate:create": "NODE_OPTIONS=\"--loader ts-node/esm\" mikro-orm migration:create",
"orm:migrate:up": "NODE_OPTIONS=\"--loader ts-node/esm\" mikro-orm migration:up",

By the way, I noticed this ticket was closed, but I was still running into the issue in version 5.4.1-dev.9+47fccacf from yarn.

All good, it was wrong repro with ESM project, where this still requires the some extra configuration (e.g. the ts-node/esm loader).

Looks like I managed to break it in dev.17 again 😄 Let me try once again.

edit: weird, in stackblitz it fails, locally it works?

This dev-16 build addressed my issue as well, which was with the command line, but thought I would confirm for you. I also want to quote @SpencerKaiser on thanks for the quick turn around on this.

By the way, I noticed this ticket was closed, but I was still running into the issue in version 5.4.1-dev.9+47fccacf from yarn.

Ok I see, I was testing this on the schema:create -d command, which works with the dev version, but the debug command is not. Lets reopen, will first test all the commands before releasing new version (hopefully later today).

Also as mentioned in #3468, if you struggle with using old versions, be sure to add @mikro-orm/knex package to your deps and lock the versions. Here is a working stackblitz using 5.3.1 (for all the people trying to tell me how that version somehow break - it works just fine as long as you really have all the packages on that version).

You should be using npx mikro-orm-esm debug instead in ESM projects, and AFAIK it won’t work in stackblitz, but I just tried and it works fine locally.

Actually we dont need to detect ts-node at all, it makes sense to move the fix so its executed when creating the config object, always based on the type: module in package.json.

Oh I see, the fix was only about CLI as the OP was about CLI.

We can run the same ts-node detection logic as we have in the init method and do the same automatically to support such use case.

But the workaround mentioned above will help with this too, just add dynamicImportProvider: (id) => require(id), to the ORM config.

It works fine for me with dev 13, can you create a repro?

https://stackblitz.com/edit/mikro-orm-repro-wcqjfb?file=package.json