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

Cannot use mikro-orm-cli in TypeScript with ESM and NodeNext module resolution #3485

Closed
BeiyanYunyi opened this issue Sep 11, 2022 · 10 comments

Comments

@BeiyanYunyi
Copy link

BeiyanYunyi commented Sep 11, 2022

Describe the bug

With these configuration, mikro-orm command will not work as expected.

// package.json
"type": "module",
"mikro-orm": {
  "useTsNode": true
}
// tsconfig.json
  "compilerOptions": {
    "target": "ESNext",
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
  "ts-node": { "esm": true }

For reprodution, error stack and more details, see https://github.com/lixiang810/mikro-orm-repro

@B4nan
Copy link
Member

B4nan commented Sep 11, 2022

You can see working ESM project here: https://stackblitz.com/edit/mikro-orm-esm-repro

The trick to make the CLI work is the ts-node/esm loader, otherwise you can't dynamically load a TS file. I am not aware of any way to achieve this automatically.

edit: huh the repro did not have the script for CLI, so here it is:

"mikro-orm": "NODE_OPTIONS='--loader ts-node/esm --no-warnings' mikro-orm",

Add this to your NPM scripts are use that instead of npx/yarn run the the CLI.

@B4nan B4nan closed this as completed Sep 11, 2022
@BeiyanYunyi
Copy link
Author

BeiyanYunyi commented Sep 11, 2022

huh the repro did not have the script for CLI

I'm sure that npm exec mikro-orm works without adding it to script section.

You can see working ESM project here: https://stackblitz.com/fork/mikro-orm-esm-repro

It doesn't have @mikro-orm/cli. To make my esm project work is easy, I did it before, but I still can't use the cli.

@B4nan
Copy link
Member

B4nan commented Sep 11, 2022

I'm sure that npm exec mikro-orm works without adding it to script section.

The reason for NPM script is to have the loader registered.

I see you even found out the trick with loaders yourself. Next time I would appreciate you put such information into the issue description. IMHO this is a wontfix, you already do the right thing to solve it. Just wrap it into NPM script.

@B4nan
Copy link
Member

B4nan commented Sep 11, 2022

Just to shed a bit more light into this - AFAIK the ts-node-esm works pretty much the same as the proposed workaround here, it spawns new node process with the loader. We can't do this from inside the ORM automatically (at least I am not aware of any way).

I will be definitely experimenting with this more, especially because the new getting started guide I am working on is ESM and also talks about this problem - and I would very much like to not mention it at all and have things working out of box... But I dont think they can at this point.

@B4nan
Copy link
Member

B4nan commented Sep 11, 2022

FYI open a GH discussion to verify we can't do better:

TypeStrong/ts-node#1880

@B4nan
Copy link
Member

B4nan commented Sep 11, 2022

I think we could do one thing - we could expose mikro-orm-esm script, which would automatically register the ts-node/esm loader. It sounds a bit confusing as you need it only for TS projects (and it will require ts-node to be installed), but I guess its better than describing this loader env vars and stuff.

B4nan added a commit that referenced this issue Sep 11, 2022
… loader

Using this command will allow dynamic importing of TS files from a CLI context.

This is an alternative to using `NODE_OPTIONS='--loader ts-node/esm --no-warnings'` explicitly.

Closes #3485
@alexojegu
Copy link
Contributor

I am trying ESM, but when I use mikro-orm-esm I only get:

npx -w @tattoo/server mikro-orm-esm schema:create -r
/usr/bin/env: «node --loader ts-node/esm --no-warnings»: The file or directory does not exist
/usr/bin/env: use -[v]S to pass options in shebang lines

When I use a package.json script:

"mikro-orm": "NODE_OPTIONS='--loader ts-node/esm --no-warnings' mikro-orm"

All work as expected:

npm -w @tattoo/server run  mikro-orm schema:create -- -r

> @tattoo/server@0.0.1 mikro-orm
> NODE_OPTIONS='--loader ts-node/esm --no-warnings' mikro-orm

Schema successfully created

@B4nan
Copy link
Member

B4nan commented Oct 3, 2022

Well, that sounds like npx bug, nothing I can help with. Maybe try moving the workspace argument to different place.

@alexojegu
Copy link
Contributor

After see the CLI code and how you register the ts-node/esm:

#!/usr/bin/env node --loader ts-node/esm --no-warnings

I have changed in my node_modules/@mikro-orm/cli/esm.js this:

#!/usr/bin/env node --loader ts-node/esm --no-warnings

by this (see the -S):

#!/usr/bin/env -S node --loader ts-node/esm --no-warnings

And all work as expected:

npx -w @tattoo/server mikro-orm-esm schema:create -r
Schema successfully created

@B4nan
Copy link
Member

B4nan commented Oct 3, 2022

Interesting, PR welcome 👍

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

3 participants