我已经在heroku网站上设置了变量,并且在本地副本上运行命令heroku config
时,它确实显示了配置变量。
] 2
尽管当我使用git push heroku
/ git push heroku master
将项目部署到heroku时,我在日志中看到了以下错误信息:
2020-06-10T13:16:12.186235+00:00 heroku[web.1]: State changed from crashed to starting
2020-06-10T13:16:17.543064+00:00 heroku[web.1]: Starting process with command `npm start`
2020-06-10T13:16:20.148859+00:00 heroku[web.1]: Process exited with status 1
2020-06-10T13:16:20.193680+00:00 heroku[web.1]: State changed from starting to crashed
2020-06-10T13:16:19.808280+00:00 app[web.1]:
2020-06-10T13:16:19.808328+00:00 app[web.1]: > chatapp@1.0.1 start /app
2020-06-10T13:16:19.808329+00:00 app[web.1]: > node server.js
2020-06-10T13:16:19.808330+00:00 app[web.1]:
2020-06-10T13:16:20.081540+00:00 app[web.1]: /app/node_modules/firebase-admin/lib/auth/credential.js:134
2020-06-10T13:16:20.081560+00:00 app[web.1]: throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
2020-06-10T13:16:20.081561+00:00 app[web.1]: ^
2020-06-10T13:16:20.081561+00:00 app[web.1]:
2020-06-10T13:16:20.081562+00:00 app[web.1]: FirebaseAppError: Service account object must contain a string "project_id" property.
2020-06-10T13:16:20.081581+00:00 app[web.1]: at FirebaseAppError.FirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:42:28)
2020-06-10T13:16:20.081583+00:00 app[web.1]: at FirebaseAppError.PrefixedFirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:88:28)
2020-06-10T13:16:20.081583+00:00 app[web.1]: at new FirebaseAppError (/app/node_modules/firebase-admin/lib/utils/error.js:123:28)
2020-06-10T13:16:20.081583+00:00 app[web.1]: at new ServiceAccount (/app/node_modules/firebase-admin/lib/auth/credential.js:134:19)
2020-06-10T13:16:20.081584+00:00 app[web.1]: at new ServiceAccountCredential (/app/node_modules/firebase-admin/lib/auth/credential.js:68:15)
2020-06-10T13:16:20.081584+00:00 app[web.1]: at Object.cert (/app/node_modules/firebase-admin/lib/firebase-namespace.js:219:58)
2020-06-10T13:16:20.081584+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:27:32)
2020-06-10T13:16:20.081585+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1118:30)
2020-06-10T13:16:20.081608+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1138:10)
2020-06-10T13:16:20.081610+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:982:32) {
2020-06-10T13:16:20.081610+00:00 app[web.1]: errorInfo: {
2020-06-10T13:16:20.081610+00:00 app[web.1]: code: 'app/invalid-credential',
2020-06-10T13:16:20.081611+00:00 app[web.1]: message: 'Service account object must contain a string "project_id" property.'
2020-06-10T13:16:20.081611+00:00 app[web.1]: },
2020-06-10T13:16:20.081611+00:00 app[web.1]: codePrefix: 'app'
2020-06-10T13:16:20.081612+00:00 app[web.1]: }
2020-06-10T13:16:20.089016+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-06-10T13:16:20.089406+00:00 app[web.1]: npm ERR! errno 1
2020-06-10T13:16:20.090535+00:00 app[web.1]: npm ERR! chatapp@1.0.1 start: `node server.js`
2020-06-10T13:16:20.090694+00:00 app[web.1]: npm ERR! Exit status 1
2020-06-10T13:16:20.090876+00:00 app[web.1]: npm ERR!
2020-06-10T13:16:20.091011+00:00 app[web.1]: npm ERR! Failed at the chatapp@1.0.1 start script.
2020-06-10T13:16:20.091144+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-06-10T13:16:20.099174+00:00 app[web.1]:
2020-06-10T13:16:20.099385+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-06-10T13:16:20.099505+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-06-10T13_16_20_092Z-debug.log
当我在本地运行服务器时,它工作正常,也许是因为我有一个.env文件,它应该被git和.gitignore忽略。 服务器获取Firebase配置变量:
require("dotenv").config({silent: process.env.NODE_ENV === 'production'});
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert({
"type": process.env.FIREBASE_TYPE,
"project_id": process.env.FIREBASE_PROJECT_ID,
"private_key_id": process.env.FIREBASE_PRIVATE_KEY_ID,
"private_key": process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
"client_email": process.env.FIREBASE_CLIENT_EMAIL,
"client_id": process.env.FIREBASE_CLIENT_ID,
"auth_uri": process.env.FIREBASE_AUTH_URI,
"token_uri": process.env.FIREBASE_TOKEN_URI,
"auth_provider_x509_cert_url": process.env.FIREBASE_AUTH_PROVIDER_X509_CERT_URL,
"client_x509_cert_url": process.env.FIREBASE_CLIENT_X509_CERT_URL,
}),
databaseURL: "https://chatapp-skysibe.firebaseio.com"
});
var db = admin.database();
我尝试使用.env推送到git和heroku,并且工作正常,但是我的数据库不安全。