Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ES modules interoperability in penpot-export #6

Open
roberto-red opened this issue Oct 3, 2023 · 0 comments
Open

Fix ES modules interoperability in penpot-export #6

roberto-red opened this issue Oct 3, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@roberto-red
Copy link
Contributor

roberto-red commented Oct 3, 2023

Description

Currently, a node.js project that declares "type": "module" in the package.json can't use penpot-export with a configuration file.

$ penpot-export
/Users/redradix/Taller/kaleidos-easyfest/penpot-export/packages/cli/dist/bin/index.js:16
var config = require(configFilePath);
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/redradix/Taller/kaleidos-easyfest/easyfest/frontend/penpot-export.config.js from /Users/redradix/Taller/kaleidos-easyfest/penpot-export/packages/cli/dist/bin/index.js not supported.
penpot-export.config.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 rename penpot-export.config.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 /Users/redradix/Taller/kaleidos-easyfest/easyfest/frontend/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.<anonymous> (/Users/redradix/Taller/kaleidos-easyfest/penpot-export/packages/cli/dist/bin/index.js:16:14) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v18.16.1

This is due to node.js require() interpreting that the .js file is an ES module, even though it's not.

Renaming the configuration file with the .cjs extension won't work, because penpot-export is limited to the exact name of that config. Also, actually writing an ES module in penpot-export.config.js won't work either; it's an issue with the loader.

Workaround

There's a workaround for UNIX systems: 

$ mv penpot-export.config.js penpot-export.config.cjs
$ ln -s penpot-export.config.cjs penpot-export.config.js

Expected outcome

penpot-export is able to read configuration files written as CommonJS in a ES modules project, or written as ES module in a CommonJS project.

Opportunity

Consider implementing a more flexible configuration system with cosmicconfig (used by Prettier): https://github.com/cosmiconfig/cosmiconfig

This could be an opportunity to implement a CLI option to load configuration files other that penpot-export.config.js.

@roberto-red roberto-red added enhancement New feature or request bug Something isn't working and removed enhancement New feature or request labels Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant