sbedulin
sbedulin

Reputation: 4342

Azure NODE_PATH environment variable doesn't have any effect

I have a NodeJS project deployed to Azure with some project-wide modules like config.

├── bin
│    └── start
├── config
│    └── index.js
└── web.config

Locally I start my app with npm start which works OK on Windows.

"scripts": {
    "start": "SET NODE_PATH=.& node bin/start"
}

On Azure I haven't found a way to run scripts from package.json, thus I altered web.config to run node bin/start.

NODE_PATH env variable is set via "Application settings" -> "App settings" and is echoed correctly. However, it doesn't have any effect.

bin/start file outputs env vars and error message Start "production" - "." - "Cannot find module 'config'":

var http = require('http');
var util = require('util');
var nconf = require('nconf');

nconf.argv()
     .env();

var configMsg = 'default msg';
try {
    var config = require('config');
    if(config) {
        configMsg = 'WOOT!';
    }
} catch (e) {
    configMsg = e.message;
}

http.createServer(function(req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });

  res.end(
    util.format(
        'Start "%s" - "%s" - "%s"',
        nconf.get('NODE_ENV'),
        nconf.get('NODE_PATH'),
        configMsg
    )
  );
}).listen(process.env.PORT);

How can I make Azure understand NODE_PATH ?

Or alternatively, how can I tell Azure to run my app with npm script like npm start ?

Upvotes: 1

Views: 1168

Answers (2)

Gary Liu
Gary Liu

Reputation: 13918

As the scripts in package.json is used for npm cli, like npm start in your project to run the application. However, node.js applications on Azure Web Apps are hosted by iisnode, which will bypass the packages.json file.

You can consider the following workarounds:

  • Move your custom module (config folder here) into node_modules folder in your root directory. When you use require('config'), the process will search the module 'config' in node_modules folder

  • Keep the architecture of your application, modify the script code where you need to involve your custom module, e.g var config = require('../config'); to involve the relative path of your module

Upvotes: 1

Brij Raj Singh - MSFT
Brij Raj Singh - MSFT

Reputation: 5113

You don't need to do npm start in Azure web apps, npm start actually looks for package.json file and runs the script thus specified against it with the configurations in package.json file, while in Azure web apps - the azure platform looks for the presence of app.js or server.js file in root and does the node <server.js> command on it.

I find this article very interesting when it comes to understanding of how azure runs your nodejs app http://blogs.msdn.com/b/silverlining/archive/2012/06/14/windows-azure-websites-node-js.aspx.

If you don't want to ship the entire node_modules folder to the web app, than you must have a package.json file with you and the azure will run the npm for your web app.

Upvotes: 0

Related Questions