Note that there are some explanatory texts on larger screens.

plurals
  1. POLet NodeJS application update itself using NPM
    text
    copied!<p>Hej There,</p> <p>I'm trying to add some non-conventional functionality to my NodeJS application but I'm having some trouble. What I'm trying to do is the following: </p> <p>I want to update my server code from the client. (An auto-update functionality if you will.)</p> <p>My first attempt was to utilize the NPM API and run:</p> <pre><code> npm.commands.install([package], function(err, data) </code></pre> <p>But of course this results in an error telling me NPM can not install while the server is running.</p> <p>My second attempt was spawning NPM update using the following code:</p> <pre><code> spawnProcess('npm', ['update'], { cwd: projectPath }, done); </code></pre> <p>The spawnProcess function is a generic spawn function:</p> <pre><code>var projectPath = path.resolve(process.cwd()); var spawnProcess = function(command, args, options, callback) { var spawn = require('child_process').spawn; var process = spawn(command, args, options); var err = false; process.stdout.on('data', function(data) { console.log('stdout', data.toString()); }); process.stderr.on('data', function(data) { err = true; console.log('stderr', data.toString()); }); if (typeof callback === 'function') { process.on('exit', function() { if (!err) { return callback(); } }); } }; </code></pre> <p>But this gives me a stderr followed by a 'CreateProcessW: can not find file' error. I don't quite know what I'm doing wrong. </p> <p>If all else fails I thought it might be possible to write a shellscript killing Node, updating the application and then rebooting it. Something like:</p> <pre><code>kill -9 45728 npm update node server </code></pre> <p>But I don't know if this is a plausible solution and how I would go about executing it from my node server. I'd rather have the spawn function working of course.</p> <p>Any help is welcome. Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload