ReferenceError: exports is not defined in ES module scope" error occurs in monorepo configuration

Viewed 846

I am trying to try the monorepo structure. But it doesn't work!

example:

/
  pacakages/
   web/
      Dockerfile
      package.json
      tsconfig.json
      src/
   bakend/
      Dockerfile
      package.json
      tsconfig.json
      src/
  package.json

If you run "yarn dev" at the top level, it will run "yarn dev" on all the packages it belongs to.

If you run "yarndev" and "yarn build" in the child class, it will works correctly.

However, if you run "yarn dev" at the top level, you will get this error.

"ReferenceError: exports is not defined in ES module scope" ...contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension."

Is there any way to save my year-end vacation?

-- Top-level packages.json --

"type": "module",
"scripts": {
    "dev": "run-p dev:*",
}, ...

-- Child packages.json --

"type": "module",
"scripts": {
    "dev": "ts-node -r tsconfig-paths/register src/index.ts",
    "build": "rm -rf dist/; tsc;",
 }, ...

-- Child ts.config --

"compilerOptions": {
    "target": "es5",
    // "module": "commonjs",
    "lib": ["es2019"],
    "outDir": "./dist",
    "removeComments": true,
    "strict": true,
    "strictPropertyInitialization": false,
    "moduleResolution": "node",
    "baseUrl": "./",
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true
},
"exclude": ["node_modules"],
"include": ["src"]

-- Top-Level ​ts.config --

not exists

0 Answers
Related