Skip to content

Conversation

bcoe
Copy link
Member

@bcoe bcoe commented Mar 16, 2025

@shadowspawn this uses the suggestion you gave in yargs adding:

export {ui as 'module.exports'};

Which seemed to make things work pretty well for both CJS and ESM:

const ui = require('cliui')()
const {Chalk} = require('chalk')
const chalk = new Chalk();
ui.div('Usage: $0 [command] [options]')

ui.div({
  text: 'Options:',
  padding: [2, 0, 1, 0]
})
import cliui from "cliui";
import chalk from "chalk";
// Deno: import cliui from "https://deno.land/x/cliui/deno.ts";

const ui = cliui({})

Thoughts? Do you think this hack (export {ui as 'module.exports'};) is reasonable, or is it going to bite us one day.

BEGIN_COMMIT_OVERRIDE
fix: make require("cliui") work as expected for CJS
END_COMMIT_OVERRIDE

@bcoe bcoe requested a review from shadowspawn March 16, 2025 15:27
@shadowspawn
Copy link
Member

The documentation is a bit unclear about whether it should be needed, but when I was looking at yargs it did seem to be needed in practice. It might be a while before I can do some research.

Reference: https://nodejs.org/docs/latest/api/modules.html#loading-ecmascript-modules-using-require

@bcoe bcoe changed the title feat: make require("cliui") work as expected for CJS fix: make require("cliui") work as expected for CJS Mar 17, 2025
@bcoe bcoe merged commit 04ccc25 into master Mar 17, 2025
5 checks passed
@bcoe bcoe deleted the export-fix branch March 17, 2025 02:26
@shadowspawn
Copy link
Member

I did some simple sanity checking. bun and deno don't currently support the (recent) variant syntax, it is a node special. But didn't cause any problems as such.

// def.cjs

const def = require('./lib.mjs');
console.log({def});
// lib.mjs 

const defaultValue = "DEFAULT";
export default defaultValue

export {defaultValue as 'module.exports'};
$  node def.cjs          
{ def: 'DEFAULT' }
$ deno run --allow-read def.cjs
{
  def: [Module: null prototype] {
    __esModule: true,
    default: "DEFAULT",
    "module.exports": "DEFAULT",
  }
}
$ bun run def.cjs       
{
  def: Module {
    default: "DEFAULT",
    "module.exports": "DEFAULT",
  },
}

@shadowspawn
Copy link
Member

There is an issue open again Deno noting the new require(esm): denoland/deno#26649

@shadowspawn
Copy link
Member

shadowspawn commented Mar 17, 2025

No problems with running an esm entry point.

// modern.mjs

import def from './lib.mjs';
console.log({def});
$ node modern.mjs 
{ def: 'DEFAULT' }
$ deno run --allow-read modern.mjs
{ def: "DEFAULT" }
$ bun run --allow-read modern.mjs
{
  def: "DEFAULT",
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants