Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go back 1 folder level with __dirname?

I am using gulp-karma and facing a simple problem but cannot seems to find what i am doing wrong .

gulp.task('test', function (done) {     karma.start({         configFile: __dirname + '..\\test\\' +'\karma.conf.js',         singleRun: true     }, done); }); 

Here is the code i am using and i cannot seems to go 1 level back in the folder directory . When i do the above it just append the ..\ to the folder direcotry without going 1 level back (which is the usual use of ..\). Following is the folder structure .

parent|       test|karma.conf.js       webapirole|gulpfile.js 

and my folder is inside the webapirole folder . i want to go back 1 folder back and go inisde the test folder which contains the karma.conf.js file. can anyone make me understand what i am doing wrong here ?

error i am getting

[18:06:32] Starting 'tdd'... ERROR [config]: File C:\Users\Documents\WebApiRole..\test\karma.conf.js does not exist 
like image 771
Malik Avatar asked Jun 15 '15 12:06

Malik


People also ask

How do I go back a directory in node JS?

You must use nodejs built in module path to work with directories. The method path. join() execute several segments into one path. If you want to move two step backward from current working directory, the code will be like this...

What is __ Dirname in path?

__dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.

What is dirname used for?

Introduction. __dirname is an environment variable that tells you the absolute path of the directory containing the currently executing file.


2 Answers

I am using (path) NPM for the above usage......

simply require path npm in js file.Then use

let reqPath = path.join(__dirname, '../../../');//It goes three folders or directories back from given __dirname. 
like image 197
Vemula Abilash Avatar answered Oct 11 '22 22:10

Vemula Abilash


TL;DR

Use path.join(__dirname, '..', 'test', 'karma.conf.js'). Prevent use of slashes.

Long Answer

As a lot of answers have pointed out, using path module is probably the best way. However, most of the solutions here have gone back to using slashes like:

path.join(__dirname+'../test/karma.conf.js')

However, by doing this, you're beating the purpose of using path. One uses path to do operations irrespective of the underlying OS (Linux, Windows etc). Just to give a bit of insight, you can do the path operations directly as string operations (like __dirname + '../test/karma.conf.js'. You do not do this because Linux uses forward slashes ( / ), Windows uses backward slashes ( \ ). This makes your application prone to errors when you port it across operating systems.

Thus, the better way would be:

path.join(__dirname, '..', 'test', 'karma.conf.js')

And of course, coming back - prevent use of slashes in your path.join, instead spread out your params.

like image 22
Pranav Totla Avatar answered Oct 11 '22 21:10

Pranav Totla