Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i go to parent directory when using __dirname?

Directory structure :

  • WebApiRole
    • GulpFile.js
  • test
    • Karma.conf.js

Gulp code from GulpFile.js

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

So my problem going to the parent directory and access the karma.conf.js . For some reason the path is not get resolved with ..\\ to go back to the parent directory of WebApiRole . can someone point me in the right direction ?

like image 461
Malik Avatar asked Jun 16 '15 04:06

Malik


People also ask

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.

How do I go back in dirname?

To go back 1 folder level with __dirname in Node. js, we can use the path. join method. const reqPath = path.

How do I go back to current directory in node JS?

log(__dirname);//current directory where we open this folder. Now, we get our current directory but you are trying to move your directory one step back. You must use nodejs built in module path to work with directories. The method path.


1 Answers

I had to use path package to resolve this issue .

var path = require("path"),
    fs = require("fs");

gulp.task('test', function (done) {
    karma.start({
        configFile: fs.readFile(path.join(__dirname, '../test/', 'karma.conf.js')),
        singleRun: true
    }, done);
});
like image 100
Malik Avatar answered Nov 13 '22 20:11

Malik