sindresorhus / p-map

Map over promises concurrently
MIT License
1.27k stars 58 forks source link

Must use import to load ES Module #35

Closed SSANSH closed 3 years ago

SSANSH commented 3 years ago

I bump my service which are using p-map for testing from version 4.0.0 to 5.0.0 and now I got the following issue during my test.

node version v14.16.0

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: .../node_modules/p-map/index.js require() of ES modules is not supported. require() of .../node_modules/p-map/index.js from ..../tests/contracts/step_definitions/test.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 index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from .../node_modules/p-map/package.json.

is is a breaking change not documented? if yes how I can fix then

note I use pMap using import

import pMap from 'p-map';

sindresorhus commented 3 years ago

Please read the release notes.

sreuter commented 3 years ago

@SSANSH In case you were still wondering: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

renqi1996 commented 3 years ago

Same problem. I use pMap by

const pMap = require('p=map');

Howerver, it reports like this:

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

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:

I tried to move my CommonJS project to ESM like this said https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

The problem is that some libraries that I used cannot be moved to ESM. So, What should I do?

Richienb commented 3 years ago

The problem is that some libraries that I used cannot be moved to ESM.

Importing CommonJS packages from ESM will still work.

dzcpy commented 2 years ago

@Richienb Could you please shed some light on it? I'm using NestJS which is incompetible with ESM. Is there any workaround to use p-map v5?

Richienb commented 2 years ago

@dzcpy If you want to use the package, you'll either need to discover a way to load ESM modules in NestJS or use an older version.

b5414 commented 2 years ago

const pMap = (...args)=>import('p-map').then(({default: pMap})=>pMap(...args));

dzcpy commented 2 years ago

const pMap = (...args)=>import('p-map').then(({default: pMap})=>pMap(...args));

Why do you even need p-map in the first place then :)

const pMap = (array, func) => Promise.all(array.map(func));
Richienb commented 2 years ago

p-map is most useful for its extra features like custom concurrency and for the abstraction it provides.

isaachinman commented 1 year ago

Hello all. I've created a util to convert ESM-only packages to CJS, for those who need it. This is still very much a work in progress, so please open an issue on the repository with any feedback.

This package (p-map@5.5.0) can be installed as CJS via @cjs-exporter/p-map.

art1c0 commented 1 week ago

@isaachinman fantastic! could you please make the same for p-retry please?

isaachinman commented 1 week ago

@art1c0 Sure thing, here you go.

Hilarious that this still causes friction over three years later.