Node.js:如何让子文件已导出函数在主文件直接可见?(Firebase Functions场景)
Got it, I totally get what you're dealing with here—Firebase Functions relies on the exports in your root index.js to recognize which functions to deploy, so just importing your post.js won't automatically make that function available. Let's fix this in a couple of straightforward ways:
Option 1: Explicitly Re-export the Function
This is the most direct approach if you only need to export that single post function. In your index.js, add these lines:
// index.js // Import the post function from your post.js file const { post } = require('./post'); // Re-export it so Firebase can pick it up exports.post = post;
Or you can even condense it into one line if you prefer:
// index.js exports.post = require('./post').post;
Option 2: Merge All Exports from post.js
If you plan to add more functions to post.js later, this method saves you from having to re-export each one individually. It merges all exports from post.js into index.js's exports object:
// index.js // Merge all exports from post.js into the root exports Object.assign(exports, require('./post'));
Why This Works
Each Node.js module has its own exports object—when you define exports.post in post.js, that's only scoped to that module. Firebase's deployment tool only looks at the exports in your root index.js, so we need to explicitly bring that function into the root module's exports for it to be recognized as a deployable cloud function.
内容的提问来源于stack exchange,提问作者Relm

