Mal*_*lik 10 directory directory-structure karma-runner gulp gulp-karma
我正在使用gulp-karma并面临一个简单的问题,但似乎无法找到我做错了什么.
gulp.task('test', function (done) {
karma.start({
configFile: __dirname + '..\\test\\' +'\karma.conf.js',
singleRun: true
}, done);
});
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的代码,我似乎无法在文件夹目录中返回1级.当我做它上面刚刚附加的..\文件夹direcotry没有去1个水平回(这是通常的使用的..\).以下是文件夹结构.
parent|
test|karma.conf.js
webapirole|gulpfile.js
Run Code Online (Sandbox Code Playgroud)
我的文件夹在webapirole文件夹中.我想返回1个文件夹并进入包含karma.conf.js文件的测试文件夹.任何人都可以让我明白我在这里做错了什么?
我得到的错误
[18:06:32] Starting 'tdd'...
ERROR [config]: File C:\Users\Documents\WebApiRole..\test\karma.conf.js does not exist
Run Code Online (Sandbox Code Playgroud)
Pra*_*tla 43
TL; 博士
使用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,而是分散您的参数。
小智 18
我正在使用(路径)NPM进行上述使用......
只需要在js文件中使用路径 npm.然后使用
let reqPath = path.join(__dirname, '../../../');//It goes three folders or directories back from given __dirname.
Run Code Online (Sandbox Code Playgroud)
小智 11
您可以像这样使用路径
const path = require('path');
path.join(__dirname, "../");
Run Code Online (Sandbox Code Playgroud)
Vik*_*and 10
__dirname只是一个字符串.您可以使用../遍历文件夹结构和path.join来解析路径
path = require('path')
configFile: path.join(__dirname, '../test/karma.conf.js'),
Run Code Online (Sandbox Code Playgroud)
小智 7
从根目录
(path.join(__dirname , 'views' ,'main.html')) -> will return Root:/views/main.html
Run Code Online (Sandbox Code Playgroud)
从 Root 的任何子文件夹
(path.join(__dirname , '../views/main.html')) -> same as above
Run Code Online (Sandbox Code Playgroud)
正如Pranav Totla所说,使用正斜杠(“/”)或反斜杠(“\”)对路径进行硬编码会使应用程序在遇到不同操作系统时容易出错。
使用内置的“路径”模块来防止错误。
// 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
Run Code Online (Sandbox Code Playgroud)
如果您将路径作为字符串发送,
configFile: path.join(__dirname+'../test/karma.conf.js'),
Run Code Online (Sandbox Code Playgroud)
这不起作用。
相反,您必须使用逗号,(加号连接两个字符串)
configFile: path.join(__dirname, '../test/karma.conf.js'),
Run Code Online (Sandbox Code Playgroud)
我们可以使用path模块从当前目录返回上一级
例子:
path.join(__dirname, '..', 'test', 'conf.js')
__dirname -- present directory
.. -- one level
test -- folder name
config.js -- file (test folder inside)
Run Code Online (Sandbox Code Playgroud)
\\尝试在 之前加上..\\。
如果没有它,您生成的路径将包含一个名为的文件夹WebApi...。您可以在错误消息输出的路径中看到这一点。
像这样:
gulp.task('test', function (done) {
karma.start({ configFile: __dirname + '\\..\\test\\' +'\karma.conf.js', singleRun: true }, done);
});
Run Code Online (Sandbox Code Playgroud)
您可能还想研究使用npm 中的路径库。通过根据需要添加和删除额外的路径分隔符,它使组合路径变得更加容易。
| 归档时间: |
|
| 查看次数: |
22608 次 |
| 最近记录: |