Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
CLI for todo App - help debugging current solution using Commander or change approach to Readline and Event Emitter?

I am trying to build a CLI for a node js only todo app using commander and conf modules in node js, with chalk to colour the output . I am not sure how to resolve the errors being returned:

ReferenceError: require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a '.js' file extension contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

I'm getting the above error for both conf and commander

Any suggestions on how I could go about debugging this, or changing approach to using readline and events/EventEmitter would be better, will be appreciated, Thanks

Below is a REDACTED version of code:

list.js

const conf = new (require('conf'))();
const chalk = require('chalk');

function list() {
  const todoList = conf.get('todo-list');
  if (todoList && todoList.length) {
    console.log(
      chalk.blue.bold(
        'Tasks in green are done. Tasks in yellow are still not done.'
      )  
     }
    }

module.exports = list;

index.js file

const { program } = require('commander');
const list = require('./list');
program.command('list').description('List all the TODO tasks').action(list);
program.command('add <task>').description('Add a new TODO task').action(add);
program.parse();

package.json file

{
  "main": "index.js",
  "type": "module",
  "keywords": [],
  "dependencies": {
    "chalk": "^5.0.0",
    "chalk-cli": "^5.0.0",
    "commander": "^8.3.0",
    "conf": "^10.1.1"
  },
  "bin": {
    "todos": "index.js"
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In your package.json you have:

"type": "module",

This means files with the .js suffix are assumed to be ECMAScript rather than CommonJS. If you want to use CommonJS you can change the file suffix or change the "type" property.

Or you can use the new syntax. In ECMAScript you use import, in CommonJS you use require.

To read more about "type" see: https://nodejs.org/dist/latest-v16.x/docs/api/packages.html#determining-module-system

about 4 years ago · Juan Pablo Isaza Relatório

0

After some more research I found out I was 'muddying the waters' between CJS or ESM modules. CJS modules use require and that is the old way of doing things prior to ES6 modules ESM modules use import

My package.json says type: module telling NodeJS that I am using ESM. But the code is saying CJS.

These are the steps I take to fix this:

  • rename index.js to index.mjs
  • update package.json accordingly
  • replace all require calls with import statements
  • replace module.exports = list with default export = list (or used a named export)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda