Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running `ng build` in a specific directory

I'm writing an automated build script that needs to run ng build in a specific directory. My project has the following structure:

project
├ build-script
└ project-client
  └ package.json

ng build is looking for the package.json file, but it can't find it in its current location. Is there a way I can direct ng build to the subdirectory without doing a cd (since bash is a bit wonky about changing directories)? Essentially, I just need a way to tell ng build to operate in a directory that I specify.

I have tried doing ng build ./project-client/ but it is giving the error that "You have to be inside an angular-cli project in order to use the build command"

Thanks.

like image 553
JGrindal Avatar asked Apr 26 '18 15:04

JGrindal


People also ask

Which folder should I run ng serve?

ng serve. To get the application running, the ng serve command must execute within the [name-of-app] folder. Anywhere within the folder will do. The Angular CLI must recognize that it is within an environment generated with ng new .


Video Answer


2 Answers

When I need to go to certain directory just to run a script I usually use pushd and popd. Example:

pushd /path/to/dir
ng build
popd

After this snippet is run your working directory (pwd) will remain unchanged.

like image 178
dmadic Avatar answered Oct 06 '22 18:10

dmadic


Note: This answer is specific to Angular commands

You can leverage the npm run command to run an Angular command while using the parameter --prefix to indicate the sub-directory.


Example 1: Original question (ng build)

Since Angular already has a shortcut for this command, you can simply run

npm run build --prefix path\to\Angular\project

Example 2: Custom command (Eg: ng build --prod)

First, create a shortcut for this command in your package.json file

{
    "scripts": {
        // "CMD-SHORTCUT": "custom-command"
        "prod-build": "ng build --prod"
    },
}

Then, run the new shortcut in the same way as example 1:

npm run prod-build --prefix path\to\Angular\project
like image 12
Burhan Avatar answered Oct 06 '22 20:10

Burhan