[英]Firebase Cloud Functions - "not defined" error for npm dependency & including a module with client side code
I'm developing an app with Firebase and I'm using Cloud Functions to generate a QR code, among other things.我正在使用Firebase开发一个应用程序,我正在使用Cloud Functions生成二维码等。
I have two problems :我有两个问题:
1, I'm trying to use the qrcodejs npm package in Cloud Functions. 1,我正在尝试在 Cloud Functions 中使用qrcodejs npm 包。
I ran sudo npm install --save qrcodejs
in my functions
directory.我在我的
functions
目录中运行了sudo npm install --save qrcodejs
。
This is the relevant part of the package.js
file in the functions
directory:这是
functions
目录中package.js
文件的相关部分:
"dependencies": {
"firebase-admin": "~5.8.1",
"firebase-functions": "^0.8.1",
"kjua": "^0.1.2",
"qrcodejs": "^1.0.0"
},
When the code is deployed and runs, I get a 'QRCode is not defined' error in the Cloud Functions log .当代码部署并运行时,我在Cloud Functions 日志中收到“QRCode is not defined”错误。
This is my functions/index.js file:这是我的函数/index.js文件:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var qrcodejs = require('qrcodejs')
exports.events = functions.https.onRequest((req, res) => {
const eventID = req.path
const promise = admin.database()
.ref('events' + eventID)
.once('value')
return promise.then(result => {
var qrcode = new QRCode("test", {
text: "http://jindo.dev.naver.com/collie",
width: 128,
height: 128,
colorDark : "#000000",
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.H
});
var html = `
<!doctype html>
<head>
<title>${result.val().title}</title>
</head>
<body>
${qrcode}</br>
${result.val().title}
</body>
</html>`
return res.status(200).send(html);
});
});
The error points to this line: var qrcode = new QRCode("test", {
. I'm pretty sure I'm doing the insertion into the html string wrong as well, but the error points to the line above.错误指向这一行:
var qrcode = new QRCode("test", {
。我很确定我在插入 html 字符串时也出错了,但错误指向上面的行。
2, I've tried using another qr code npm package too, kjua . 2,我也尝试过使用另一个二维码 npm 包, kjua 。 I'd much prefer to use kjua , but I'm having an additional problem with that.
我更喜欢使用kjua ,但我还有一个问题。
Upon running firebase deploy
I get the following error at the ' i functions: preparing functions directory for uploading...
' step:运行
firebase deploy
后,我在“ i functions: preparing functions directory for uploading...
”步骤中收到以下错误:
ReferenceError: window is not defined
It points to the second line of the functions/node_modules/kjua/dist/kjua.min.js
file.它指向
functions/node_modules/kjua/dist/kjua.min.js
文件的第二行。
The problem seems to be that the code is written as it were run in a browser.问题似乎是代码是在浏览器中运行时编写的。 Can I still include this package as a dependency in Cloud Functions somehow?
我仍然可以以某种方式将此包作为 Cloud Functions 的依赖项包含在内吗?
Thank you very much非常感谢你
For sake of Answer, for people like me you just need to replace为了回答,对于像我这样的人你只需要更换
window
with global
and it works in node.js / cloud functions.具有
global
的window
,它适用于 node.js / 云功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.