question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

npx create-react-app fails Must use import to load ES Module

See original GitHub issue

Describe the bug

npx create-react-app test --use-npm --template typescript failes with following error

internal/modules/cjs/loader.js:1172
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/apoorvmote/test/node_modules/is-promise/index.js
require() of ES modules is not supported.
require() of /home/apoorvmote/test/node_modules/is-promise/index.js from /home/apoorvmote/test/node_modules/run-async/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /home/apoorvmote/test/node_modules/is-promise/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/apoorvmote/test/node_modules/is-promise/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1172:13)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Module.require (internal/modules/cjs/loader.js:1042:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (/home/apoorvmote/test/node_modules/run-async/index.js:3:17)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14) {
  code: 'ERR_REQUIRE_ESM'
}

Aborting installation.
  node  has failed.

If I try to install with yarn create react-app test --template typescript then I get following error

internal/modules/cjs/loader.js:584
            if (e.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED') throw e;
                                                            ^
Error [ERR_INVALID_PACKAGE_TARGET]: Invalid "exports" main target "index.js" defined in the package config /home/apoorvmote/.config/yarn/global/node_modules/is-promise/package.json
    at resolveExportsTarget (internal/modules/cjs/loader.js:542:13)
    at resolveExportsTarget (internal/modules/cjs/loader.js:581:20)
    at applyExports (internal/modules/cjs/loader.js:455:14)
    at resolveExports (internal/modules/cjs/loader.js:508:23)
    at Function.Module._findPath (internal/modules/cjs/loader.js:632:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1001:27)
    at Function.Module._load (internal/modules/cjs/loader.js:884:27)
    at Module.require (internal/modules/cjs/loader.js:1074:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/home/apoorvmote/.config/yarn/global/node_modules/run-async/index.js:3:17) {
  code: 'ERR_INVALID_PACKAGE_TARGET'
}
error Command failed.

Did you try recovering your dependencies?

npm v 16.14.4 yarn v 1.22.4

Which terms did you search for in User Guide?

I searched exactly npx create-react-app not workin saw few old github issues.

Environment

Invalid "exports" main target "index.js" defined in the package config /home/apoorvmote/.config/yarn/global/node_modules/is-promise/package.json

Steps to reproduce

  1. brew install node@12
  2. npx create-react-app test --use-npm --template typescript
  3. yarn create react-app test --template typescript

Expected behavior

I literally created new react project today morning and deployed to production. I don’t know why suddenly its not working anymore. It doesn’t work in Ubuntu 18.04, Ubuntu 20.04 and Windows 10 Pro. Both Ubuntu running on WSL 2

Actual behavior

The error from windows 10 pro

internal/modules/cjs/loader.js:1172
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\Admin\Desktop\test\node_modules\is-promise\index.js
require() of ES modules is not supported.
require() of C:\Users\Admin\Desktop\test\node_modules\is-promise\index.js from C:\Users\Admin\Desktop\test\node_modules\run-async\index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename C:\Users\Admin\Desktop\test\node_modules\is-promise\index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\Admin\Desktop\test\node_modules\is-promise\package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1172:13)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Module.require (internal/modules/cjs/loader.js:1042:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (C:\Users\Admin\Desktop\test\node_modules\run-async\index.js:3:17)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14) {
  code: 'ERR_REQUIRE_ESM'
}

Aborting installation.
  node  has failed.

Reproducible demo

(Paste the link to an example project and exact instructions to reproduce the issue.)

npx create-react-app test --use-npm --template typescript yarn create react-app test --template typescript

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanZimcommented, Apr 25, 2020

Known bug, a dependency is broken, see https://github.com/facebook/create-react-app/issues/8896#issuecomment-619406384 for workaround.

0reactions
apoorvmotecommented, Apr 26, 2020

Issue is fixed by create-react-app people. But I was stil having issue & not able to create react app.

I am on Ubuntu 20.04 and this is what worked for me.

  • First uninstall node and yarn and reinstall again this may fix problem for most of you
  • If problem still persists then go to /home/username/.config/yarn/global/
  • You will find 3 different file or folders here
    1. node_modules
    2. package.json
    3. yarn.lock
  • then remove node_modules and yarn.lock & keep package.json with
    1. rm -rf node_modules
    2. rm yarn.lock
  • Then you are done. Just continue with npx create-react-app my-app
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error [ERR_REQUIRE_ESM]: Must use import to load ES ...
Describe the bug I'm trying to create a new React app using create-react-app and it in every way it just don't work. it...
Read more >
npx create-react-app not working "Must use import to load ES ...
Have tried solutions from other pages, using esm and listing type: modules in the package.json to no avail. Probably since this is within ......
Read more >
npx create react app Must use import to load ES Module error Code ...
install node 12.12.0 using nvm and Boom!
Read more >
npx create-react-app not working "Must use import to load ES ...
[Solved]-npx create-react-app not working "Must use import to load ES Module:"-Reactjs · Restart the machine. · Re-install Create React App · Uninstall yarn...
Read more >
Must use import to load ES Module #208 - Issuehunt
I think this is a node issue, I can't even run npx create-react-app. ... Does anyone else still have the "Must use import...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found