sindresorhus / p-limit

Run multiple promise-returning & async functions with limited concurrency
MIT License
1.84k stars 99 forks source link

Module not found: Can't resolve '#async_hooks' #72

Closed alaindeurveilher closed 8 months ago

alaindeurveilher commented 8 months ago

Hi,

Since the update of v5.0.0 (updating from 4.0.0) I now get the following error:

⨯ ./node_modules/p-limit/index.js:2:0
Module not found: Can't resolve '#async_hooks'

My environment is NextJS 14, Node JS 20.8.0

I did not have the issue with pLimit v4.0.0

I see in the diff between 4.0.0 and 5.0.0 a new line for the usage of import {AsyncResource} from '#async_hooks';

so the error is definitely coming from this update.

I also read a warning on the NodeJS webpage version 21.1 about the usage of async_hook: source: https://nodejs.org/api/async_hooks.html

Async hooks Stability: 1 - Experimental. Please migrate away from this API, if you can. We do not recommend using the createHook, AsyncHook, and executionAsyncResource APIs as they have usability issues, safety risks, and performance implications. Async context tracking use cases are better served by the stable AsyncLocalStorage API. If you have a use case for createHook, AsyncHook, or executionAsyncResource beyond the context tracking need solved by AsyncLocalStorage or diagnostics data currently provided by Diagnostics Channel, please open an issue at https://github.com/nodejs/node/issues describing your use case so we can create a more purpose-focused API.

Is it safe to be used then? Is this API compatible with node 20 and below ?

Thank you.

sindresorhus commented 8 months ago

This is a problem with Next.js. This package is just using sub-path imports. Open an issue on Next.js instead.

alaindeurveilher commented 8 months ago

Issue created on Next.js: https://github.com/vercel/next.js/issues/58052

andrmoel commented 8 months ago

It is not an issue with nextjs. We use p-limit is a TypeScript backend project and face the same issue in our unit tests.

We are running on node18 What is the root cause of this?

andrmoel commented 8 months ago

I created a repo to reproduce: https://github.com/andrmoel/async-hooks-test

Description is in the README.md

sindresorhus commented 8 months ago

@andrmoel p-limit is a ESM module, so you need to use the correct TypeScript config. This has nothing specifically to do with this package.

Add "module": "node16", "moduleResolution": "node16" to your tsconfig.json. (Example)

andrmoel commented 8 months ago

@sindresorhus It doesn't work. I don't want to make my package a module just because it uses p-limit.

I can add node16 to my tsconfig but it won't change anything. PR: https://github.com/andrmoel/async-hooks-test/pull/1 Check the run: https://github.com/andrmoel/async-hooks-test/actions/runs/6769536794/job/18396108036

Please clone my repo and try yourself

sindresorhus commented 8 months ago

I don't want to make my package a module just because it uses p-limit.

Please read: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#pure-esm-package