Angular升级后ng serve报错:工作目录非绝对路径
Angular 17.3.7升级后运行ng serve报错:工作目录不是绝对路径
问题描述
将@angular/cli和@angular/core升级至17.3.7版本后,删除node_modules及package-lock.json,执行npm install后运行ng serve,出现错误:
The working directory "C:\Users\srapolu\gen-ai-latest\gen-ai" is not an absolute path
已尝试重装Angular CLI及重新安装依赖,问题未解决。
项目配置文件
package.json
{ "name": "gen-ai", "version": "0.0.0", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "build-prod": "ng build --configuration production", "watch": "ng build --watch --configuration development", "test": "ng test", "serve:ssr:gen-ai": "node dist/gen-ai/server/server.mjs" }, "private": true, "dependencies": { "@angular/animations": "^17.3.8", "@angular/common": "^17.3.8", "@angular/compiler": "^17.3.8", "@angular/core": "^17.3.8", "@angular/forms": "^17.3.8", "@angular/platform-browser": "^17.3.8", "@angular/platform-browser-dynamic": "^17.3.8", "@angular/router": "^17.3.8", "@azure/msal-angular": "^3.0.17", "@azure/msal-browser": "^3.14.0", "@ng-bootstrap/ng-bootstrap": "^16.0.0", "@popperjs/core": "^2.11.8", "@sreyaj/ng-shimmer": "^2.0.0", "bootstrap": "^5.3.2", "file-saver": "^2.0.5", "ngx-toastr": "^18.0.0", "node": "^20.13.1", "rxjs": "~7.8.0", "tslib": "^2.3.0", "xlsx": "^0.18.5", "zone.js": "~0.14.3" }, "devDependencies": { "@angular-devkit/build-angular": "^17.3.7", "@angular/cli": "^17.3.7", "@angular/compiler-cli": "^17.3.8", "@angular/localize": "^17.3.8", "@types/file-saver": "^2.0.7", "@types/jasmine": "~5.1.0", "esbuild-wasm": "~0.20.2", "jasmine-core": "~5.1.0", "karma": "~6.4.0", "karma-chrome-launcher": "~3.2.0", "karma-coverage": "~2.2.0", "karma-jasmine": "~5.1.0", "karma-jasmine-html-reporter": "~2.1.0", "typescript": "~5.3.2" }, "overrides": { "esbuild": "npm:esbuild-wasm@latest" } }
angular.json
{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": { "gen-ai": { "projectType": "application", "schematics": { "@schematics/angular:component": { "style": "scss" } }, "root": "", "sourceRoot": "src", "prefix": "app", "architect": { "build": { "builder": "@angular-devkit/build-angular:application", "options": { "outputPath": "dist/gen-ai", "index": "src/index.html", "browser": "src/main.ts", "polyfills": [ "zone.js" ], "tsConfig": "tsconfig.app.json", "inlineStyleLanguage": "scss", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "src/styles.scss" ], "scripts": [] }, "configurations": { "production": { "budgets": [ { "type": "initial", "maximumWarning": "2mb", "maximumError": "3mb" }, { "type": "anyComponentStyle", "maximumWarning": "4kb", "maximumError": "5kb" } ], "outputHashing": "all" }, "development": { "optimization": false, "extractLicenses": false, "sourceMap": true, "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.development.ts" } ] } }, "defaultConfiguration": "production" }, "serve": { "builder": "@angular-devkit/build-angular:dev-server", "configurations": { "production": { "buildTarget": "gen-ai:build:production" }, "development": { "buildTarget": "gen-ai:build:development" } }, "defaultConfiguration": "development" }, "extract-i18n": { "builder": "@angular-devkit/build-angular:extract-i18n", "options": { "buildTarget": "gen-ai:build" } }, "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "polyfills": [ "zone.js", "zone.js/testing" ], "tsConfig": "tsconfig.spec.json", "inlineStyleLanguage": "scss", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "src/styles.scss" ], "scripts": [] } } } } }, "cli": { "analytics": "4ac7a682-d5ab-401b-9d7c-9d8eee2cc564" } }
解决方案
1. 切换到CMD执行命令
Windows PowerShell可能存在路径解析问题,打开命令提示符(CMD),执行以下步骤:
- 切换到项目根目录:
cd C:\Users\srapolu\gen-ai-latest\gen-ai - 重新运行:
ng serve
2. 统一Angular CLI版本
确保全局与本地CLI版本一致:
- 查看全局版本:
ng --version - 查看本地版本:
npx ng --version - 若版本不一致,卸载全局CLI:
npm uninstall -g @angular/cli - 重新安装对应版本:
npm install -g @angular/cli@17.3.7
3. 移除esbuild覆盖配置
package.json中的overrides配置替换esbuild为wasm版本,可能导致路径解析异常:
- 删除package.json中的以下内容:
"overrides": { "esbuild": "npm:esbuild-wasm@latest" }
- 执行
npm install后重新运行ng serve
4. 清理缓存并重置配置
- 清理npm缓存:
npm cache clean --force - 清理Angular CLI缓存:
ng cache clean - (可选)重置angular.json配置:先备份原文件,再执行
ng update @angular/cli@17.3.7 --force
内容的提问来源于stack exchange,提问作者santhosh kumar
相关产品推荐
相关产品推荐

