open-cli-tools / concurrently

Run commands concurrently. Like `npm run watch-js & npm run watch-less` but better.
https://www.npmjs.com/package/concurrently
MIT License
6.98k stars 227 forks source link

ERR_REQUIRE_ESM #488

Closed jaikarans closed 2 months ago

jaikarans commented 2 months ago

I have a Monorepo with expressjs and react in typescript. can succesfully run commands like yarn workspace client start and yarn workspace server dev but when i use concurrently for running the both commands together i get error like this.

yarn run v1.22.19
$ concurrently "yarn workspace server dev" "yarn workspace client start"
/home/jaikaran/projects/file-share/node_modules/cliui/build/index.cjs:293
const wrap = require('wrap-ansi');
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/jaikaran/projects/file-share/node_modules/wrap-ansi/index.js from /home/jaikaran/projects/file-share/node_modules/cliui/build/index.cjs not supported.
Instead change the require of index.js in /home/jaikaran/projects/file-share/node_modules/cliui/build/index.cjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/jaikaran/projects/file-share/node_modules/cliui/build/index.cjs:293:14) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.14.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

here is my Monorepo package.json

  "name": "file-share",
  "version": "1.0.0",
  "description": "Effortlessly share your files from your personal storage with friends across the internet.",
  "main": "index.js",
  "author": "Jaikaran Saini",
  "license": "MIT",
  "private": true,
  "workspaces": [
    "server",
    "client"
  ],
  "scripts": {
    "client": "yarn workspace client start",
    "server": "yarn workspace server dev",
    "dev": "concurrently \"yarn workspace server dev\" \"yarn workspace client start\""
  },
  "devDependencies": {
    "@types/concurrently": "^7.0.0",
    "concurrently": "^8.2.2"
  }
}

here is Client package.json

  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^13.0.0",
    "@testing-library/user-event": "^13.2.1",
    "@types/jest": "^27.0.1",
    "@types/node": "^16.7.13",
    "@types/react": "^18.0.0",
    "@types/react-dom": "^18.0.0",
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "react-scripts": "5.0.1",
    "typescript": "^4.4.2",
    "web-vitals": "^2.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

and this is my server package.json file

  "name": "server",
  "version": "1.0.0",
  "description": "share files on network",
  "main": "index.js",
  "author": "Jaikaran Saini",
  "license": "MIT",
  "scripts": {
    "dev": "nodemon --watch \"*.ts\" --exec \"ts-node\" ./src/server.ts"
  },
  "dependencies": {
    "archiver": "^7.0.1",
    "dotenv": "^16.4.5",
    "express": "^4.19.2",
    "ts-node": "^10.9.2",
    "typescript": "^5.4.5"
  },
  "devDependencies": {
    "@types/archiver": "^6.0.2",
    "@types/express": "^4.17.21",
    "@types/node": "^20.14.2",
    "nodemon": "^3.1.3"
  }
}
jaikarans commented 2 months ago

`Solved by make changes innode_modules/cliui/build/index.cjs`:293:14 file commented this code // const wrap = require('wrap-ansi'); and added below code


let wrap;
async function loadWrapAnsi() {
    let wrap;
    try {
      const wrapModule = await import('wrap-ansi');
      wrap = wrapModule.default; // Assuming wrap-ansi exports a default export
    } catch (err) {
      // Handle import error if necessary
      console.error('Failed to import wrap-ansi:', err);
    }
    return wrap;
}
loadWrapAnsi();
gustavohenke commented 2 months ago

Take a look at the closed issues: https://github.com/open-cli-tools/concurrently/issues/432#issuecomment-1987399378