Environment variable in Javascript (Gulp)

I have a local development machine and a test server.
Now I have an APP_ID that’s being used in Javascript. I’ve been looking into ways that they kan differ on my local machine and on the test server.

Using Gulp

With gulp it’s possible to add a flag on the command line:

gulp build --env=production

That way I can get the correct APP_ID from a file.
The only issue is with this approach I need to run my build on the server, at this moment I run gulp locally and upload the changes to my server

Is it okay to build on the server? Are there other ways to use environment variables in Javascript?

My suggestion is to not build on the server but build locally and then deploy to the server using one of the many deploy solution (es. deployer.org for php). Normally javascript NPM packages put build output even in GIT repository ready for use in other projects or for deploy.

For more info on how to use env variable in node (gulp run over node) see this page

For example in linux you can set env variable with export

app.js :


Then try

> export foo=app1

> node app.js


Then try

> export foo=app2

> node app.js


This is valid only if you run your code server side on node (ex on gulp).
If you are developing a client side library and you want to create different builds that targets different enviroments you have to instruct gulp to do so. In this case this is a guide that can help you