dividab / tsconfig-paths

Load node modules according to tsconfig paths, in run-time or via API.
MIT License
1.82k stars 104 forks source link

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

Open volodyad opened 2 years ago

volodyad commented 2 years ago

on the branch https://github.com/volodyad/tsconfig-paths/tree/colorette-error run npm run example:error

code const { createColors } = require('colorette')

tsconfig

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "outDir": "js_out",
    "noImplicitAny": false,
    "sourceMap": false,
    "baseUrl": ".",
    "paths": {
      "*": [
        "node_modules/*"
      ],
    }
  }
}

error

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\projects\tsconfig-paths\example\error\node_modules\colorette\index.js
require() of ES modules is not supported.
require() of C:\projects\tsconfig-paths\example\error\node_modules\colorette\index.js from C:\projects\tsconfig-paths\example\error\main.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.
Instead rename index.js to end in .cjs, change the requiring code to use import(), 
or remove "type": "module" from C:\projects\tsconfig-paths\example\error\node_modules\colorette\package.json.

    at new NodeError (internal/errors.js:322:7)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1102:13)      
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at Object.<anonymous> (C:\projects\tsconfig-paths\example\error\main.ts:1:26)  
    at Module.m._compile (C:\projects\tsconfig-paths\node_modules\ts-node\src\index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)

if run this command without ./register - all works fine colorette package supports both cjs and esm style, seems tsconfig does not parse it properly