Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 = fileList.map(function(filePath){             return _fs.readFileSync(filePath, FILE_ENCODING);         });     _fs.writeFileSync(distPath, out.join(EOL), FILE_ENCODING);     console.log(' '+ distPath +' built.'); }  concat([     'foo/bar.js',     'foo/lorem.js',     'foo/maecennas.js' ], DIST_FILE_PATH); 

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?

UPDATE

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 ;-)

like image 975
Christoph Avatar asked Mar 31 '12 13:03

Christoph


2 Answers

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

 process.chdir('/temp/foo'); 

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

like image 177
Luke Girvin Avatar answered Oct 03 '22 22:10

Luke Girvin


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

process.chdir(__dirname); 
like image 25
Tracker1 Avatar answered Oct 03 '22 22:10

Tracker1