node.js Windows中带有正斜杠的Nodejs绝对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34329149/
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
Nodejs absolute paths in windows with forward slash
提问by Megh Parikh
Can I have absolute paths with forward slashes in windows in nodejs? I am using something like this :
我可以在nodejs的windows中使用带正斜杠的绝对路径吗?我正在使用这样的东西:
global.__base = __dirname + '/';
var Article = require(__base + 'app/models/article');
But on windows the build is failing as it is requiring something like C:\Something\Something/apps/models/article. I aam using webpack. So how to circumvent this issue so that the requiring remains the same i.e. __base + 'app/models/src'?
但是在 Windows 上,构建失败了,因为它需要类似C:\Something\Something/apps/models/article. 我正在使用 webpack。那么如何规避这个问题,以便要求保持不变,即__base + 'app/models/src'?
回答by Vikas Bansal
I know it is a bit late to answer but I think my answer will help some visitors.
我知道现在回答有点晚了,但我认为我的回答会对一些访客有所帮助。
In Node.jsyou can easily get your current running file name and its directory by just using __filenameand __dirnamevariables respectively.
在Node.js其中,只需分别使用__filename和__dirname变量即可轻松获取当前运行的文件名及其目录。
In order to correct the forward and back slash accordingly to your system you can use pathmodule of Node.js
为了根据您的系统更正正斜杠和反斜杠,您可以使用path模块Node.js
var path = require('path');
Like here is a messed path and I want it to be correct if I want to use it on my server. Here the pathmodule do everything for you
就像这里是一个混乱的路径,如果我想在我的服务器上使用它,我希望它是正确的。这里的path模块为你做一切
var randomPath = "desktop//my folder/\myfile.txt";
var randomPath = "桌面//我的文件夹/\myfile.txt";
var correctedPath = path.normalize(randomPath); //that's that
console.log(correctedPath);
desktop/my folder/myfile.txt
desktop/my folder/myfile.txt
If you want the absolute path of a file then you can also use resolvefunction of pathmodule
如果你想要一个文件的绝对路径,那么你也可以使用模块的resolve功能path
var somePath = "./img.jpg";
var resolvedPath = path.resolve(somePath);
console.log(resolvedPath);
/Users/vikasbansal/Desktop/temp/img.jpg
/Users/vikasbansal/Desktop/temp/img.jpg
回答by Megh Parikh
I finally did it like this:
我终于这样做了:
var slash = require('slash');
var dirname = __dirname;
if (process.platform === 'win32') dirname = slash(dirname);
global.__base = dirname + '/';
And then to require var Article = require(__base + 'app/models/article');. This uses the npm package slash (which replaces backslashes by slashes in paths and handles some more cases)
然后要求var Article = require(__base + 'app/models/article');. 这使用了 npm 包斜杠(用路径中的斜杠替换反斜杠并处理更多情况)
回答by apet
it's 2020, 5 years from the question was published, but I hope that for somebody my answer will be useful. I've used the replace method, here is my code(express js project):
现在是 2020 年,距问题发布还有 5 年,但我希望我的回答对某人有用。我使用了替换方法,这是我的代码(express js 项目):
const viewPath = (path.join(__dirname, '../views/')).replace(/\/g, '/')
exports.articlesList = function(req, res) {
res.sendFile(viewPath + 'articlesList.html');
}
回答by Amadan
I recommend against this, as it is patching node itself, but... well, no changes in how you require things.
我建议不要这样做,因为它正在修补节点本身,但是......好吧,你需要的东西没有变化。
(function() {
"use strict";
var path = require('path');
var oldRequire = require;
require = function(module) {
var fixedModule = path.join.apply(path, module.split(/\/|\/));
oldRequire(fixedModule);
}
})();

