3

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

Marius
  • 1,664
  • 2
  • 16
  • 28

0 Answers0