【发布时间】:2019-03-10 08:47:27
【问题描述】:
我在尝试部署时一直遇到这个问题。我最初能够部署一次,而 firebase 只有启动器“你好,世界”云功能。
我安装了 Node 8,我意识到这可能是我的问题。我搜索了一下,发现只要你指定引擎就可以了,例如:
package.json sn-p
"engines": {
"node": "8"
},
但是在我添加这个之后,我得到了相同的结果。
这是我的完整 package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"@google-cloud/storage": "^2.0.3",
"busboy": "^0.2.14",
"cors": "^2.8.4",
"firebase-admin": "^6.0.0",
"firebase-functions": "^2.0.5",
"request-promise": "^4.2.2",
"uuid": "^3.3.2"
},
"engines": {
"node": "8"
},
"private": true
}
这是我的函数文件夹中的 index.js
'use strict';
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
const functions = require('firebase-functions');
const cors = require('cors')({ origin: true });
const Busboy = require('busboy');
const os = require('os');
const path = require('path');
const fs = require('fs');
const fbAdmin = require('firebase-admin');
const uuid = require('uuid/v4');
// exports.helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
const gcconfig = {
projectId: 'rs-0001',
keyFilename: 'rs-0001.json'
};
const gcs = require('@google-cloud/storage')(gcconfig);
// exports.helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
fbAdmin.initializeApp({
credential: fbAdmin.credential.cert(require('./rs-0001.json'))
});
exports.storeImage = functions.https.onRequest((req, res) => {
return cors(req, res, () => {
if (req.method !== 'POST') {
return res.status(500).json({ message: 'Not allowed.' });
}
if (
!req.headers.authorization ||
!req.headers.authorization.startsWith('Bearer ')
) {
return res.status(401).json({ error: 'Unauthorized.' });
}
let idToken;
idToken = req.headers.authorization.split('Bearer ')[1];
const busboy = new Busboy({ headers: req.headers });
let uploadData;
let oldImagePath;
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
const filePath = path.join(os.tmpdir(), filename);
uploadData = { filePath: filePath, type: mimetype, name: filename };
file.pipe(fs.createWriteStream(filePath));
});
busboy.on('field', (fieldname, value) => {
oldImagePath = decodeURIComponent(value);
});
busboy.on('finish', () => {
const bucket = gcs.bucket('rs-0001.appspot.com');
const id = uuid();
let imagePath = 'images/' + id + '-' + uploadData.name;
if (oldImagePath) {
imagePath = oldImagePath;
}
return fbAdmin
.auth()
.verifyIdToken(idToken)
.then(decodedToken => {
return bucket.upload(uploadData.filePath, {
uploadType: 'media',
destination: imagePath,
metadata: {
metadata: {
contentType: uploadData.type,
firebaseStorageDownloadTokens: id
}
}
});
})
.then(() => {
return res.status(201).json({
imageUrl:
'https://firebasestorage.googleapis.com/v0/b/' +
bucket.name +
'/o/' +
encodeURIComponent(imagePath) +
'?alt=media&token=' +
id,
imagePath: imagePath
});
return null
})
.catch(error => {
return res.status(401).json({ error: 'Unauthorized!' });
});
});
return busboy.end(req.rawBody);
});
});
我已经使用了以前的建议来解决类似问题,建议转到 /functions/ 目录并执行 npm install,然后继续转到上一个目录并运行
firebase deploy --only functions
这让我回到了
i functions: preparing functions directory for uploading...
Error: Error parsing triggers: Cannot find module 'firebase-functions'
Try running "npm install" in your functions directory before deploying.
我已经在打开 --debug 的情况下运行它,以接收
i functions: preparing functions directory for uploading...
[2018-10-04T15:34:29.744Z] >>> HTTP REQUEST GET https://runtimeconfig.googleapis.com/v1beta1/projects/rs-0001/configs
[2018-10-04T15:34:31.249Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=UTF-8,vary=X-Origin, Referer, Origin,Accept-Encoding, date=Thu, 04 Oct 2018 15:34:31 GMT, server=ESF, cache-control=private, x-xss-protection=1; mode=block, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, alt-svc=quic=":443"; ma=2592000; v="44,43,39,35", accept-ranges=none, connection=close
Error: Error parsing triggers: Cannot find module 'firebase-functions'
Try running "npm install" in your functions directory before deploying.
我也试过了
cd functions && sudo npm i && cd .. && firebase deploy --only functions --debug
得到相同的结果。我花了几个小时似乎遇到了同样的问题,删除了 node_modules,单独安装了所有软件包等等。有人可以帮忙吗?
node -v
v8.12.0
npm -v
6.4.1
我在全球范围内安装了最新版本的 firebase-tools,并将项目的 .json 文件放在同一目录中...
【问题讨论】:
-
仅供参考:您不必更改函数文件夹即可运行 firebase CLI cmets。 CLI 将查找父文件夹以查找项目的根目录。
-
如果您从头开始创建一个新项目并将代码复制到其中并从那里开始工作会发生什么?
-
分享你的 package.json
标签: node.js firebase google-cloud-functions node-modules