Galooshi / import-js

A tool to simplify importing JS modules
MIT License
524 stars 70 forks source link

Config file can't be loaded inside an ES Module project #587

Closed mikabytes closed 7 months ago

mikabytes commented 7 months ago

In a project with a package.json that specifies

...
 "type": "module"
...

...and using a ".importjs.js" file, then this error happens

Unable to parse configuration file. Reason:                                                                                                                   
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/mika/mikabytes/cabinroad/app/public/.importjs.js from /home/mika/.nvm/versions/node/v20.10.0/lib/node_modules/import-js/build/FileUtils.js not supported.
.importjs.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename .importjs.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/mika/mikabytes/cabinroad/app/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.readJsFile (/home/mika/.nvm/versions/node/v20.10.0/lib/node_modules/import-js/build/FileUtils.js:35:12)
    at Configuration.loadUserConfig (/home/mika/.nvm/versions/node/v20.10.0/lib/node_modules/importjs/build/Configuration.js:271:44)
    at new Configuration (/home/mika/.nvm/versions/node/v20.10.0/lib/node_modules/import-js/build/Configuration.js:209:25)
    at new Importer (/home/mika/.nvm/versions/node/v20.10.0/lib/node_modules/import-js/build/Importer.js:96:19)
    at Interface.<anonymous> (/home/mika/.nvm/versions/node/v20.10.0/lib/node_modules/import-js/build/daemon.js:65:20)

However, the proposed solution in the error message to rename the file to .importjs.cjs won't work, because importjs doesn't look for that file (or the related .importjs.mjs one).

I'll try to fix this and submit a PR shortly

lencioni commented 7 months ago

Fixed by #589