2022-01-30

Both Require and import not working javascript

I am trying to create a cli tool to make a todo list. For some reason I can't figure out I'm unable to use either require or import when trying to import the Chalk package for highlighting terminal outputs

here is what I have for my index.js file

#! /usr/bin/env node
const { program } = require("commander");
const list = require("./commands/list.js");

program.command("list").description("List all the TODO tasks").action(list);

program.parse();

Here is my list.js file

#! /usr/bin/env node

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."
      )
    );
    todoList.forEach((task, index) => {
      if (task.done) {
        console.log(chalk.greenBright(`${index}. ${task.text}`));
      } else {
        console.log(chalk.yellowBright(`${index}. ${task.text}`));
      }
    });
  } else {
    console.log(chalk.red.bold("You don't have any tasks yet."));
  }
}
module.exports = list;

and my package.json file

{
  "name": "near-clear-state",
  "version": "1.0.0",
  "description": "Tool to let NEAR users clear the state of their account ",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "type": "commonjs",
  "author": "Dorian",
  "license": "ISC",
  "dependencies": {
    "chalk": "^5.0.0",
    "commander": "^8.3.0",
    "conf": "^10.1.1",
    "near-api-js": "^0.44.2"
  },
  "bin": {
    "near-clear-state": "./index.js"
  }
}

when I try running anything from this cli tool I'm making I get this error if I use require

➜  near-clear-state near-clear-state --help
/Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/commands/list.js:4
const chalk = require("chalk");
              ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/node_modules/chalk/source/index.js from /Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/commands/list.js not supported.
Instead change the require of index.js in /Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/commands/list.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/commands/list.js:4:15)
    at Object.<anonymous> (/Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/index.js:3:14) {
  code: 'ERR_REQUIRE_ESM'
}

Or this error when i use import

/Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/commands/list.js:3
import { Chalk } from "chalk";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1026:15)
    at Module._compile (node:internal/modules/cjs/loader:1061:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/index.js:3:14)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)

Node.js v17.4.0

Please help me



from Recent Questions - Stack Overflow https://ift.tt/2u7NDfFeX
https://bit.ly/3GblJNq

No comments:

Post a Comment