简体   繁体   中英

node-config doesn't read config files when app is set up as Windows Service using node-windows

I'm using node-windows to set up my application to run as a Windows Service. I am using node-config to manage configuration settings. Of course, everything is working fine when I run my application manually using node app.js command. When I install it as a service and it starts, the configuration settings are empty. I have production.json file in ./config folder, and I can set NODE_ENV to production in the install script. I can confirm that the variable is set correctly and still nothing. log.info('CONFIG_DIR: ' + config.util.getEnv('CONFIG_DIR')); produces undefined even if I explicitly set it in env value for the service. Looking for any insight.

install script:

var Service = require('node-windows').Service;
var path = require('path');
// Create a new service object
var svc = new Service({
    name:'Excel Data Import',
    description: 'Excel Data Import Service.',
    script: path.join(__dirname, "app.js"), // path application file
     env:[
         {name:"NODE_ENV", value:"production"},
         {name:"CONFIG_DIR", value:  "./config"},
         {name:"$NODE_CONFIG_DIR", value: "./config"}
     ]
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
    svc.start();
});

svc.install();

app script:

var config = require('config');
var path = require('path');    

var EventLogger = require('node-windows').EventLogger;
var log = new EventLogger('Excel Data Import');

init();

function init() {
    log.info("init");

    if(config.has("File.fileFolder")){
        var pathConfig = config.get("File.fileFolder");   

        log.info(pathConfig);
        var DirectoryWatcher = require('directory-watcher');
        DirectoryWatcher.create(pathConfig, function (err, watcher) {
            //...
        });
    }else{
        log.info("config doesn't have File.fileFolder");            
    }
}

I know this response is very late, but also i had the same problem, and here is how i solved it :

var svc = new Service({
  name:'ProcessName',
  description: 'Process Description',
  script: require('path').join(__dirname,'bin\\www'),
  env:[
        {name: "NODE_ENV", value: "development"}, 
        {name: "PORT", value: PORT},
        {name: "NODE_CONFIG_DIR", value: "c:\\route-to-your-proyect\\config"}
      ]
});

When you are using windows, prefixing your enviroment variables with $ , is not required.

Also, when your run script isn´t on the same dir as your config dir, you have to provide a full path to your config dir.

When you have errors with node-windows , is also helpful dig into the error log. It is located on rundirectory/daemon/processname.err.log

I hope this will help somebody.

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