Is it possible to ensure iisnode respects NODE_PATH in Azure?

370 views Asked by At

It is not uncommon to want to be able to have non-relative imports, for example, for configuration, etc...

In the world of running a node executable on your own (development env, any cloud provider... things like that) you can simply set an env var and have it respected by the node runtime.

Imagine a project structure like so: dist |--foo |--bar |--baz app.js |--config

in app.js with NODE_PATH=dist, I can simply require('config') and have what I need.

Within Azure App Services, it appears to ignore NODE_PATH from Application Settings. Is something missing or is this not possible?

1

There are 1 answers

1
Aaron Chen On BEST ANSWER

In Azure App Services, you can set the NODE_PATH environment variable in the Azure portal with the following steps.

1, Create the D:\home\site\my_node_modules\config directory and put the index.js file in where. In this case, I just export the "name" variable.

// D:\home\site\my_node_modules\config\index.js
var name = "foobar";
// export it
exports.name = name;

2, Navigate to your App Service in the Azure portal, click on Application settings in the SETTING menu and then set the NODE_PATH variable as below:

enter image description here

3, In the app.js file, you can simply require('config') like this:

var http = require('http')
var config = require('config')

http.createServer(function (req, res) {
    res.end(config.name)
}).listen(process.env.PORT || 3000)

4, At last, it works fine.

enter image description here