如何使用__ dirname 返回1个文件夹级别?

我使用 因果报应和面对一个简单的问题,但似乎不能找到我做错了什么。

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

这是我使用的代码,我似乎不能去1级回到文件夹目录。当我做以上它只是 附录..\文件夹目录没有去1级回来(这是通常使用的 ..\)。下面是文件夹结构。

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

我的文件夹在 Webapirole文件夹里。我想回到1个文件夹,然后进入包含 karma.con.js 文件的 测试文件夹。有人能让我明白我做错了什么吗?

我得到的错误

[18:06:32] Starting 'tdd'...
ERROR [config]: File C:\Users\Documents\WebApiRole..\test\karma.conf.js does not exist
104997 次浏览

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.

__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'),

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.

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'),

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

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.

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.

You can use Path like this

const path = require('path');
path.join(__dirname, "../");

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

import { join, sep } from 'path';
join(__dirname, sep, "..", sep, "..");

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

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)