简体   繁体   中英

How do I obtain the path of a file in a Meteor package?

I know how to get the current directory from a Meteor package , but how do I get the path of a specific file in the project?

node's __dirname and __filename don't work in Meteor.

It is complicated.

  1. meteor run copies your project files to a tree of directories inside <project-dir>/.meteor/local/build , reorganizes them in non-obvious ways (eg. the private subdirectory in the original tree becomes the assets subdirectory) and mixes it in with various npm modules to create a bundle that can be executed as a nodejs project. Indeed, to avoid duplications, there is a .gitignore file automatically set up in the .meteor directory that tells git, if you use it for version control, not to copy the .meteor/local directory.

  2. The original project directory gets watched in case you change a file. The change then gets copied into the current project build directory and the project rebuilt.

  3. If you deploy to a remote system, the build gets copied to a server and then run.

process is usually a defined global server-side object, and works according to the node.js API, because the meteor server code is ultimately running in node.js.

So you can run console.log(process.cwd()); in your server-side to obtain the current working directory for the server process, usually something like:

~/<meteor project directory>/.meteor/local/build/programs/server

This suggests that when meteor run is done locally, original project files are in ../../../../../ , but don't use that as it may change in the future.

Instead, for the directory containing the original project files, you could use:

baseDir = process.cwd().replace(/\/\.meteor.*$/, '');

This will get the working directory, and truncate everything beginning with /.meteor

This won't work for a server deploy, though, because the original project tree is not needed on the server, only the build. Files that aren't intended to be client or server code could possibly be stuck in the private subdir, which as I mentioned becomes the assets subdir in the build. Ways to currently find files in the build is either manual inspection .meteor/local in a local run, or use of a JS library that calls or imitates gnu find.

Since you mentioned packages , I note that in the build, server-side package code finally ends up in:

~/<project-dir>/.meteor/local/build/programs/server/packages

and client side in:

~/<project-dir>/.meteor/local/build/programs/web.browser/packages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM