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' when using webpack #2799

Closed
sayav opened this issue Feb 20, 2022 · 3 comments
Closed

Cannot find module '../package.json' when using webpack #2799

sayav opened this issue Feb 20, 2022 · 3 comments

Comments

@sayav
Copy link

sayav commented Feb 20, 2022

Describe the bug
When building with webpack, the function Utils.getORMVersion throws an error as it is unable to import package.json. This used to work in 4.x when require('../package.json') was used instead of Utils.requireFrom. The bug occurs in 5.0.0, 5.0.1 and 5.0.2. The code runs fine when using ts-node.

Also another issue is with ConfigurationLoader.getPackageConfig which has the same problem as it recursively looks for package.json files and cannot import them. When deploying with webpack this won't be an issue as there would be no local package.json file but is an issue for those using webpack in development.

Stack trace

(node:6003) UnhandledPromiseRejectionWarning: Error: Cannot find module '../package.json'
    at webpackEmptyContext (/Users/agni/development/temp/bundle.js:2:324247)
    at /Users/agni/development/temp/bundle.js:2:320610
(Use `node --trace-warnings ...` to show where the warning was created)
(node:6003) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:6003) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

To Reproduce
Repo - https://github.com/sayav/mikro-orm-webpack-issue

yarn start runs using ts-node

yarn webpack creates bundle.js (when running with webpack, it's a good idea to copy bundle.js to a different location with no package.json files to avoid the second bug). Replacing return this.requireFrom('../package.json', __dirname).version with require('../package.json') in Utils.getORMVersion solves the issue.

Expected behavior
No crash

Versions

Dependency Version
node 14.18.2
typescript 4.5.5
mikro-orm 5.x
your-driver mysql
@B4nan
Copy link
Member

B4nan commented Feb 20, 2022

Thanks for the repro. I kinda hate webpack for this, but what can we do. Note that just changing it back to require in the bundle does not help, you need to rebuild it, in other words, this is not about webpack not liking createRequire, but rather about not bundling the package.json so it can't be required.

I guess I will revert back to the require calls, I hope it won't break something else like ESM support (but given its a known path, it should work fine).

I don't understand how getPackageConfig can have the same problem, as that should never throw if the file is not found (and is not using createRequire like the former). It even first checks if the file exists.

edit: I see, the file is there, so the check says let's require, but its not in the webpack bundle. But this one sounds like something you will need to get around by reconfiguring webpack. I can offer moving the MIKRO_ORM_ALLOW_VERSION_MISMATCH env var check above, so it's not triggered at all if you use it.

@sayav
Copy link
Author

sayav commented Feb 20, 2022

Thanks for the reply! So I was actually changing the code in node_modules not the bundle, and then rebuilding which seemed to do the trick.

Moving MIKRO_ORM_ALLOW_VERSION_MISMATCH would definitely help. I'll try and see if I can bundle my package.json to fix this one.

Also, I agree that webpack is a bit annoying.

@B4nan
Copy link
Member

B4nan commented Feb 20, 2022

FYI I ended up doing both, moving the MIKRO_ORM_ALLOW_VERSION_MISMATCH check as well as adding try/catch to the getPackageConfig helper, let's stay safe.

@B4nan B4nan closed this as completed in 6cfb526 Feb 20, 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