sindresorhus / p-map

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

`Must use import to load ES Module` (with typescript) #44

Closed omar-dulaimi closed 3 years ago

omar-dulaimi commented 3 years ago

Hello,

How to use this library with typescript? I'm getting this error:

Must use import to load ES Module: /home/omar/Desktop/node_modules/p-map/index.js\n' +
    'require() of ES modules is not supported.\n' +
    'require() of /home/omar/Desktop/node_modules/p-map/index.js from /home/omar/Desktop/src/resolvers/views.ts 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.\n' +
    'Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/omar/Desktop/node_modules/p-map/package.json.

Here's my tsconfig:

{
    "compilerOptions": {
        "target": "ES2018",
        "module": "CommonJS",
        "lib": [
            "es2020",
            "esnext.asynciterable"
        ],
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "esModuleInterop": true,
        "outDir": "dist",
        "baseUrl": "src",
        "sourceMap": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "moduleResolution": "node",
        "removeComments": true,
    },
    "include": [
        "src/**/*.ts",
        "prisma/**/*.ts",
    ],
    "exclude": [
        "node_modules/**/*",
        "_warmup/**/*",
        ".vscode/**/*",
        ".husky/**/*",
        "dist/**/*"
    ],
}
sindresorhus commented 3 years ago

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

omar-dulaimi commented 3 years ago

@sindresorhus Well I have already checked that, and already using import syntax and tried a dynamic import but nothing worked.

I had to revert back to v4 until this issue is fixed.

dzcpy commented 2 years ago

So, how can we fix this? I'm using it with NestJS, changing package.json and tsconfig.json will break other things. Can't we support require for compatibility? Like for example import pMap from 'p-map/legacy'?

dzcpy commented 2 years ago

Well, I ended up reimplementing p-map in a util function... export const pMap = <T>(array: any[], func: (...args: any[]) => Promise<T>): Promise<T[]> => Promise.all(array.map(func)); It's a bit annoying when you can't use the most current version of a nice module in your project

d-513 commented 2 years ago

Ah, all modules from this author seem to have ESM now. This is annoying for anyone using Babel or Typescript or someone sticking with CommonJS. You should just keep this lib at CommonJS to be honest - since that can be importd from ESM aswell. Sad

b5414 commented 2 years ago

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