0

I'm aware that I can select the node version to use with NVM but, can I build two angular projects (with different ng version and node version) at the same time without issues? My scenario is a self-hosted build server (Windows) with two agents. Each of these might be, at the same time, on charge of building an Angular app with different version.

Regards

Josto
  • 137
  • 11

2 Answers2

1

Sure you can, instead of running the globally installed ng run the local one with npx like this npx ng build, npx will use the local installed @angular/cli ng command found under ./node_modules/.bin/ of your project, npx comes installed with npm.

Another option is to add a script in package.json:

"scripts": {
    "build": "ng build --prod=true --build-optimizer=true --aot=true",
},

And runt it with npm run build.

Andrei
  • 4,289
  • 4
  • 25
  • 23
  • Thank you @Andrei, Do I need setting (with NVM) the node version specific for the angular version I'm going to build as first step of the pipeline? or this is not needed when using npx? – Josto Mar 17 '22 at 07:33
  • 1
    Yes it's best to use the recommended nodejs/npm version for the specific angular version, although it might work using the same version. – Andrei Mar 17 '22 at 11:03
  • 1
    I also recommend building the angular projects in Docker containers, this way you always build using the same environment (OS, Node.js, NPM, ...). – Andrei Mar 17 '22 at 11:05
  • Indeed Docker would be the best bet :) but in this case we can not use it. So, my idea is add this three steps to pipeline: 1: nvm use (sets the right version of node according angular version to build) 2: npm Install (to download/upgrade node modules) 3: npm run build (to launch the ng build script established in package.json). Is this correct from your point of view? Nothing to worry about when two pipellines building different angular versions concur? – Josto Mar 17 '22 at 15:08
  • 1
    One thing that you need be aware of is that when you call nvm use it switches the version in all open consoles, at least according to nvm windows: "When you do run nvm use x.x.x, the active version of node is automatically updated across all open console windows."" So if there is a pipeline in progress with node 10 and you call nvm use 12 in a second pipeline it will switch everywhere to node 12, if in the first pipeline has another node command it will use node 12 instead of 10 so this might cause issues. – Andrei Mar 17 '22 at 15:43
  • 1
    I suggest you test it out and see how it works, also take a look at https://volta.sh, its similar to nvm but it might work for you usecase, according to docs: "Whenever you install a tool with Volta, it adds a shim to your PATH that acts as an intelligent (and fast) router to the right version of the tool and runs it with the right Node engine." – Andrei Mar 17 '22 at 15:44
0

As @Andrei stated, is not possible to use NVM to set the Node version at the beginning of pipeline because it will change the version in all open consoles (so, if another pipeline with a different version of Node is running, would be affected).

Luckily, I found an easy workaround which does not require install additional tools or change package.json:

  1. Download the node version you want as zip file
  2. Unzip to a folder in Agent (like C:\LocalNode\node-v17.8.0-win-x64)
  3. Add Node path to PATH environment var only for current pipeline

To add Node path only for current pipeline, we have to add a Powershell task as first task of the pipeline with the current command:

Write-Host ("##vso[task.setvariable variable=Path;]D:\LocalNode\node-v17.8.0-win-x64;$Env:Path")

Rest of the tasks of the pipeline will use the Node version from D:\LocalNode\node-v17.8.0-win-x64

If you want to create a pipeline for a different Node version, just add the version to D:\LocalNode and use the above command with the right path as first task of the pipeline. No problem if both pipelines run at the same time.

Josto
  • 137
  • 11