NodeJS NX SERVE::要加载ES模块,请设置“type”:在package.json中使用“module”或使用.mjs扩展名

kokeuurv  于 2023-05-12  发布在  Node.js
关注(0)|答案(1)|浏览(136)

我在我的项目中使用NX。当我跑的时候:

nx serve api

我有这个错误:0:07:38 AM-正在监视模式下启动编译... 10:07:41 AM-找到0个错误。查看文件更改。调试器侦听ws://localhost:9229/eaf65d56 - 25a0 - 4093-b137-ccf4d22ba251调试器侦听ws://localhost:9229/eaf65d56 - 25a0 - 4093-b137-ccf4d22ba251有关帮助,请参阅:[https://nodejs.org/en/docs/inspector](https://nodejs.org/en/docs/inspector)(节点:24118)警告:要加载ES模块,请设置“type”:在package.json中使用"module"或使用. mjs扩展名。(使用node --trace-warnings ...显示创建警告的位置)/home/beta/Téléchargements/Cell-iA/Transcription IA/transcription-ia/dist/src/main. js:1 import {containerIoc} from './container-ioc';^^^^^^ 语法错误:无法在Object的模块外部使用import语句。compileFunction(node:vm:352:18)在wrapSafe(节点:internal/modules/cjs/loader:1032:(15)模块。_compile(节点:internal/modules/cjs/loader:1067:27)在Object.Module._extensions..js(节点:internal/modules/cjs/loader:1157:10)在模块上。load(node:internal/modules/cjs/loader:九八一:32)在Function.Module._load(节点:internal/modules/cjs/loader:八二二:12)在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)在ModuleWrap.(node:internal/modules/esm/translators:168:29)at www.example.com(node:internal/modules/esm/module_job:197:25)at async Promise. all(index 0)ModuleJob.run (node:internal/modules/esm/module_job:197:25) at async Promise.all (index 0) `
这是我的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"
  }
}

我已经安装了ts-node,但我有同样的问题
你能帮帮我吗?
想运行nx服务api

hjzp0vay

hjzp0vay1#

如果您使用的是ES模块,您的package.json必须具有

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

否则,node(运行程序,而不是编译器)将尝试将代码作为非模块文件启动,从而导致上面的“无法将ES模块作为CommonJS模块运行”运行时错误
另一种解决方案是将tsconfig.jsonmodule设置为CommonJS,这不是您想要的
旁注:尝试将https://github.com/esbuild-kit/tsx作为ts-node的替代品,因为它具有内置的监视器和更多固定的角落情况,是一种更稳定、更高效的解决方案。

相关问题