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 cd into D:\git\projectx and then use node.exe combine.js. This makes it so that the current directory inside your script points to D:\git\projectx

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

5/29/2013 10:09:39 AM

Accepted Answer

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.

3/31/2012 2:30:17 PM

You can always use __dirname to represent the directory of the script you are in...


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow