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

Unable to register ts-node when the baseUrl compiler option is not set #4679

Closed
ianyong opened this issue Sep 7, 2023 · 0 comments · Fixed by #4680
Closed

Unable to register ts-node when the baseUrl compiler option is not set #4679

ianyong opened this issue Sep 7, 2023 · 0 comments · Fixed by #4680

Comments

@ianyong
Copy link
Contributor

ianyong commented Sep 7, 2023

Describe the bug

Since TypeScript 4.1, the paths compiler option can be used without the baseUrl compiler option. However, the ConfigurationLoader assumes that the baseUrl option is set when the paths option is set when MikroORM's useTsNode flag in package.json is set to true:

const { options } = tsNode.register({
project: tsConfigPath,
transpileOnly: true,
compilerOptions: {
module: 'commonjs',
},
}).config;
if (Object.entries(options?.paths ?? {}).length > 0) {
Utils.requireFrom('tsconfig-paths', tsConfigPath).register({
baseUrl: options.baseUrl,
paths: options.paths,
});
}

In the above code snippet, the register function from tsconfig-paths expects baseUrl to be a string. When the baseUrl compiler option is not set, the error below occurs.

Stack trace

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:399:5)
    at validateString (node:internal/validators:163:11)
    at Object.isAbsolute (node:path:1157:5)
    at configLoader (/Users/ianyong/<redacted>/node_modules/.pnpm/tsconfig-paths@4.1.0/node_modules/tsconfig-paths/src/config-loader.ts:50:34)
    at Object.register (/Users/ianyong/<redacted>/node_modules/.pnpm/tsconfig-paths@4.1.0/node_modules/tsconfig-paths/src/register.ts:79:42)
    at Function.registerTsNode (/Users/ianyong/<redacted>/node_modules/.pnpm/@mikro-orm+core@5.7.12_@mikro-orm+migrations@5.7.12_@mikro-orm+postgresql@5.7.12_@mikro-orm+seeder@5.7.12/node_modules/@mikro-orm/core/utils/ConfigurationLoader.js:118:71)
    at Function.configure (/Users/ianyong/<redacted>/node_modules/.pnpm/@mikro-orm+cli@5.7.12_@mikro-orm+migrations@5.7.12_@mikro-orm+postgresql@5.7.12_@mikro-orm+seeder@5.7.12_pg@8.11.0/node_modules/@mikro-orm/cli/CLIConfigurator.js:29:61)
    at async /Users/ianyong/<redacted>/node_modules/.pnpm/@mikro-orm+cli@5.7.12_@mikro-orm+migrations@5.7.12_@mikro-orm+postgresql@5.7.12_@mikro-orm+seeder@5.7.12_pg@8.11.0/node_modules/@mikro-orm/cli/cli.js:13:18 {
  code: 'ERR_INVALID_ARG_TYPE'
}

To Reproduce
Steps to reproduce the behavior:

  1. Enable the useTsNode flag in package.json.
  2. Set the paths compiler option but not the baseUrl compiler option in tsconfig.json.
  3. Run any command that makes use of ts-node, such as mikro-orm migration:fresh.

Expected behavior

When baseUrl is not specified, it should default to '.' instead of throwing an error.

Additional context

The expected behaviour described above is effectively the same approach taken by the tsconfig-paths library itself when reading directly from a tsconfig.json file which does not have the baseUrl compiler option specified (dividab/tsconfig-paths#208). The reason why I believe this issue should be fixed within MikroORM and not upstream is because in this case, MikroORM is not respecting the interface of the register method by passing in an argument of type string | undefined when string is expected.

Versions

Dependency Version
node 18.16.0
typescript 5.0.3
mikro-orm 5.7.12
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 a pull request may close this issue.

1 participant