简体   繁体   中英

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'" 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 ?

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, eg var config = require('../config'); to involve the relative path of your module

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM