Christoph Christoph - 3 months ago 6x
Node.js Question

How to set working directory for Node.js on windows?

I just installed node.js for windows and it really was a breeze to get it running. I would like to use it as part of my build process to combine several files together like so:

// settings
var FILE_ENCODING = 'utf-8',
EOL = '\n',
DIST_FILE_PATH = 'dist/myAwesomeScript.js';

// setup
var _fs = require('fs');

function concat(fileList, distPath) {
var out ={
return _fs.readFileSync(filePath, FILE_ENCODING);
_fs.writeFileSync(distPath, out.join(EOL), FILE_ENCODING);
console.log(' '+ distPath +' built.');


This really works like a charm. However it does only work if I place all my scripts into the nodejs directory which is C:\Program Files (x86)\nodejs and start the cmd process with admin rights.

But I need to have my project files in another directory ( say D:\git\projectx\ ) and would like to be able to run: node.exe D:\git\projectx\combine.js. Unfortunatly things doesn't work that way because node.exe will look for the files within it's own directory which is C:\Program Files (x86)\nodejs. There must be away to start the nodejs process and tell it to use another directory as its working directory, am I wrong?


As someone pointed out on IRC. The solution to my problem was rather simple. Just
and then use
node.exe combine.js
. This makes it so that the current directory inside your script points to

However, I'm accepting Luke's answer since it seems to be also true ;-)


You can set the current working directory using process.chdir, using Unix-style pathnames:


I'm not sure how to specify the drive prefix (D:) though.