You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NX v16 Monorepo中NestJS启动报错:仅支持file/data协议URL

NX v16 Monorepo中NestJS应用Windows启动报错解决方案

问题描述

使用NX v16创建Monorepo后,React应用可正常运行,但通过NX VS Code插件生成器创建的NestJS应用,执行nx run <nest-app-name>:serve启动时出现以下错误:

Error: Only URLs with a scheme in: file and data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'

环境信息:Windows 11系统,Node.js版本18.16.0,同一Monorepo内的React应用无此问题,推测为Windows路径处理相关问题。

项目package.json内容

{
  "name": "my-nx-monorepo",
  "version": "0.1.0",
  "license": "MIT",
  "scripts": {
    "graph": "npx nx graph",
    "dashboard": "npx nx run dashboard:serve",
    "dashboard:lint": "npx nx run dashboard:lint",
    "dashboard:test": "npx nx run dashboard:test",
    "dashboard:build": "npx nx run dashboard:build",
    "connect-to-nx-cloud": "npx nx connect-to-nx-cloud"
  },
  "private": true,
  "dependencies": {
    "@nestjs/common": "^9.1.1",
    "@nestjs/core": "^9.1.1",
    "@nestjs/platform-express": "^9.1.1",
    "@swc/helpers": "~0.5.0",
    "@tanstack/react-query": "^4.29.5",
    "@tanstack/react-query-devtools": "^4.29.6",
    "@xstate/react": "^3.2.2",
    "axios": "^1.4.0",
    "clsx": "^1.2.1",
    "i18next": "^22.4.15",
    "i18next-browser-languagedetector": "^7.0.1",
    "normalize.css": "^8.0.1",
    "react": "18.2.0",
    "react-aria": "^3.24.0",
    "react-dom": "18.2.0",
    "react-i18next": "^12.2.2",
    "react-router-dom": "^6.11.1",
    "react-stately": "^3.22.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.0",
    "tslib": "^2.3.0",
    "xstate": "^4.37.2"
  },
  "devDependencies": {
    "@babel/preset-react": "^7.14.5",
    "@nestjs/schematics": "^9.1.0",
    "@nestjs/testing": "^9.1.1",
    "@nx/cypress": "16.1.0",
    "@nx/eslint-plugin": "16.1.0",
    "@nx/jest": "16.1.4",
    "@nx/js": "16.1.4",
    "@nx/linter": "16.1.0",
    "@nx/nest": "16.1.4",
    "@nx/node": "16.1.4",
    "@nx/react": "16.1.0",
    "@nx/vite": "16.1.0",
    "@nx/webpack": "16.1.4",
    "@nx/workspace": "16.1.0",
    "@swc/cli": "~0.1.62",
    "@swc/core": "~1.3.51",
    "@tanstack/eslint-plugin-query": "^4.29.4",
    "@testing-library/react": "14.0.0",
    "@total-typescript/ts-reset": "^0.4.2",
    "@types/jest": "^29.4.0",
    "@types/node": "18.14.2",
    "@types/react": "18.0.28",
    "@types/react-dom": "18.0.11",
    "@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",
    "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.0",
    "nx-cloud": "latest",
    "prettier": "^2.6.2",
    "react-test-renderer": "18.2.0",
    "sass": "^1.55.0",
    "ts-jest": "^29.1.0",
    "ts-node": "10.9.1",
    "typescript": "~5.0.2",
    "vite": "^4.3.4",
    "vite-plugin-eslint": "^1.8.1",
    "vite-tsconfig-paths": "^4.0.2",
    "vitest": "^0.31.0"
  }
}

解决方案

1. 确认NestJS应用的模块系统配置

打开NestJS应用目录下的tsconfig.app.json,确保compilerOptions中的module设置为CommonJS(NestJS默认使用CommonJS,ESM模式在Windows下易出现路径解析问题):

{
  "compilerOptions": {
    "module": "CommonJS",
    // 其他保持原有配置
  }
}

2. 修正NestJS应用的启动配置

找到NestJS应用目录下的project.json,修改serve目标的配置,明确指定tsconfig路径并确保使用node执行:

{
  "targets": {
    "serve": {
      "executor": "@nx/nest:serve",
      "options": {
        "tsConfig": "apps/<你的nest应用名>/tsconfig.app.json",
        "watch": true,
        "executable": "node"
      }
    }
  }
}

3. 临时规避ESM加载器问题

在启动命令前添加环境变量,禁用实验性的ESM相关特性:

NODE_OPTIONS="--no-experimental-fetch" nx run <你的nest应用名>:serve

也可以在根package.json的scripts中新增启动脚本:

"scripts": {
  "nest-app:serve": "NODE_OPTIONS=\"--no-experimental-fetch\" nx run <你的nest应用名>:serve"
}

4. 升级NX相关依赖

旧版本的@nx/nest和@nx/node可能存在Windows路径处理的bug,执行以下命令升级到16.x系列的最新版本:

npm update @nx/nest @nx/node nx

内容的提问来源于stack exchange,提问作者XxXtraCookie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 23:27:15