[英]Why am I getting an ng not found error when deploying my Angular project to Heroku?
My project layout looks like this:我的项目布局如下所示:
package.json (A)
index.js
client
> package.json (B)
with my outer package.json (A) looking like this:我的外部 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"
}
}
while the package.json (B) inside the client folder that holds the angular project looks like this:而包含 angular 项目的客户端文件夹中的 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"
}
}
Looking up other threads that have asked similar things, most of them said that I needed to move the @angular/cli and @angular/compiler-cli from devDependencies to dependencies, which is what I did.查找其他问过类似问题的线程,他们中的大多数人都说我需要将 @angular/cli 和 @angular/compiler-cli 从 devDependencies 移动到依赖项,这就是我所做的。
However, when Heroku runs the script但是,当 Heroku 运行脚本时
"build": "cd client && ng build --prod && cd .. && node index.js"
I'm not sure why I keep getting the error that ng was not found.我不确定为什么我不断收到未找到 ng 的错误。 Another thing I tried was to add the preinstall script to package.json (A)
我尝试的另一件事是将预安装脚本添加到 package.json (A)
"preinstall": "npm install -g angular-cli"
which removed the ng not found error, but instead I got an error saying that the build command would only work inside of an angular-cli project, which is making me think that maybe the change directory isn't working?它删除了 ng not found 错误,但是我收到一个错误,说 build 命令只能在 angular-cli 项目中工作,这让我认为更改目录可能不起作用? This is really confusing to me, because npm run build works fine for me...
这真的让我很困惑,因为 npm 运行构建对我来说很好......
I messed around a bit and finally got it to work.我搞砸了一点,终于让它工作了。 My solution was to change my package.json (A) scripts to
我的解决方案是将我的 package.json (A) 脚本更改为
"scripts": {
"start": "node index.js",
"build": "cd client && npm install && npm run build --prod"
},
and to move literally everything in devDependencies in package.json (B) into the dependencies folder.并将 package.json (B) 中的 devDependencies 中的所有内容移动到依赖项文件夹中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.