【问题标题】:How to go back 1 folder level with __dirname?如何使用 __dirname 返回 1 个文件夹级别?
【发布时间】:2015-08-30 23:07:09
【问题描述】:

我正在使用gulp-karma 并面临一个简单的问题,但似乎找不到我做错了什么。

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.conf.js 文件的 test 文件夹。谁能让我明白我在这里做错了什么?

我遇到的错误

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

【问题讨论】:

标签: javascript node.js directory path gulp


【解决方案1】:

我正在使用 (path) NPM 进行上述用途......

只需要 path 在 js 文件中使用 npm。然后使用

let reqPath = path.join(__dirname, '../../../');//It goes three folders or directories back from given __dirname.

【讨论】:

【解决方案2】:

TL;DR

使用path.join(__dirname, '..', 'test', 'karma.conf.js')。禁止使用斜线。

长答案

正如许多答案所指出的,使用path 模块可能是最好的方法。 但是,这里的大多数解决方案都回到了使用斜杠,例如:

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

但是,这样做,您就违背了使用path 的目的。一个人使用path 进行操作,而与底层操作系统(Linux、Windows 等)无关。只是为了提供一些见解,您可以直接将路径操作作为字符串操作(例如__dirname + '../test/karma.conf.js'。您不这样做是因为 Linux 使用正斜杠( / ),Windows 使用反斜杠( \ )。这使您的应用程序跨操作系统移植时容易出错。

因此,更好的方法是:

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

当然,回来 - 防止在您的 path.join 中使用斜线,而是展开您的参数。

【讨论】:

  • 这是最好的答案。
【解决方案3】:

__dirname 只是一个字符串。可以使用../向上遍历文件夹结构,使用path.join解析路径

path = require('path')

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

【讨论】:

    【解决方案4】:

    从根目录

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

    从根目录的任何子文件夹

    (path.join(__dirname , '../views/main.html')) -> same as above
    

    【讨论】:

      【解决方案5】:

      如果您将路径作为字符串发送,

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

      这不起作用。

      您必须使用逗号,(加号连接两个字符串)

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

      【讨论】:

        【解决方案6】:

        你可以像这样使用路径

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

        【讨论】:

          【解决方案7】:

          尝试在..\\ 之前添加\\

          没有它,您生成的路径将有一个名为WebApi... 的文件夹作为其中的一部分。您可以在错误消息输出的路径中看到这一点。

          像这样:

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

          您可能还想研究使用来自 npm 的 path 库。通过根据需要处理添加和删除额外的路径分隔符,它使组合路径变得更加容易。

          【讨论】:

          • 感谢您的回复。但是没有成功。这是我为 ERROR [config]: File C:\Users\Documents\WebApiRole\..\test\karma.conf.js does not exist! 得到的输出,你可以看到它仍然认为 ..\ 作为路径字符串的一部分
          【解决方案8】:

          以下是您需要了解的有关相对文件路径的所有信息:

          以/开头返回根目录并从那里开始

          从 ../ 开始向后移动一个目录并从那里开始

          从 ../../ 开始向后移动两个目录并从那里开始(依此类推...)

          要继续前进,只需从第一个子目录开始并继续前进。

          【讨论】:

            【解决方案9】:

            无论任何操作系统,这都会将您移回 2 个目录:

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

            【讨论】:

              【解决方案10】:

              就像Pranav Totla 所说,使用正斜杠 ("/") 或反斜杠 ("") 对路径进行硬编码会使应用程序在跨操作系统移植时容易出错。

              // 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
              

              【讨论】:

                猜你喜欢
                • 2018-06-06
                • 1970-01-01
                • 2018-05-08
                • 1970-01-01
                • 2013-02-06
                • 1970-01-01
                • 2020-02-27
                • 2013-08-24
                • 2016-06-13
                相关资源
                最近更新 更多