Javascript 如何使用 __dirname 返回 1 个文件夹级别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30845416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to go back 1 folder level wih __dirname?
提问by Malik
I am using gulp-karmaand facing a simple problem but cannot seems to find what i am doing wrong .
我正在使用gulp-karma并面临一个简单的问题,但似乎无法找到我做错了什么。
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 appendthe ..\to the folder direcotry without going 1 level back (which is the usual use of ..\). Following is the folder structure .
这是我正在使用的代码,我似乎无法在文件夹目录中返回 1 级。当我做它上面刚刚附加的..\文件夹direcotry没有去1个水平回(这是通常的使用的..\)。以下是文件夹结构。
parent|
test|karma.conf.js
webapirole|gulpfile.js
and my folder is inside the webapirolefolder . i want to go back 1 folder back and go inisde the testfolder which contains the karma.conf.js file. can anyone make me understand what i am doing wrong here ?
我的文件夹在webapirole文件夹内。我想返回 1 个文件夹并返回包含 karma.conf.js 文件的测试文件夹。谁能让我明白我在这里做错了什么?
error i am getting
我得到的错误
[18:06:32] Starting 'tdd'...
ERROR [config]: File C:\Users\Documents\WebApiRole..\test\karma.conf.js does not exist
回答by Vemula Abilash
I am using (path) NPM for the above usage......
我正在使用 ( path) NPM 用于上述用法......
simply require pathnpm in js file.Then use
只需要在 js 文件中添加路径npm。然后使用
let reqPath = path.join(__dirname, '../../../');//It goes three folders or directories back from given __dirname.
回答by Vikash Anand
__dirnameis just a string. you can use ../to traverse up the folder structure and path.join to resolve the path
__dirname只是一个字符串。您可以使用../遍历文件夹结构和 path.join 来解析路径
path = require('path')
configFile: path.join(__dirname, '../test/karma.conf.js'),
回答by dilanSachi
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'),
回答by dan brown
from Root directory
从根目录
(path.join(__dirname , 'views' ,'main.html')) -> will return Root:/views/main.html
from any sub-folder of Root
从 Root 的任何子文件夹
(path.join(__dirname , '../views/main.html')) -> same as above
回答by Jim Simon
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.
没有它,您生成的路径将有一个文件夹称为其中的WebApi...一部分。您可以在错误消息输出的路径中看到这一点。
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 pathlibrary from npm. It makes combining paths a lot easier by handling adding and removing extra path separator characters as needed.
您可能还想考虑使用npm 中的路径库。通过根据需要处理添加和删除额外的路径分隔符,它使组合路径变得更加容易。
回答by Pranav Totla
TL;DR
TL; 博士
Use path.join(__dirname, '..', 'test', 'karma.conf.js'). Prevent use of slashes.
使用path.join(__dirname, '..', 'test', 'karma.conf.js'). 防止使用斜线。
Long Answer
长答案
As a lot of answers have pointed out, using pathmodule is probably the best way.
However, most of the solutions here have gone back to using slashes like:
正如很多答案所指出的那样,使用path模块可能是最好的方法。但是,这里的大多数解决方案都重新使用斜杠,例如:
path.join(__dirname+'../test/karma.conf.js')
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.
但是,通过这样做,您违背了使用path. 无论底层操作系统(Linux、Windows 等)如何,都使用路径来执行操作。为了给一些真知灼见,可以直接做路径操作的字符串操作(比如__dirname + '../test/karma.conf.js',你不这样做,因为Linux使用斜杠(/),Windows使用反斜杠(\)。这使得你的应用程序容易出错当您跨操作系统移植它时。
Thus, the better way would be:
因此,更好的方法是:
path.join(__dirname, '..', 'test', 'karma.conf.js')
path.join(__dirname, '..', 'test', 'karma.conf.js')
And of course, coming back - prevent use of slashes in your path.join, instead spreadout your params.
当然,回来 - 防止在您的 中使用斜杠path.join,而是分散您的参数。
回答by Vasu Ch
just append \before ..\test.
只需\在..\test.

