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

fix(core): detect ts-node when using esm loader #5332

Merged
merged 1 commit into from Mar 13, 2024

Conversation

Ruby184
Copy link
Contributor

@Ruby184 Ruby184 commented Mar 13, 2024

The problem

When using mikroorm in typescript ESM project and using cli, .ts config and entitiesTs are not taken into account. This is because ts-node is not detected for node > 16.20 when using ts-node/esm loader as node option https://github.com/TypeStrong/ts-node?tab=readme-ov-file#node-flags-and-other-tools (directly or using mikro-orm-esm binary).

If we have the orm script in package.json and defined configPaths:

  "scripts": {
    "orm-node-loader": "cross-env \"NODE_OPTIONS='--loader ts-node/esm --no-warnings'\" mikro-orm",
    "orm-esm-binary": "mikro-orm-esm"
  },
  "mikro-orm": {
    "configPaths": [
      "./src/db/orm.config.ts",
      "./src/db/orm.config.js"
    ]
  },

And run npm run orm-esm-binary debug or npm run orm-node-loader debug in development, you will see the message

 - searched config paths:
   - /home/ruby/workspace/server/src/db/orm.config.js (not found)
   - /home/ruby/workspace/server/dist/mikro-orm.config.js (not found)
   - /home/ruby/workspace/server/mikro-orm.config.js (not found)
- configuration not found (MikroORM config file not found in ['./src/db/orm.config.js', './dist/mikro-orm.config.js', './mikro-orm.config.js'])

For node 16 this works as ts-node is detected because it will install internal ts-node symbol in process.

Description of the solution

Adding the check for ts-node/esm in process.execArgv (args passed to nodejs runtime: https://nodejs.org/api/process.html#processexecargv) in this case --loader option is passed as nodejs option, will correctly detect ts-node and ts config is used when using cli.

Alternative solutions

  • Forcing to register ts-node using env - this works but is redundant bacause we already use loader
cross-env "NODE_OPTIONS='--loader ts-node/esm --no-warnings'" MIKRO_ORM_CLI_USE_TS_NODE=true mikro-orm
cross-env MIKRO_ORM_CLI_USE_TS_NODE=true mikro-orm-esm
  • Add useTsNode to options in package.json - not ideal bacause it will require ts-node also in production

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Add useTsNode to options in package.json - not ideal bacause it will require ts-node also in production

FYI this is a flag only for the CLI, it has no effect on your app.

Copy link

codecov bot commented Mar 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.72%. Comparing base (a081bea) to head (6699958).

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #5332    +/-   ##
========================================
  Coverage   99.72%   99.72%            
========================================
  Files         223      223            
  Lines       16973    16973            
  Branches     3940     4161   +221     
========================================
  Hits        16927    16927            
  Misses         46       46            

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@B4nan B4nan changed the title Detect ts-node when using esm loader fix(core): detect ts-node when using esm loader Mar 13, 2024
@B4nan B4nan merged commit 23cc880 into mikro-orm:master Mar 13, 2024
11 checks passed
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

Successfully merging this pull request may close these issues.

None yet

2 participants