【问题标题】:Netlify "ng command not found"Netlify “未找到 ng 命令”
【发布时间】:2019-06-04 20:15:20
【问题描述】:

我正在尝试在 netlify 上部署我的 Angular 应用程序。但是,我遇到了一个问题,它说找不到 ng 命令。我的计算机上有全局角度的 cli 和 npm。当前的部署设置是:

构建命令:ng build --prod

发布目录:dist/mysite

这可能是什么原因?非常感谢!

这里是与失败构建相关的日志

5:43:56 PM: Build ready to start
5:43:58 PM: build-image version: 84aca9ba39e0ee86ba194760fbfc51a808f62543
5:43:58 PM: buildbot version: 57d44fd1631cc3fff8b38fbaf88d3d04f946418a
5:43:58 PM: Fetching cached dependencies
5:43:58 PM: Starting to download cache of 254.7KB
5:43:58 PM: Finished downloading cache in 113.606368ms
5:43:58 PM: Starting to extract cache
5:43:58 PM: Failed to fetch cache, continuing with build
5:43:58 PM: Starting to prepare the repo for build
5:43:59 PM: No cached dependencies found. Cloning fresh repo
5:43:59 PM: git clone git@gitlab.com:jshede/my-website
5:44:05 PM: Preparing Git Reference refs/heads/master
5:44:08 PM: Starting build script
5:44:08 PM: Installing dependencies
5:44:09 PM: Downloading and installing node v8.15.0...
5:44:09 PM: Downloading https://nodejs.org/dist/v8.15.0/node-v8.15.0-linux-x64.tar.xz...
5:44:10 PM:   0.0%
5:44:10 PM: 
###################
5:44:10 PM:                                              26.9%
5:44:10 PM: 
#####################
5:44:10 PM: ################################################### 100.0%
5:44:10 PM: Computing checksum with sha256sum
5:44:10 PM: Checksums matched!
5:44:13 PM: Now using node v8.15.0 (npm v6.4.1)
5:44:13 PM: Attempting ruby version 2.3.6, read from environment
5:44:14 PM: Using ruby version 2.3.6
5:44:14 PM: Using PHP version 5.6
5:44:14 PM: Started restoring cached go cache
5:44:14 PM: Finished restoring cached go cache
5:44:14 PM: unset GOOS;
5:44:14 PM: unset GOARCH;
5:44:14 PM: export GOROOT='/opt/buildhome/.gimme/versions/go1.10.linux.amd64';
5:44:14 PM: export PATH="/opt/buildhome/.gimme/versions/go1.10.linux.amd64/bin:${PATH}";
5:44:14 PM: go version >&2;
5:44:14 PM: export GIMME_ENV='/opt/buildhome/.gimme/env/go1.10.linux.amd64.env';
5:44:14 PM: go version go1.10 linux/amd64
5:44:14 PM: Installing missing commands
5:44:14 PM: Verify run directory
5:44:14 PM: Executing user command: ng build --prod
5:44:14 PM: /usr/local/bin/build: line 32: ng: command not found
5:44:14 PM: Caching artifacts
5:44:14 PM: Started saving pip cache
5:44:14 PM: Finished saving pip cache
5:44:15 PM: Started saving emacs cask dependencies
5:44:15 PM: Finished saving emacs cask dependencies
5:44:15 PM: Started saving maven dependencies
5:44:15 PM: Finished saving maven dependencies
5:44:15 PM: Started saving boot dependencies
5:44:15 PM: Finished saving boot dependencies
5:44:15 PM: Started saving go dependencies
5:44:15 PM: Finished saving go dependencies
5:44:15 PM: Cached node version v8.15.0
5:44:15 PM: Error running command: Build script returned non-zero exit code: 127
5:44:15 PM: Failing build: Failed to build site
5:44:15 PM: failed during stage 'building site': Build script returned non-zero exit code: 127
5:44:15 PM: Finished processing build request in 17.582066584s

这是我在 Angular 应用中的 package.json

   {
  "name": "mysite",
  "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/animations": "^6.1.0",
    "@angular/common": "^6.1.0",
    "@angular/compiler": "^6.1.0",
    "@angular/core": "^6.1.0",
    "@angular/forms": "^6.1.0",
    "@angular/http": "^6.1.0",
    "@angular/platform-browser": "^6.1.0",
    "@angular/platform-browser-dynamic": "^6.1.0",
    "@angular/router": "^6.1.0",
    "core-js": "^2.5.4",
    "rxjs": "~6.2.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.8.0",
    "@angular/cli": "~6.2.3",
    "@angular/compiler-cli": "^6.1.0",
    "@angular/language-service": "^6.1.0",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.3.0",
    "eslint": "^5.11.1",
    "eslint-config-google": "^0.11.0",
    "eslint-plugin-typescript": "^0.14.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~2.9.2",
    "typescript-eslint-parser": "^21.0.2"
  }
}

【问题讨论】:

  • 你设置了基本目录吗?你的 netlify 构建命令是什么(ng build --prod)?在您的 repo 中,根不是项目的根,因此 netlify 无法看到您的 package.json,除非您设置基本目录
  • 尝试使用npx ng build --prod,以防 angular-cli 未全局安装

标签: angular deployment netlify


【解决方案1】:

这是我在Netlify 上使用的一些Angular 简单设置设置

忽略依赖并部署文件夹

无需将这些保留在您的存储库中。 Netlify 将启动正确的工具并添加您的依赖项。

.gitignore

/node_modules
/dist

为 Netlify 设置构建环境(可选)

netlify.toml 会覆盖 app.netlify.com 管理员上的设置,因此如果我想要更改,我不必编辑它们。无需此文件即可将它们设置在那里。

netlify.toml(存储库的根目录)

[build]
  command = "npm run build:prod"
  publish = "dist"

build:prod 添加到package.json 的脚本部分

  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "build:prod": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },

angular.json 中设置正确的部署位置

projects.mysite.architect.build.options.outputPath 应该是“dist”或独特的,并且与您的 netlify.toml 发布值和忽略路径相匹配。 (注意:可以使用子路径,只要它们都像 dist/my-site 一样匹配)

确保有一个_redirects 来处理 SPA

projects.mysite.architect.build.options.assets 值应类似于angular.json 中的以下内容

        "assets": [
          "src/favicon.ico",
          "src/assets",
          "src/_redirects"
        ],

至少创建重定向文件,以将我们的路由代理到根索引页面。

src/_redirects

/* /index.html 200

【讨论】:

  • 是的,一旦您开始意识到 Netlify 与在本地进行生产构建并提供本地发布目录一样,事情就会变得更加清晰。作为最佳实践,我在提交 netlify 部署之前使用 serve <directory> 测试我的构建。
  • 它对我有用。直到我尝试添加重定向。虽然该文件包含在我的 Netlify 部署构建中。由于某种原因,Netlify 找不到我的重定向。
猜你喜欢
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多