create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

"type": "module" with React. [module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"']

Open JahnoelRondon opened this issue 4 years ago • 13 comments

Hello All. Currently I am trying to set up a backend in my react project and im running into issues trying to use ES 6 imports.

ReactModuletype

I went back and created a blank react app and added "type": "module" into my package.json and my server.js can now use ES 6 imports when I run nodemon server.js. However now When i try to npm start my react project I get the error.

Failed to compile.

Module not found: Error: Can't resolve './App' in '/home/user/Desktop/WebDev/React/temp/merntemplate/src'
Did you mean 'App.js'?
BREAKING CHANGE: The request './App' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

ERROR in ./src/index.js 8:0-24
Module not found: Error: Can't resolve './App' in '/home/user/Desktop/WebDev/React/temp/merntemplate/src'
Did you mean 'App.js'?
BREAKING CHANGE: The request './App' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

When I remove the "type": "module from my pakcage.json and run npm start the react apps works fine again however my server now cant use import and can only use require. I know there are other ways besides using module type like making my server.js to server.mjs or any file i want to use esm however I have always used type module when cloning templates but now that I am doing it on my own I am running into this issue and can't find anything on the web on how to use "type": "module" in a react project so that I dont have to name all my backend files .mjs.

JahnoelRondon avatar Jan 01 '22 19:01 JahnoelRondon

Based on this stackoverflow post, It seems like a quick fix solution would be for react-scripts to set fullySpecified to false

{
    test: /\.m?js/,
    resolve: {
      fullySpecified: false,
    },
}

This isn't a practical solution for us developers. It requires ejecting to make it work this way.

Dan503 avatar Mar 05 '22 07:03 Dan503

Can we change the title of this issue to something easier to read?

"failed to resolve only because it was resolved as fully specified"

Dan503 avatar Mar 05 '22 07:03 Dan503

See also https://github.com/webpack/webpack/issues/11467#issuecomment-691873586

This is a webpack tweak that next.js uses as well since a lot of existing npm packages which may output esm bundles don't fully qualify their imports.

transitive-bullshit avatar Apr 01 '22 13:04 transitive-bullshit

Any movement on this being tracked by the react-scripts team? This blocks vuln upgrades of react-scripts. If the fix of adding the resolve rule to the webpack config of react-scripts that might be best to unblock upgrades of this by consuming projects

akatipally avatar May 02 '22 18:05 akatipally

I solved this issue by setting "type": "module", in package.json and renaming all imported modules to ***.mjs I use react version (npm view react version 18.2.0 ) ( node -v v16.16.0)

boufoussmed avatar Nov 02 '22 18:11 boufoussmed

I solved this issue by downgrading the version of react-scripts in my package.json file.

    "react-scripts": "4.0.3",

chetanyakan avatar Dec 23 '22 09:12 chetanyakan

I solved this issue by downgrading the version of react-scripts in my package.json file.

    "react-scripts": "4.0.3",

This is probably okay but it did bring back the error: [Webpack build failing with ERR_OSSL_EVP_UNSUPPORTED] Solution: https://stackoverflow.com/questions/69394632/webpack-build-failing-with-err-ossl-evp-unsupported

mcnamara-charles avatar Jan 04 '23 03:01 mcnamara-charles

How is this closed, when the solution is to downgrade to previous version?

Jackob32 avatar Feb 06 '23 12:02 Jackob32

Newer versions of create react app or something must have fixed this because it works fine now on a project I just started making. Will have to check again. Not sure though.

JahnoelRondon avatar Feb 06 '23 22:02 JahnoelRondon

FWIW I just hit this issue with "react-scripts": "5.0.1" (while trying to add in "@react-spring/web": "^9.7.0",)

auchers avatar Feb 24 '23 19:02 auchers

This answer on Stack Overflow solved it for me, without having to eject 💪🏽

danielweinmann avatar May 10 '23 17:05 danielweinmann

I have some ancient pre-Go code using react-scripts, what I could do as a get-around with [email protected] was:

edit node_modules/react-scripts/config/webpack.config.js:

after line 418: include: paths.appSrc, insert: resolve: { fullySpecified: false, },

go run ./: yarn build now works

haraldrudell avatar Aug 04 '23 00:08 haraldrudell

I'm experiencing this issue, I have this new docker compose to run my react app and others - I wouldn't like to touch the webpack config because it would require me to eject... Is there a good solution nowadays? downgrading react-scripts doesn't look good to me folks.

jmarcoscosta2015 avatar Jul 10 '25 22:07 jmarcoscosta2015