How to go back 1 folder level with __dirname?
Asked Answered
F

11

113

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
Forename answered 15/6, 2015 at 12:39 Comment(1)
possible duplicate of How do i go to parent directory when using __dirname?Forename
L
135

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.

Libya answered 6/4, 2020 at 9:39 Comment(2)
The design of path.join is quite intuitive, solving various cross-platform slash problems, and should be done so.Among
I had a server folder and a client folder, I was trying to have my express serve the static files and sendFile the index html from the dist folder in my client folder. Every other solution with __dirname didn't work for me except yours. Thank you.Letrice
T
71

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.
Tillion answered 26/12, 2016 at 13:18 Comment(1)
using slashes with the path module, defeats the purpose of using path. please see @Pranav Totla's answer below https://mcmap.net/q/193395/-how-to-go-back-1-folder-level-with-__dirnameSalmons
P
29

__dirname is just a string. you can use ../ to traverse up the folder structure and path.join to resolve the path

path = require('path')

configFile: path.join(__dirname, '../test/karma.conf.js'),
Potential answered 7/6, 2016 at 18:56 Comment(0)
C
11

You can use Path like this

const path = require('path'); 
path.join(__dirname, "../");
Chamber answered 16/2, 2021 at 9:12 Comment(0)
S
9

if you are sending the path as a string,

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

this doesn't work.

Instead you have to use a comma, (the plus sign concatenates the two strings)

configFile: path.join(__dirname, '../test/karma.conf.js'),
Sammysamoan answered 6/4, 2018 at 15:3 Comment(0)
O
8

Like Pranav Totla said, hardcode the path with forward slashes ( "/" ) or backward slashes ( "\" ) makes the application prone to errors when it came across different operating systems.

Use the built in "path" module to prevent errors.

// Import "path"
const path = require('path');

// To go down on the three from index.html:
path.join(__dirname, 'css', 'style.css')

// To go up on the three from style.css:
path.join(__dirname, '..', 'img', 'cat.jpg')

// Three
root/
| |_css/
| |_img/
|
|_index.html
Ossiferous answered 29/7, 2021 at 10:1 Comment(0)
P
7

from Root directory

(path.join(__dirname , 'views' ,'main.html')) -> will return Root:/views/main.html

from any sub-folder of Root

(path.join(__dirname , '../views/main.html')) -> same as above
Poppas answered 8/7, 2018 at 9:7 Comment(0)
M
5

we can use path module to go back one level from the current directory

Example:

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

__dirname -- present directory
..        -- one level
test      -- folder name
config.js -- file (test folder inside)
Marseilles answered 25/8, 2022 at 10:21 Comment(0)
S
3

Try putting a \\ before the ..\\.

Without it, the path your generating has a folder called WebApi... as part of it. You can see this in the path being output from the error message.

Like this:

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

You may also want to look into using the path library from npm. It makes combining paths a lot easier by handling adding and removing extra path separator characters as needed.

Scissile answered 15/6, 2015 at 12:44 Comment(1)
thank you for your response . however it didn't work out . this is the output i am getting for that ERROR [config]: File C:\Users\Documents\WebApiRole\..\test\karma.conf.js does not exist! as you can see it still considers ..\ as part of the path stringForename
G
2

Here is all you need to know about relative file paths:

Starting with / returns to the root directory and starts there

Starting with ../ moves one directory backward and starts there

Starting with ../../ moves two directories backward and starts there (and so on...)

To move forward, just start with the first sub directory and keep moving forward.

Gouty answered 26/6, 2020 at 20:59 Comment(0)
D
2

this will move you 2 directory back irrespective of any operating system:

import { join, sep } from 'path';
join(__dirname, sep, "..", sep, "..");
Dare answered 28/4, 2021 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.