sindresorhus / p-memoize

Memoize promise-returning & async functions
MIT License
397 stars 30 forks source link

v6.0.0 - Module not found: Can't resolve 'p-memoize' in create-react-app #30

Closed cduff closed 3 years ago

cduff commented 3 years ago

I was previously using p-memoize v5.0.1 successfully in a create-react-app project. When I try upgrading to the new p-memoize v6.0.0 I get errors like:

Failed to compile.

./src/index.tsx
Module not found: Can't resolve 'p-memoize' in 'C:\Users\user\folder\my-app\src'

Steps to reproduce:

npx create-react-app my-app --template typescript
npm i p-memoize
...
import reportWebVitals from "./reportWebVitals";
import "p-memoize";

ReactDOM.render(
...
npm start

This results in an error message as above. It works if you downgrade p-memoize to v5.0.1.

sindresorhus commented 3 years ago

Please read the release notes.

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

I would probably wait upgrading until TS 4.5 is out, which has better ESM support.

cduff commented 3 years ago

Thanks for your response @sindresorhus.

p-memoize moved to pure ESM at v5.0.0. This, as well as the subsequent v5.0.1, were working with create-react-app. So I was therefore confused when v6.0.0 stopped working.

Was there some additional change made in v6.0.0 that doesn't work with the ESM support of CRA? I couldn't see anything mentioned in the release notes.

sindresorhus commented 3 years ago

Hmm. The package was rewritten in TS, but that should not have been an observable change. Anyway, I don't think it's worth looking into this as I said, TS will improve its ESM compatibility in 4.5 which will be out soon.

cduff commented 3 years ago

FYI @sindresorhus I just had a bit more of a look at this.

The problem is caused by a change in package.json from

"exports": "./index.js",

to

"exports": "./dist/index.js",

i.e. adding a subpath to exports breaks it. I tested hacking the package to remove the subpath and it works again.

I went looking for potentially associated outstanding issues against create-react-app and found the following:

https://github.com/facebook/create-react-app/issues/10933 https://github.com/facebook/create-react-app/issues/10892 https://github.com/facebook/create-react-app/issues/11528

So it seems there are known issues with create-react-app around this.

cduff commented 2 years ago

FYI, the latest version of p-memoize (6.0.1) now works with the newly released version of create-react-app (5.0.0).