-
-
Notifications
You must be signed in to change notification settings - Fork 168
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 use import statement outside a module #490
Comments
Thanks for sharing this issue. However, could you please share a repository with a minimal reproducing example so that I can easily investigate? |
@ValentinH Thanks for looking into this (node:52258) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
SyntaxError: Cannot use import statement outside a module
/mre-react-easy-crop/node_modules/react-easy-crop/index.module.js:1
import { __assign, __extends } from 'tslib';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1178:20)
at Module._compile (node:internal/modules/cjs/loader:1220:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Function.Module._load (node:internal/modules/cjs/loader:960:12)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
at ModuleJob.run (node:internal/modules/esm/module_job:194:25) I believe it has to do with the |
Also, in the editor if you go to
|
temp fix with patch-package:
Essentially what is changes is that I added |
Thanks for sharing this. I'm sorry I haven't found the time to look into this so far. To be honest, these kinds of issues are pretty complex to deal with as a lib maintainer because there are so many frameworks that behave differently in the way they consume packages. I might fix it for your use case and break it for others... |
It's not just my use case, it doesn't work with Remix at all, here's a minimal application of remix and react-easy-crop: https://stackblitz.com/edit/remix-run-remix-mryppq?file=app%2Froutes%2F_index.tsx |
Thanks for submitting this minimal reproduction. I had a look and so far I don't know what should I change to have this working on Remix 🙈 |
This could be solved by providing an ESM build. |
This is already the case. |
Ah, I looked at the wrong dist file. |
You have a misconfiguration in the package.json. Please consider fixing this small bug that makes the library hard to use with some frameworks. Please have a look at the lint error/warnings for the library here
For anyone using Remix V2 with Vite you can fix this bug by adding the ssr.noExternal option in the vite.config.js (https://remix.run/docs/en/main/future/vite#esm--cjs):
|
That's great news! I like fixing bugs without wanting to 🤣 |
Oh sorry, I didn't communicate clearly. The part of this issue is fixed. The one that would give an error in the code editor is fixed, but not the terminal error when starting the app in prod mode. |
Alright I'll try this then |
Could you please try |
Just tried it, didn't fix the issue. SyntaxError: Named export '__assign' not found. The requested module 'tslib' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'tslib';
const { __assign, __extends } = pkg;
file:///Users/arpit.dalal/personal/xman/node_modules/react-easy-crop/index.module.mjs:1
import { __assign, __extends } from 'tslib';
^^^^^^^^
SyntaxError: Named export '__assign' not found. The requested module 'tslib' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'tslib';
const { __assign, __extends } = pkg;
at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
at ModuleJob.run (node:internal/modules/esm/module_job:214:5)
at ModuleLoader.import (node:internal/modules/esm/loader:329:24)
at getBuild (file:///Users/arpit.dalal/personal/xman/server/index.ts:204:4)
at file:///Users/arpit.dalal/personal/xman/server/index.ts:218:34 I tried the suggested solution in the error message by importing Thanks for working on this @ValentinH. I am not too well versed in building a package for both CJS and ESM, so I am unfortunately out of ideas. Luckily, @lww's workaround of adding |
based on the docs https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md the import should be
notice the missing m in index.d.mts |
I'm actually wondering if we should just get rid of tslib. I don't even recall why it was added in the first place and it's something I haven't seen used elsewhere. |
I tried that but it requires more changes to the index.m.ts file and the files it imports. However, I couldn't find how to do it with We need to revamp the build pipeline from scratch to have something we can control. However, the fact that the Cropper component imports some non- js files (css) makes it more tricky. Last but not least, this is the most boring and painful to get right topic. |
Actually, this works "exports": {
".": {
"import": {
"types": "./index.module.d.mts", <-- "index.module.d.mts" instead of "index.d.ts"
"default": "./index.module.mjs"
},
"require": {
"types": "./index.d.ts",
"default": "./index.js"
}
},
"./react-easy-crop.css": {
"import": "./react-easy-crop.css",
"require": "./react-easy-crop.css"
}
} The |
I tried that but https://arethetypeswrong.github.io/?p=react-easy-crop%405.0.7--canary.527.82deecf.0 is even worse. Can you try [email protected] please? |
Surprisingly, that worked. I don't see |
I'm too tired 😭 Please try It think it expects |
nvm, sorry, I tried the canary |
I'll try to move the build pipeline to plain Rollup when I get some motivation. 🤞 |
Running into the same issue. Is there anything we can help with to solve it? |
I'm stuck on this topic. Ideally, someone with good knowledge around bundling would jump in to fix this 😅 |
I'm getting this in Remix as well. Edit: This fixed for me: #490 (comment) |
Same for me while using AstroJs and react |
Describe the bug
I'm getting
Cannot use import statement outside a module
when building it for prod, dev just works fine. I'vetype: module
in my package.json and here's my tsconfigAnd when importing
Cropper
like thisimport Cropper from 'react-easy-crop'
gives this error when using itThis project did build successfully before with these exact configs but after adding
react-easy-crop
, it doesn'tFull error:
Expected behavior
It should build successfully with these configs and shouldn't throw an error when using the
Cropper
componentThe text was updated successfully, but these errors were encountered: