问题
I am trying to setup Jenkins for an Angular CLI project. I have installed node and Angular Cli on the Jenkins server under a specific user account. if I open a command prompt on the server an execute the following commands to verify they are installed properly, this is the result:
I have configured the project with Jenkins, and i created two build steps two execute two bat files.
One runs: npm install
and the second one runs: ng build --prod
Then I build Jenkins, it runs the npm install but it fails running ng build --prod because it says " 'ng' is not recognised as an internal or external command".
Am I doing something wrong? Is there another way to probably use the angular cli on the node_modules folder, So it does not need to use the angular cli installed on the server. It seems like Angular CLI is installed only for my user on the server but not for the user Jenkins use to build.
PS: I installed Angular CLI globally using:
npm i -g @angular/cli
回答1:
No need to install angular cli on server, just run
npm run ng -- build
That will run the local version from your project devDependencies
This way you can pass any flag to your local cli npm run ng -- test
, npm run ng -- lint
, etc
You can pass additional flags to
ng
just like thatrun ng -- build --prod
More details at https://docs.npmjs.com/cli/run-script
回答2:
Just for further clarification when someone searches for the same problem and finds this question (as I did):
If you want to use the --prod flag while running the build command, as asked in this question, you can use:
npm run ng -- build --prod
Important are the "--" between "ng" and "build" with spacing. This is due to the syntax of "npm run", more information can be found here: https://docs.npmjs.com/cli/run-script
This also solves the problem described in a comment below the accepted answer: "This is working but its excluding the additional parameters like --test when running the build"
回答3:
If it is working on the Local command prompt, Restart the Jenkin server.
Restart Jenkins --> http://host-name:port/base-url/restart
ex:- http://localhost:8080/jenkins/restart
If it is not working on local command as well install angular CLI globally and set the environment settings and do the previous step.
回答4:
try as below.
BUILD_ID=dontKillMe nohup ng serve
回答5:
The only issue with npm run ng build
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
来源:https://stackoverflow.com/questions/47297329/ng-is-not-recognised-as-an-internal-or-external-command-jenkins-angular-cli