Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving up a directory using Node.js process.cwd()

Not sure if I have ever figured out a good workaround for this.

I am in a directory, I want to use process.cwd() to find the currently working directory but want to move up on directory, something like so:

var serverPath = path.resolve(process.cwd() + '../bin/www');

however, this obviously won't work, and will give me an error along these lines:

Error: Cannot find module '/Users/amills001c/WebstormProjects/ORESoftware/suman../bin/www'

so what is the best way to use process.cwd() and move up a directory?

like image 588
Alexander Mills Avatar asked Sep 18 '25 23:09

Alexander Mills


2 Answers

You just need a / before the ..:

var serverPath = path.resolve(process.cwd() + '/../bin/www');
like image 132
Paul Avatar answered Sep 22 '25 02:09

Paul


You're missing a \.

You should consider calling path.join(process.cwd(), '../bin/www'); before you pass it to path.resolve. This will help get the slashes correct. Concatenation of file paths is generally considered risky business.

like image 27
Chris Anderson-AWS Avatar answered Sep 22 '25 00:09

Chris Anderson-AWS