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 find module '...\package.json' imported from ...\@mikro-orm\core\utils\Utils.js #2738

Closed
itsame-luigi opened this issue Feb 10, 2022 · 1 comment

Comments

@itsame-luigi
Copy link
Contributor

Describe the bug
Using mikro-orm@5.0.0-rc.3 with MIKRO_ORM_DYNAMIC_IMPORTS crashes on init

Stack trace

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\dev\package.json' imported from C:\dev\project\node_modules\@mikro-orm\core\utils\Utils.js
    at __node_internal_captureLargerStackTrace (node:internal/errors:464:5)
    at new NodeError (node:internal/errors:371:5)
    at finalizeResolution (node:internal/modules/esm/resolve:321:11)
    at moduleResolve (node:internal/modules/esm/resolve:756:10)
    at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:867:11)
    at Loader.resolve (node:internal/modules/esm/loader:89:40)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)
    at Loader.import (node:internal/modules/esm/loader:177:28)
    at importModuleDynamically (node:internal/modules/cjs/loader:1041:29)
    at importModuleDynamicallyWrapper (node:internal/vm/module:437:21)
    at importModuleDynamically (node:vm:381:43)
    at exports.importModuleDynamicallyCallback (node:internal/process/esm_loader:30:14)
    at eval (eval at dynamicImport (C:\dev\project\node_modules\@mikro-orm\core\utils\Utils.js:684:16), <anonymous>:3:1)
    at Function.dynamicImport (C:\dev\project\node_modules\@mikro-orm\core\utils\Utils.js:684:50)
    at Function.checkPackageVersion (C:\dev\project\node_modules\@mikro-orm\core\utils\ConfigurationLoader.js:209:49)
    at Function.init (C:\dev\project\node_modules\@mikro-orm\core\MikroORM.js:33:63)
    at null.<anonymous> (C:\dev\project\src\db\index.ts:4:35)
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

To Reproduce
Steps to reproduce the behavior:

  1. Install @mikro-orm/core@5.0.0-rc.3
  2. Set up a basic project and run MikroORM.init()

Expected behavior
No crash

Additional context

  • Running in an ES Module project with MIKRO_ORM_DYNAMIC_IMPORTS enabled.
  • Running on Windows (not sure if this is OS-specific)
  • The path being requested is not relative to @mikro-orm/core, but should be. This is likely due to the use of pathToFileURL in Utils.dynamicImport, which will convert a relative path like "../package.json" to an absolute path using the current working directory (rather than relative to the importing module).
  • Even if the path were correct, you cannot use dynamic imports for JSON files in NodeJS without --experimental-json-modules. To load a package.json you should continue to use require if it is available, otherwise you should use fs.readFileSync.

Versions

Dependency Version
node 16.6.2
typescript 4.5.4
mikro-orm 5.0.0-rc.3
your-driver n/a
@B4nan
Copy link
Member

B4nan commented Feb 10, 2022

The relative path is already fixed in master. I will try to use createRequire for this (we already have Utils.requireFrom() helper that does it), the docs says it should be still valid approach and we use that to check the TS version already.

@B4nan B4nan closed this as completed in f567d2d Feb 10, 2022
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