0

I've created one npm package which accesses few API's running in the backend. The URL to access those api's are hardcoded in the package. Most of the times we need to change the URL to point to some another api (different version pointing different database). And we end up editing the code and update the URL and re-publish the npm package.

Our npm package code is in a private git repo. We are creating a pipeline which will auto-publish the package on check-in. We want to add the URL's as build variables which will be fetched during build pipeline and will be added as part of npm package before publishing.

Is there any way to do so? If yes what changes needs to be done in the azure pipeline process and in the actual code?

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
Nikhil Patil
  • 83
  • 3
  • 8
  • what do you have in place already? what doesnt work? this question is too broad – 4c74356b41 Nov 05 '19 at 12:20
  • To give an idea, when we deploy any front-end web app on azure, we set api-url's used in the app as environment variables in azure devops. We can just change the api-url and redeploy it. We want to do something similar for a npm package -- Changing api-urls used in npm package through azure environment variables – Nikhil Patil Nov 05 '19 at 12:36
  • Just want to check is there good news about trying the approach I provided in my answer? If my reply helped, appreciate for [marking it as an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) which will also help others in the community. If still have any other concern on this, feel free to leave comment below. – Mengdi Liang Nov 08 '19 at 03:23

1 Answers1

0

As what you said, you store your npm package code in Repos. Here I think the Replace Token task can for you to achieve replacing the URL from variable into your code.

To use that task:

  • (1). you need firstly store URLs into variables. For example, create one variable names A and input the value.
  • (2). Then follow that task syntax(#{****}#) to modify your code.

    Go the script where you want it to be replaced with the variables value. Modify it as #{A}# . (Here please make sure the **** value is same with the relevant variables name you configured before.)

    Thus the corresponding location can be replaced by the variables value automatically during the build.

For detailed steps, you can refer my previous answer which has the similar demands.


Another things you need pay attention to is (3). add this Replace token task before your package publish step. After you check in the code and trigger the build, it will firstly override the source code by replacing the variable value into script. Then the next publish step could pack and publish a completed source files with these changed codes.

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35