node.js - NX SERVE ::Để tải một module ES, hãy đặt "type": "module" trong package.json hoặc sử dụng phần mở rộng .mjs
Tôi sử dụng NX cho dự án của mình. Khi tôi chạy:
nx serve api
tôi có lỗi này: `0:07:38 AM - Bắt đầu biên dịch ở chế độ theo dõi...
10:07:41 SA - Tìm thấy 0 lỗi. Đang theo dõi các thay đổi của tệp. Trình gỡ lỗi lắng nghe tại ws://localhost:9229/eaf65d56-25a0-4093-b137-ccf4d22ba251 Trình gỡ lỗi lắng nghe tại ws://localhost:9229/eaf65d56-25a0-4093-b137-ccf4d22ba251 Để được trợ giúp, hãy xem: https://nodejs.org/en/docs/inspector (node:24118) Cảnh báo: Để tải một mô-đun ES, hãy đặt "type": "module" trong package.json hoặc sử dụng phần mở rộng .mjs. (Sử dụng node --trace-warnings ... để hiển thị nơi cảnh báo được tạo ra) /home/beta/Téléchargements/Cell-iA/Transcription IA/transcription-ia/dist/src/main.js:1 import { containerIoc } từ './container-ioc'; ^^^^^^
Lỗi cú pháp: Không thể sử dụng câu lệnh import bên ngoài một module tại Object.compileFunction (node:vm:352:18) tại wrapSafe (node:internal/modules/cjs/loader:1032:15) tại Module._compile (node:internal/modules/cjs/loader:1067:27) tại Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10) tại Module.load (node:internal/modules/cjs/loader:981:32) tại Function.Module._load (node:internal/modules/cjs/loader:822:12) tại 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) tại ModuleWrap. (node:internal/modules/esm/translators:168:29) tại ModuleJob.run (node:internal/modules/esm/module_job:197:25) tại async Promise.all (chỉ số 0)
Đây là Tsfiles của tôi:
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"
}
}
Tôi đã cài đặt ts-node nhưng vẫn gặp cùng một vấn đề
Bạn có thể giúp tôi được không?
Muốn chạy nx serve api
Câu trả lời #1
Nếu bạn đang sử dụng các module ES, package.json của bạn PHẢI có
// package.json
{
"type": "module",
...
}
Nếu không, node (trình chạy, không phải trình biên dịch) sẽ cố gắng khởi chạy mã của bạn như một tệp không phải module dẫn đến lỗi runtime "cannot run ES module as CommonJS module" nêu trên
Giải pháp thay thế là đặt module trong tsconfig.json của bạn thành CommonJS, điều này không phải là điều bạn muốn
Ghi chú thêm: thử https://github.com/esbuild-kit/tsx như một sự thay thế cho ts-node như một giải pháp ổn định và hiệu quả hơn với trình theo dõi tích hợp sẵn và nhiều trường hợp góc đã được sửa
