node.jstypescriptts-nodenomachine-nx

NX SERVE ::To load an ES module, set "type": "module" in the package.json or use the .mjs extension


i use NX for my project. When i run :

nx serve api 

i have this error : `0:07:38 AM - Starting compilation in watch mode...

10:07:41 AM - Found 0 errors. Watching for file changes. Debugger listening on ws://localhost:9229/eaf65d56-25a0-4093-b137-ccf4d22ba251 Debugger listening on ws://localhost:9229/eaf65d56-25a0-4093-b137-ccf4d22ba251 For help, see: https://nodejs.org/en/docs/inspector (node:24118) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Use node --trace-warnings ... to show where the warning was created) /home/beta/Téléchargements/Cell-iA/Transcription IA/transcription-ia/dist/src/main.js:1 import { containerIoc } from './container-ioc'; ^^^^^^

SyntaxError: Cannot use import statement outside a module at Object.compileFunction (node:vm:352:18) at wrapSafe (node:internal/modules/cjs/loader:1032:15) at Module._compile (node:internal/modules/cjs/loader:1067:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.Module._load (/home/beta/Téléchargements/Cell-iA/Transcription IA/transcription-ia/packages/js/src/executors/node/node-with-require-overrides.ts:11:38) at ModuleWrap. (node:internal/modules/esm/translators:168:29) at ModuleJob.run (node:internal/modules/esm/module_job:197:25) at async Promise.all (index 0) `

This is my Tsfiles :

tsconfig.app.json :

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "types": ["node", "jest", "express"],
    "esModuleInterop": true,
    "noImplicitAny": true,
    "strict": true,
    "resolveJsonModule": true,
    "allowJs": true,
    "moduleResolution": "node",
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "sourceMap": true
  },
  "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
  "include": ["src/**/*.ts"],
  "ts-node": {
    "require": ["tsconfig-paths/register"]
  }
}

tsconfig.json :

{
  "extends": "../../tsconfig.base.json",
  "files": ["./custom.d.ts"],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
  ],
  "compilerOptions": {
    "baseUrl": ".",
    "composite": true,
    "declaration": true,
    "paths": {
      "@transcription-ia/api-interfaces": [
        "../../libs/api-interfaces/src/index.ts"
      ],
      "@transcription-ia/common-entities": [
        "../../libs/common-entities/src/index.ts"
      ],
      "@transcription-ia/helpers": ["../../libs/helpers/src/index.ts"]
    }
  }
}

tsconfig.base.json :

{
  "compileOnSave": false,
  "compilerOptions": {
    "rootDir": ".",
    "moduleResolution": "node",
    "sourceMap": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "module": "ESNext",
    "lib": ["es2020", "dom"],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "paths": {
      "@transcription-ia/api-interfaces": ["libs/api-interfaces/src/index.ts"],
      "@transcription-ia/common-entities": ["libs/common-entities/src/index.ts"]
    }
  },

  "exclude": ["node_modules", "tmp"]
}

project.json:

{
  "name": "api",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/api/src",
  "projectType": "application",
  "targets": {
    "build": {
      "executor": "@nx/js:tsc",
      "outputs": ["{options.outputPath}"],
      "options": {
        "outputPath": "dist/",
        "main": "apps/api/src/main.ts",
        "tsConfig": "apps/api/tsconfig.app.json",
        "assets": ["apps/api/src/assets"],
        "transformers": [
          {
            "name": "typescript-transform-paths/nx-transformer"
          }
        ]
      }
    },
    "serve": {
      "executor": "@nx/js:node",
      "defaultConfiguration": "development",
      "options": {
        "buildTarget": "api:build"
      },
      "configurations": {
        "development": {
          "buildTarget": "api:build:development"
        },
        "production": {
          "buildTarget": "api:build:production"
        }
      }
    },
    "lint": {
      "executor": "@nx/linter:eslint",
      "outputs": ["{options.outputFile}"],
      "options": {
        "lintFilePatterns": ["apps/api/**/*.ts"]
      }
    },
    "test": {
      "executor": "@nx/jest:jest",
      "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
      "options": {
        "jestConfig": "apps/api/jest.config.ts",
        "passWithNoTests": true
      },
      "configurations": {
        "ci": {
          "ci": true,
          "codeCoverage": true
        }
      }
    }
  },
  "tags": []
}

package.json :

{
  "name": "transcription-ia",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {},
  "private": true,
  "devDependencies": {
    "@babel/preset-react": "^7.14.5",
    "@nx/cypress": "16.1.3",
    "@nx/esbuild": "16.1.3",
    "@nx/eslint-plugin": "16.1.3",
    "@nx/jest": "16.1.3",
    "@nx/js": "16.1.3",
    "@nx/linter": "16.1.3",
    "@nx/node": "16.1.3",
    "@nx/react": "16.1.3",
    "@nx/vite": "16.1.3",
    "@nx/workspace": "16.1.3",
    "@swc/cli": "~0.1.62",
    "@swc/core": "~1.3.51",
    "@testing-library/react": "14.0.0",
    "@types/express": "~4.17.13",
    "@types/jest": "^29.4.0",
    "@types/node": "18.14.2",
    "@types/react": "18.0.28",
    "@types/react-dom": "18.0.11",
    "@types/uuid": "^9.0.1",
    "@typescript-eslint/eslint-plugin": "^5.58.0",
    "@typescript-eslint/parser": "^5.58.0",
    "@vitejs/plugin-react": "^3.0.0",
    "@vitest/coverage-c8": "^0.31.0",
    "@vitest/ui": "^0.31.0",
    "cypress": "^12.11.0",
    "esbuild": "^0.17.17",
    "eslint": "~8.15.0",
    "eslint-config-prettier": "8.1.0",
    "eslint-plugin-cypress": "^2.10.3",
    "eslint-plugin-import": "2.27.5",
    "eslint-plugin-jsx-a11y": "6.7.1",
    "eslint-plugin-react": "7.32.2",
    "eslint-plugin-react-hooks": "4.6.0",
    "jest": "^29.4.1",
    "jest-environment-node": "^29.4.1",
    "jsdom": "~20.0.3",
    "nx": "16.1.3",
    "prettier": "^2.6.2",
    "react-test-renderer": "18.2.0",
    "ts-jest": "^29.1.0",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "~5.0.2",
    "vite": "^4.3.4",
    "vite-plugin-eslint": "^1.8.1",
    "vite-tsconfig-paths": "^4.0.2",
    "vitest": "^0.31.0"
  },
  "dependencies": {
    "@faker-js/faker": "^7.6.0",
    "@gladiaio/sdk": "^0.10.71",
    "@swc/helpers": "~0.5.0",
    "@types/bcrypt": "^5.0.0",
    "@types/cors": "^2.8.13",
    "@types/jsonwebtoken": "^9.0.2",
    "@types/multer": "^1.4.7",
    "awilix": "^8.0.1",
    "awilix-express": "^8.0.0",
    "axios": "^1.0.0",
    "bcrypt": "^5.1.0",
    "cors": "^2.8.5",
    "express": "~4.18.1",
    "helmet": "^7.0.0",
    "jsonwebtoken": "^9.0.0",
    "multer": "^1.4.5-lts.1",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "stripe": "^12.4.0",
    "tslib": "^2.3.0"
  }
}

i already install ts-node but i have the same problem

Can you help me please ?

Want run nx serve api


Solution

  • If you are using ES modules, your package.json MUST have

    // package.json
    {
      "type": "module",
      ...
    }
    

    Otherwise node (the runner, not the compiler) will try to launch your code as a not-a-module file leading to the above "cannot run ES module as CommonJS module" runtime error


    Alternative solution is to set your tsconfig.json module to CommonJS, which is not what you want


    Side note: try https://github.com/esbuild-kit/tsx as an alternative for ts-node as more stable and efficient solution with builtin watcher and more fixed corner cases