我发现Firebase的云功能使用package.json
文件加载依赖项,但是在调用我的功能时会遇到错误:
Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/user_code/rest_api.js:2:17)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
这是我的index.js
文件:
const processor = require('./processor.js');
exports.processThing = functions.https.onRequest((req, res) => {
let param = req.body.param;
processor.process(param);
});
您在需要节点模块时不包括文件扩展名。您的require
应该阅读:
const processor = require('./processor');
Firebase CLI应该照顾其余的。它会像package.json
所需的依赖关系一样捆绑它。
您需要在包含以下命令的情况下安装express模块依赖项。 npm install Express 只需确保安装在右文件夹上意味着您需要将其安装到 function 文件夹
喜欢〜/nodejsprojects/amzfire/ function $ npm install expand express
然后您将在package.json
中看到"依赖项":{
"express": "^4.16.3"
}
多数民众赞成在!