brev / web-image-gen

Modern responsive web image generation and tooling.
10 stars 1 forks source link

Cannot run web-image-gen in ES module project #6

Open HriBB opened 1 month ago

HriBB commented 1 month ago

I am using latest remix which uses "type": "module" in package.json. When I run web-image-gen I get this error.

$ npm run imagegen

> imagegen
> web-image-gen generate --config ./.web-image-gen.js --verbose

file:///home/bojan/www/ropecess/node_modules/web-image-gen/dist/config.js:81
                throw new Error([`Cannot import ${configFile}:`, error.message].join(' '));
                      ^

Error: Cannot import ./.web-image-gen.js: module is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/home/bojan/www/ropecess/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at getConfig (file:///home/bojan/www/ropecess/node_modules/web-image-gen/dist/config.js:81:23)
    at async file:///home/bojan/www/ropecess/node_modules/web-image-gen/dist/cli.js:50:16

Node.js v20.15.1

Script works perfectly if I change to "type": "commonjs".

I also tried using --config .web-image-gen.cjs but it accepts only .js or .json

Maybe if I find some time, I can make a PR, but no promises.

Cool tool BTW ;)

HriBB commented 1 month ago

I was able to work around this issue by "fixing" and "unfixing" the package.json befoe and after the web-image-gen script like this:

"fix": "sed -i -e 's/\"type\": \"module\"/\"type\": \"commonjs\"/g' ./package.json",
"unfix": "sed -i -e 's/\"type\": \"commonjs\"/\"type\": \"module\"/g' ./package.json",
"web-image-gen": "npm run fix && web-image-gen generate --verbose && npm run unfix"
HriBB commented 1 month ago

The above fix is a lame workaround. Instead, I just created a PR to allow .cjs config file ;)

brev commented 4 weeks ago

Hi @HriBB, thanks for the issues and PRs. Sorry for the slow reply. I'll dive in on these next week. Thanks.

HriBB commented 3 weeks ago

No problem, take your time ;) I am testing out different approaches to responsive images and this library looks promising ;)