Marius
Marius

Reputation: 2008

Nodejs + TS CommonJS vs Module issue for Inquirer package

When i imported inquirer i have started getting errors about CommonJS and Modules errors. So this current one suggesting my to use dynamic imports const inquirer = await import('inquirer'); then i also had to change in tscondig "module": "ES2022", but then i start getting different errors.

What is the correct way of setting up Node + TS so these type of issues wont occur. Currently I'm using v18.8.0 but had similar problem with v14

app.ts

import inquirer from 'inquirer';
// const inquirer = await import('inquirer');
import puppeteer from 'puppeteer';

(async () => {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  await page.goto('https://example.com/');

  inquirer
    .prompt(['Chose'])
    .then((answers: any) => {
      // Use user feedback for... whatever!!
    })
    .catch((error: any) => {
      if (error.isTtyError) {
        // Prompt couldn't be rendered in the current environment
      } else {
        // Something else went wrong
      }
    });
})();

tsconfig.json

{
  "compilerOptions": {
    // "module": "ES2022",
    "target": "ES2022",
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "noImplicitAny": true,
    "alwaysStrict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "removeComments": true
  },
  "include": ["./src/**/*", "./src/**/*.ts"],
  "exclude": ["node_modules"]
}

.eslintrc

{
  "parser": "@typescript-eslint/parser",
  "extends": ["plugin:@typescript-eslint/recommended"],
  "parserOptions": {
    "ecmaVersion": 2022
    // "sourceType": "module"
  },
  "plugins": ["@typescript-eslint"],
  "env": {
    "es2022": true,
    "node": true
  },
  "rules": {
    "prefer-const": "error",
    "no-var": "error",
    "@typescript-eslint/no-var-requires": "off",
    "@typescript-eslint/no-unused-vars": "off",
    "@typescript-eslint/no-explicit-any": "off"
  }
}

package.json

{
  "name": "testing",
  "version": "1.0.0",
  "author": "",
  "license": "ISC",
  "main": "app.js",
  "scripts": {
    "dev": "ts-node-dev src/app.ts dev",
    "build": "tsc -build",
    "start": "node build/app.js"
  },
  "dependencies": {
    "inquirer": "^9.1.0",
    "puppeteer": "^17.0.0"
  },
  "devDependencies": {
    "@types/inquirer": "^9.0.1",
    "@types/node": "^18.7.13",
    "@typescript-eslint/eslint-plugin": "^5.35.1",
    "@typescript-eslint/parser": "^5.35.1",
    "eslint": "^8.23.0",
    "ts-node-dev": "^2.0.0",
    "typescript": "^4.8.2"
  }
}

Repo: https://github.com/MariuzM/node-setup/tree/master

Upvotes: 1

Views: 607

Answers (1)

I also encountered the same issue, the fix is to include a "type": "module" in your package.json and then change all instances of your import to use .js after the file path even while using TypeScript. If you are not importing any custom files or modules, you can skip this. Also you would get an error if you run ts-node app.ts you've to compile the TypeScript to JavaScript before testing it,

You can update your build command to this "build": "tsc app.ts --esModuleInterop true --target ES2020 --module nodenext --moduleResolution nodenext --outDir build", then your start command to this npm run build && node ./build/app.js. That should solve the issue

Upvotes: 0

Related Questions