将Angular项目部署到Heroku时为什么会出现ng not found错误?

时间:2020-05-23 14:18:03

标签: node.js angular heroku npm

我的项目布局如下:

package.json (A)
index.js
client
> package.json (B)

我的外部package.json(A)看起来像这样:

{
  "name": "---",
  "version": "1.0.0",
  "description": "---",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "build": "cd client && ng build --prod && cd .. && node index.js"
  },
  "repository": {
    "type": "git",
    "url": "---"
  },
  "author": "---",
  "license": "ISC",
  "bugs": {
    "url": "---"
  },
  "homepage": "---",
  "dependencies": {
    "axios": "^0.19.2",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongoose": "^5.9.14"
  }
}

客户端文件夹中保存有角度项目的package.json(B)如下所示:

{
  "name": "client",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/cli": "~9.0.6",
    "@angular/compiler-cli": "~9.0.6",
    "@angular/animations": "~9.0.6",
    "@angular/common": "~9.0.6",
    "@angular/compiler": "~9.0.6",
    "@angular/core": "~9.0.6",
    "@angular/fire": "^6.0.0",
    "@angular/forms": "~9.0.6",
    "@angular/platform-browser": "~9.0.6",
    "@angular/platform-browser-dynamic": "~9.0.6",
    "@angular/router": "~9.0.6",
    "firebase": "^7.14.4",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.900.6",
    "@angular/cli": "~9.0.6",
    "@angular/compiler-cli": "~9.0.6",
    "@angular/language-service": "~9.0.6",
    "@types/node": "^12.11.1",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~5.18.0",
    "typescript": "~3.7.5"
  }
}

查找其他询问类似问题的线程,大多数人说我需要将@ angular / cli和@ angular / compiler-cli从devDependencies移到依赖项,这就是我所做的。

但是,当Heroku运行脚本

"build": "cd client && ng build --prod && cd .. && node index.js"

我不确定为什么会不断收到找不到ng的错误消息。我尝试的另一件事是将预安装脚本添加到package.json(A)

"preinstall": "npm install -g angular-cli"

这消除了ng not found错误,但是我收到一条错误消息,指出build命令仅在angular-cli项目内部有效,这使我认为更改目录可能无法正常工作?这真的让我感到困惑,因为npm run build对我来说很好...

1 个答案:

答案 0 :(得分:0)

我搞砸了一点,终于使它起作用了。我的解决方案是将package.json(A)脚本更改为

"scripts": {
    "start": "node index.js",
    "build": "cd client && npm install && npm run build --prod"
  },

,然后将package.json(B)中devDependencies中的所有内容移到dependenciess文件夹中。