react-easy-crop: Cannot use import statement outside a module
Describe the bug
I’m getting Cannot use import statement outside a module when building it for prod, dev just works fine. I’ve type: module in my package.json and here’s my tsconfig
"esModuleInterop": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2022",
And when importing Cropper like this import Cropper from 'react-easy-crop' gives this error when using it
JSX element type 'Cropper' does not have any construct or call signatures.ts(2604)
This project did build successfully before with these exact configs but after adding react-easy-crop, it doesn’t
Full error:
(node:406) 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
/myapp/node_modules/react-easy-crop/index.module.js:1
import { __assign, __extends } from 'tslib';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Expected behavior
It should build successfully with these configs and shouldn’t throw an error when using the Cropper component
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 4
- Comments: 30 (13 by maintainers)
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 https://publint.dev/react-easy-crop@5.0.6 and here https://arethetypeswrong.github.io/?p=react-easy-crop%405.0.6
./index.module.js is written in ESM, but is interpreted as CJS. Consider using the .mjs extension, e.g. ./index.module.mjs ([More info](https://publint.dev/rules.html#file_invalid_format)) The types is not exported. Consider adding pkg.exports["."].import.types to be compatible with TypeScript's "moduleResolution": "bundler" compiler option. Note that you cannot use "./index.d.ts" because it has a mismatching format. Instead, you can duplicate the file and use the .mts extension, e.g. pkg.exports["."].import.types: "./index.d.mts" ([More info](https://publint.dev/rules.html#types_not_exported))[Resolution failed](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/NoResolution.md) Import failed to resolve to type declarations or JavaScript files.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):
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
I’m getting this in Remix as well.
Edit: This fixed for me: https://github.com/ValentinH/react-easy-crop/issues/490#issuecomment-2034722747
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. @lww’s suggestion is correct here,
.mjsextension should be used instead of.jsnvm, sorry, I tried the canary
5.0.7--canary.527.82deecf.0withssr.noExternalin the vite config. Withoutssr.noExternalin the vite config, it fails on start. Failing withreact-easy-crop@5.0.7--canary.527.1aad632.0too 😞 Sorry dude, I know it’s a hard problem. Thanks for trying to figure this out, I don’t have anything else to suggest here.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 react-easy-crop@5.0.7–canary.527.82deecf.0 please?
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.
Could you please try
5.0.7--canary.2940c18.0and let me know if it solves your issues? (I’m not really confident that it does)Ah, I looked at the wrong dist file.
temp fix with patch-package:
Essentially what is changes is that I added
type: moduleto the package.json and then removed the import from tslib inindex.module.jsand declared the relevant functions in the file itself. This fix works for me and my use case, I HAVEN’T tested every function and every way you can usereact-easy-crop, so test this solution for your use case@ValentinH Thanks for looking into this Here’s minimal reproducing example, clone it, run
cp .env.example .env, runnpm run setupand thennpm run devto run the dev server. Everything will run with flying colors. You can go tolocalhost:3000to see the app running locally, you can log in withkody/kodylovesyou(username/password) and then go tolocalhost:3000/settings/profile/photo, then clickChangebutton to change the image and select any image then you’ll see theCroppercomponent to crop the picture that you’ve selected, you can click thesavebutton and it’ll actually crop the image and save it in the db. Functionally everything works!! The only issue is when you build the app. Runnpm run buildto build the app > runnpm startto start in prod mode and then you’ll see the error on your console, this is what I get:I believe it has to do with the
module.jsextension maybe 🤷♂️