Find absolute base path of the project directory
Solution 1
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 meteor
command, 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.
Solution 2
I ran into the same predicament when I updated to 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.
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..
Hope that helps
Solution 3
Since version 1.3, the documented function
Assets.absoluteFilePath(assetPath)
seems to be the best way to get the project path reliably.
Solution 4
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
Solution 5
For Meteor 0.8.3,
__meteor_bootstrap__.serverDir
gives out the working directory, when run in server mode.
example
if (Meteor.isServer) {
console.log(__meteor_bootstrap__.serverDir);
}
loomi
Updated on July 09, 2022Comments
-
loomi almost 2 years
Until now we could get the absolute path of a file to open later as readStream with this code snippet:
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/...
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?
-
loomi almost 11 yearsNice solution, but definitely something the framework needs to handle in the future. Are we on the same page here?
-
0x6A75616E almost 11 years@loomi Yes, absolutely. I think they'll need better than "get file contents" APIs to make this a more usable framework. The good news is there's always reasonable workarounds ;)
-
Christian Fritz over 10 yearsI don't know. I only just discovered this myself in the current version.
-
anshuman over 10 yearsIts not Node application root path. only directory path and depends where you are executing this code.
-
Christian Fritz over 10 years@anshuman: can you explain what you mean by "directory path"? also, which places are you thinking about when saying that it depends on where it is executed? clearly, this is not meant for the client, if that's what you are thinking.
-
anshuman over 10 years@ChristianFritz , The Op question is to find absolute base path of 'project' directory not current working directory. Your solution would give path for current working directory only. as process.env.PWD is nothing but PWD env variable being set for current working directory (at least on linux. not sure about windows though.)
-
zch about 10 years@ChristianFritz Thank you for the advice. However this only seems to work when running my Meteor app locally. The bundled app deployed at *.meteor.com doesn't seem to accept this method. Any thoughts?
-
krivar over 9 yearsin Meteor 1.0 it says
__meteor_bootstrap__ is not defined
-
Mike Cluck almost 9 yearsWhile this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
-
svelandiag almost 9 yearsits not a link only answer, I told him how to access the root.path with that package...
-
Konard over 8 yearsOn windows it is undefined.
-
eomeroff over 8 yearsYeah, what is for windows?
-
Konard over 8 yearsThis one stackoverflow.com/questions/18378809/… works for Windows
-
dudewad over 8 yearsAnd then as of Meteor 1.2.1x it could break again if they change their conventions. Using the system to do it it much safer.
-
Konard over 8 yearsYes, sure. How are you going to do such system? Can you offer alternative solution?
-
dudewad over 8 yearsUnless your use case is less common, I would thing that the accepted answer of process.env.PWD should do the trick. But, if you run meteor from a different directory than your base directory (if that's even something anyone would even do), then you might have to get fancier.
-
Konard over 8 years
process.env.PWD
does not work on Windows. And calling meteor from different directory is also possible. So do you have any ideas on how to solve the universal case? -
dudewad over 8 yearsThat's a good point. It should work everywhere. This might be a stupid idea but you could get the PWD equivalent in windows using the "solution" provided by the question of this thread: github.com/nodejs/node-v0.x-archive/issues/2305 -- the problem there is that it's ugly. But you could use PWD in that case by calling that function when meteor starts, and adding it to Meteor.settings. Not pretty, but potentally better than having to hunt down 40 instances of path.resolve('../../../../../../.') in a year when the directory structure changes again.
-
dudewad over 8 yearsI guess the point of what I'm saying is that hard coding paths = dangerous, IMO. Doesn't mean I know how to fix it, unfortunately.
-
Konard over 8 yearsIf you look carefully I'm actually using
process.cwd()
as suggested in github.com/nodejs/node-v0.x-archive/issues/2305 at every line of code at the answer. There is also a reason why I specified a Meteor version, it is working and will work forever for that specific version. So if this is the reason you downvoted the answer I strongly disagree with you. -
Konard over 8 yearsAlso
path.resolve('.').split('/.meteor')[0];
gives predictable result even if the path will change inside project's folder (as long as it is in project's folder). Because we actually need first directory that contain.meteor
folder and it seems that this actually will work longer thanpath.resolve('../../../../../../.')
. -
Konard over 8 yearsIf you need a 100% guarantee to have correct result you always can make a test, for just that part of code, and move it to function or package. So as soon test starts to fail the only thing you should do is to find a better solution and fix it in one place. Why you ever will need 40 instances of that?
-
Konard over 8 yearsAs of Meteor 1.2.1 on Windows
__meteor_bootstrap__
is defined and equal topath.resolve('.')
andprocess.cwd()
. -
Konard over 8 yearsDoes not work on Windows as of Meteor 1.2.1: github.com/VeliovGroup/Meteor-root/issues/3.
-
Konard over 8 yearsFixed at ostrio:meteor-root 1.0.3
-
Christian Fritz about 8 yearswhat is
assetPath
? Can I use this even when I don't have any assets? -
loomi about 8 yearsThis function returns the path of an Asset. This is finally the clean version to find the path of a file with which you like to work in your Meteor Application. If you need the absolut path without the need to address an asset I guess you create a dummy Asset and strip the non necessary part.
-
Christian Fritz about 8 yearswell, that's nice but the original question was about the project base path, not the assets path. So actually this answer is wrong. This should not be the accepted answer.
-
loomi about 8 yearsKinda true, but I guess most people need the basepath to access some specific files related to an application. So I think it is the most robust method longterm. (See also comments on question itself.)