-
Notifications
You must be signed in to change notification settings - Fork 185
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
CJS / ES Module? #137
Comments
Same error for me when trying to add nest js to a vite project |
I am new to TS. I am using typeorm, which requires this library. I cannot set tsconfig module type to esnext, it must be commonjs when I import "reflect-metadata". And that is causing other problems in my application that are very annoying for a newbie. |
I got this to work importing from a package with "type": "module" and using yarn v3 / berry: Create this patch file:
Then in your package json, add this resolutions block: "resolutions": {
"reflect-metadata": "patch:[email protected]#./path/to/your/patchfile.patch"
}, If you are using a monorepo, this has to be at the top level package.json regardless of where you are importing reflect-metadata. |
Nothing should ever need type:module; if it's ESM it'd have .mjs, so I'm not sure why vite is failing to properly recognize https://unpkg.com/browse/[email protected]/Reflect.js as CJS. It doesn't export anything, since it just mutates the global Reflect, but since that's not a requirement of CJS, this seems like a vite bug, plain and simple. |
reflect-metadata doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix. |
Since it is written as a Script, and has no syntactic or runtime dependencies on a specific module syntax, it is both a perfectly legal CJS module and a perfectly legal ES module (though NodeJS will only treat it as CJS). It can be loaded in any of the following ways:
This package obeys the mechanics of NodeJS's module resolution algorithm. If you are using a bundler that is unable to bundle this package, then it is not obeying the mechanics of NodeJS's module resolution algorithm, and thus the bug lies in the bundler. I would advise you open an issue in your bundler's repo. |
@rbuckton I agree with your view, but could you please add an |
Bundlers should be perfectly capable of figuring out the nature of the package without the |
@ljharb you can't avoid them if the framework you're using mandates them. For example, Angular warns that
While the above isn't blocking, it will still cause concerned devs to spend time looking for a cause and, hopefully, end up here. When that would be easily avoided by a couple of declarations in |
Sounds like a flaw/mistake/bug in angular then. |
Each time I see an update on this ticket I have mixed feelings (I know, no one cares 😅) The root cause is this small thing called This one change in this library is not solving the root cause yes, but solving all warnings. |
I'm using reflect-metadata in a SvelteKit project, and it's working great 👍
I have a warning at the start:
reflect-metadata doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.
Maybe this message speaks better to you?
Is it the way you distribute the package?
The text was updated successfully, but these errors were encountered: