node.js 查找项目目录的绝对基本路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18378809/
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
Find absolute base path of the project directory
提问by loomi
Until now we could get the absolute path of a file to open later as readStream with this code snippet:
到目前为止,我们可以使用以下代码片段获取稍后作为 readStream 打开的文件的绝对路径:
var base = path.resolve('.');
var file = base + '/data/test.csv';
fs.createReadStream(file)
Since Meteor 0.6.5 the base path is pointing to .meteor/local/build/programs/...
从 Meteor 0.6.5 开始,基本路径指向 .meteor/local/build/programs/...
There is also the Assets API, which but can not give us back a path but only the read document. We but need a stream to process some bigger data files?
还有资产API,它不能给我们返回路径,而只能返回读取的文档。我们但需要一个流来处理一些更大的数据文件?
采纳答案by loomi
Since version 1.3, the documentedfunction
从 1.3 版开始,记录的功能
Assets.absoluteFilePath(assetPath)
Assets.absoluteFilePath(assetPath)
seems to be the best way to get the project path reliably.
似乎是可靠地获得项目路径的最佳方式。
回答by Christian Fritz
Another way to find your project's root directory now is this:
现在找到项目根目录的另一种方法是:
var base = process.env.PWD
Note that this is not the same as process.cwd(). Instead it is the directory where you ran the meteorcommand, which is typically what you are looking for. Note also that this probably won't be very helpful when running your app from a deployed bundle.
请注意,这与process.cwd(). 相反,它是您运行meteor命令的目录,这通常是您要查找的目录。另请注意,从已部署的包中运行您的应用程序时,这可能不会很有帮助。
回答by 0x6A75616E
I ran into the same predicament when I updated to 0.6.5.
当我更新到 0.6.5 时,我遇到了同样的困境。
What I'm currently doing is getting the path like this:
我目前正在做的是获得这样的路径:
var meteor_root = Npm.require('fs').realpathSync( process.cwd() + '/../' );
This returns on dev mode:
这将返回开发模式:
/my/application/.meteor/local/build/programs
and on bundled mode:
和捆绑模式:
/my/application/build/app/programs
So from here I'm getting to my application's "root" path like so:
所以从这里我进入我的应用程序的“根”路径,如下所示:
var application_root = Npm.require('fs').realpathSync( meteor_root + '/../' );
// if running on dev mode
if( Npm.require('path').basename( Npm.require('fs').realpathSync( meteor_root + '/../../../' ) ) == '.meteor' ){
application_root = Npm.require('fs').realpathSync( meteor_root + '/../../../../' );
}
The only case in which this would fail is if you happen to name your application's folder ".meteor" but that's an edge case.
唯一会失败的情况是,如果您碰巧将应用程序的文件夹命名为“.meteor”,但这是一种极端情况。
Relative to that you can access whatever else you need to.
相对于此,您可以访问您需要的任何其他内容。
Additionally, you can also get direct access to to the assets folder that the meteor bundler creates:
此外,您还可以直接访问流星捆绑器创建的资产文件夹:
var assets_folder = meteor_root + '/server/assets/' + Npm.require('path').basename( application_root );
This is likely to be temporary as I expect better file/path interaction APIs to be added eventually..
这可能是暂时的,因为我希望最终会添加更好的文件/路径交互 API。
Hope that helps
希望有帮助
回答by svelandiag
Hey you do not need to hardcode like the above answers... take a look to This package
嘿,你不需要像上面的答案那样硬编码......看看这个包
After install it you can access the root path of your meteor just wih Meteor.rootPath
安装后你可以访问你的meteor 的根路径 Meteor.rootPath
回答by nshimiye
For Meteor 0.8.3,
对于流星 0.8.3,
__meteor_bootstrap__.serverDirgives out the working directory, when run in server mode.
__meteor_bootstrap__.serverDir在服务器模式下运行时给出工作目录。
example
例子
if (Meteor.isServer) {
console.log(__meteor_bootstrap__.serverDir);
}
回答by Manoj Rana
you could get project basic root path by
您可以通过以下方式获取项目基本根路径
process.env.PWD
回答by Konard
As of Meteor 1.2.1, this works for me:
从 Meteor 1.2.1 开始,这对我有用:
var absoluteBasePath = path.resolve('../../../../../.');
The same result using split:
使用相同的结果split:
var absoluteBasePath = path.resolve('.').split(path.sep + '.meteor')[0];
Using process.cwd():
使用process.cwd():
var absoluteBasePath = path.resolve(process.cwd(), '../../../../../');
var absoluteBasePath = path.resolve(process.cwd()).split(path.sep + '.meteor')[0];

