15

before installing Jenkins I ran this: npm install -g @angular/cli

but I also have this in devDependencies in the package.json for the project:

"@angular/cli": "1.0.0-beta.32.3",


when running a Jenkins build, I get this message in the log:

'ng' is not recognized as an internal or external command, operable program or batch file.

=======================================

Here is the Windows batch command in Jenkins:

cmd /c call npm install

set Path=%WORKSPACE%\node_modules\@angular\cli\bin;%PATH%

echo %PATH%

ng build -prod

========================================

here is a little more log output from Jenkins:

C:\Program Files (x86)\Jenkins\workspace\UiUnitTests>ng build -prod

'ng' is not recognized as an internal or external command, operable program or batch file.

C:\Program Files (x86)\Jenkins\workspace\UiUnitTests>exit 9009 Build step 'Execute Windows batch command' marked build as failure


but when I run this just from the command line (not in a Jenkins job), this works fine:

C:\Program Files (x86)\Jenkins\workspace\UiUnitTests>ng build -prod

Your global Angular CLI version (1.0.0-rc.1) is greater than your local

version (1.0.0-beta.32.3). The local Angular CLI version is used.

To disable this warning use "ng set --global warnings.versionMismatch=false". Hash: 7853ecb5a81a25eadbeb Time: 61317ms chunk {0} polyfills.7aaf5284cd5921eea40b.bundle.js (polyfills) 278 kB {4} [initial] [rendered] chunk {1} main.3380f71d3e71966aea27.bundle.js (main) 371 kB {3} [initial] [rendered] chunk {2} styles.9db1bafdfc989b37db97.bundle.css (styles) 69 bytes {4} [initial] [rendered] chunk {3} vendor.24574fc8320129058fac.bundle.js (vendor) 2.18 MB [initial] [rendered] chunk {4} inline.d1f5b52100bed2568d44.bundle.js (inline) 0 bytes [entry] [rendered]

C:\Program Files (x86)\Jenkins\workspace\UiUnitTests>

================================================

last but not least, here is the Jenkins log output from echo %PATH%

C:\Program Files (x86)\Jenkins\workspace\UiUnitTests>echo C:\Program Files (x86)\Jenkins\workspace\UiUnitTests\node_modules\@angular\cli\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Amazon\cfn-bootstrap\;C:\Ruby23-x64\bin;C:\Program Files\nodejs\;C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps C:\Program Files (x86)\Jenkins\workspace\UiUnitTests\node_modules\@angular\cli\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Amazon\cfn-bootstrap\;C:\Ruby23-x64\bin;C:\Program Files\nodejs\;C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps

user372225
  • 851
  • 2
  • 9
  • 23
  • Not going to put this as an answer, but for me, putting 'call ng build --prod' stopped it quitting the script after running the ng build. – JsAndDotNet Oct 05 '18 at 09:01

2 Answers2

29

Do not install the CLI globally.

Run the npm install for the repo and any time you need to run an ng command use this:

node_modules/.bin/ng [command goes here]

This will save on install time and ensure there are no differences between your local and global versions.

Additional commentary: Update your app to the latest version of the CLI

Brocco
  • 62,737
  • 12
  • 70
  • 76
  • 6
    You don't need to run ng directly. If you use the npm scripts the correct ng commands will be executed for you. For instance, you can run "npm run build -- --prod" to create the dist folder for prod. – Fabricio May 25 '17 at 11:30
  • works great, thank you! Having the CLI installed globally won't hurt but it is not the solution. – Sebastian Ballarati Aug 29 '17 at 12:03
  • @SebastianBallarati Jenkins do not allow to install Angular CLI globally – Ammar Hasan Feb 01 '19 at 23:39
  • closely related but slightly off topic - some users may find this helpful ... https://stackoverflow.com/questions/68244224/ng-add-angular-fire-error-cannot-run-login-in-non-interactive-mode/68244350#68244350 – danday74 Jul 04 '21 at 11:48
4

Try npm run ng build. The only issue with this is it omits any other parameter like --prod or --test after build.

Following are the commands what i am using to run my angular build successfully from Jenkins.The last command is executed the dirty way by setting up the path variables. Don't know if there is a cleaner way to do this. This does execute the commands properly without omitting anything.

@echo on
cmd /c npm install -g @angular/cli@latest

echo yarn Install
cmd /c yarn

echo Build
set PATH=%PATH%;C:\Users\Administrator\AppData\Roaming\npm;C:\Users\Administrator\AppData\Roaming\npm\node_modules\@angular\cli\bin;
ng build --prod --aot=true
hakuna
  • 6,243
  • 10
  • 52
  • 77
  • Doesn't work for me, fails with `ERROR: Task not found: "npm"`. Previously it was something like `ERROR: Task not found: "ng"`. Executing `npm install -g @angular/cli` additionally to `npm install` also didn't help. – Halfist Jun 29 '21 at 16:08