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
async import
of typescript file doesn't work from cjs
#184
Comments
Thanks for reporting. Minimal reproduction: https://stackblitz.com/edit/stackblitz-starters-3fj5rs?file=package.json It seems transformation is not happening because the file has an explicit I think we can also enable jiti transforms when in cjs, dynamic import syntax is detected but it adds overhead for all current |
How would I enable the flag in that case, given that the code looks like this? Note that this code is triggered by Nuxt when it loads the Sentry module (and uses CJS export by default). |
You need an explicit jiti instance in place of |
By "stub" do you mean the mode used during development only? Because that's not the case here. Please have a look at (Sorry if I'm saying obvious things. Just clarifying in case I misunderstood). With all that said, I'm contemplating just forcing diff --git a/package.json b/package.json
index 3fcd235..733a350 100755
--- a/package.json
+++ b/package.json
@@ -17,13 +17,7 @@
"registry": "https://registry.npmjs.org"
},
"type": "module",
- "exports": {
- ".": {
- "import": "./dist/module.mjs",
- "require": "./dist/module.cjs"
- }
- },
- "main": "./dist/module.cjs",
+ "main": "./dist/module.mjs",
"types": "./dist/module.d.ts",
"files": [
"dist" which I feel like would be perfectly fine. At least for versions of Nuxt that use |
Yep, I got it. Regardless I think it would be always much much safer if in this line you expect Would love to hear your feedback about doing it :) |
I've tried quickly and that seems to work too. |
If nuxt uses MJS/Native ESM, i think this issue is irrelevant no? Because mainly we want typescript support from what i understand and it is possible when jiti transform happens. (we do transform dynamic import in native ESM files when passed to jiti only because we have do transform anyway as currently jiti is based on CJS. Is is a coincidence that works with ESM!) Depending on Nuxt behavior that might use jiti implicitly for ESM files, is not sure assumption and can be broken in the future. (update: reading above seems confusing explanation haha feel free to DM me if you have questions!) |
If I'd want to do everything by the book then how would I initialize
const jiti = require("jiti")(__filename); which would technically work when running in Nuxt since it seems to "polyfill" that but if I'd assume that I'm running in a native ESM context then it wouldn't be available. |
This should work in ESM context (PR welcome for docs!) import initJiti from 'jiti'
const jiti = initJiti(import.meta.url)
console.log(jiti.resolve('.')) |
Environment
Node 18
jiti 1.21.0
Reproduction
test.cjs
typescript.ts
npx jiti@latest test.cjs
Describe the bug
Trying to import *.ts file using
async import
fromcjs
file fails with:It works if the
test.cjs
file is renamed totest.mjs
Additional context
Note that the actual case where I've noticed it is a lot less contrived. I found this issue by trying to configure
@nuxtjs/sentry
using a file that the module needed to import.So I've set the
serverConfig: '~/config/sentry.server'
option which the Sentry module then tries to import usingasync import
. The issue is that that sentry module is imported through the CJS export rather than MJS so then it fails. (note how exports are defined in the module: https://github.com/nuxt-community/sentry-module/blob/ad424f10763d5c08826e250f715ebde699d4edfa/package.json#L19-L27).Logs
No response
The text was updated successfully, but these errors were encountered: