本文介绍了Heroku错误:找不到模块"./config/keys"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我部署了一个Nodejs应用程序,到目前为止它的后端到Heroku.我在浏览器中收到应用程序错误.当我运行 heroku日志时,我看到此错误:

I deployed a Nodejs application, the backend of it so far to Heroku. I am getting an application error in the browser. When I run heroku logs, I see this error:

Error: Cannot find module './config/keys'

所以我运行了 heroku运行'ls -al',我看到了:

So I run a heroku run 'ls -al' and I see this:

Running ls -al on ⬢ murmuring-temple-46226... up, run.3327 (Free)
total 284
drwx------   8 u6811 dyno   4096 Apr 21 11:41 .
drwxr-xr-x  15 root  root   4096 Apr 18 13:09 ..
-rw-------   1 u6811 dyno     21 Apr 21 11:37 .gitignore
drwx------   3 u6811 dyno   4096 Apr 21 11:37 .heroku
drwx------   2 u6811 dyno   4096 Apr 21 11:37 .profile.d
-rw-------   1 u6811 dyno      0 Apr 21 11:37 @1
-rw-------   1 u6811 dyno    574 Apr 21 11:37 index.js
drwx------   2 u6811 dyno   4096 Apr 21 11:37 models
drwx------ 261 u6811 dyno  12288 Apr 21 11:38 node_modules
-rw-------   1 u6811 dyno 235090 Apr 21 11:37 package-lock.json
-rw-------   1 u6811 dyno    565 Apr 21 11:37 package.json
drwx------   2 u6811 dyno   4096 Apr 21 11:37 routes
drwx------   2 u6811 dyno   4096 Apr 21 11:37 services

我确实看到我的 config 文件夹不在顶部的文件和文件夹列表中,但这可能是因为在我的 .gitignore 文件中因此它将忽略 keys.js 文件,还是我在代码库中引用了错误的代码?

I do see that my config folder is not in that list of files and folders up top, but that can be because in my .gitignore file, I have it so it will ignore the keys.js file, or could it be I have it referenced wrong in my code base?

这就是我在 index.js 中的要求:

const express = require('express');
const mongoose = require('mongoose');
const cookieSession = require('cookie-session');
const passport = require('passport');
const keys = require('./config/keys');
require('./models/User');
require('./services/passport');

mongoose.connect(keys.mongoURI);

const app = express();

app.use(
  cookieSession({
    maxAge: 30 * 24 * 60 * 60 * 1000,
    keys: [keys.cookieKey]
  })
);
app.use(passport.initialize());
app.use(passport.session());

require('./routes/authRoutes')(app);

const PORT = process.env.PORT || 5000;
app.listen(PORT);

这就是我在 services/passport.js 中引用它的方式:

and this is how I reference it in services/passport.js:

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');

const User = mongoose.model('users');

passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  User.findById(id).then(user => {
    done(null, user);
  });
});

// passport.use() is a generic register to make Passport
// aware of new strategy
// creates a new instance to authenticate users
passport.use(
  new GoogleStrategy(
    {
      clientID: keys.googleClientID,
      clientSecret: keys.googleClientSecret,
      callbackURL: '/auth/google/callback'
    },
    (accessToken, refreshToken, profile, done) => {
      User.findOne({ googleId: profile.id }).then(existingUser => {
        if (existingUser) {
          // we already have a record with given profile id
          done(null, existingUser);
        } else {
          // we dont have a user record with this id, make a new record
          new User({ googleId: profile.id })
            .save()
            .then(user => done(null, user));
        }
      });
    }
  )
);

这里的最佳实践是使用容纳API密钥和其他凭据的 config/keys.js 文件夹文件结构将应用程序成功部署到Heroku的最佳实践?

What is the best practice here for deploying an application successfully to Heroku with a config/keys.js folder file structure that houses API keys and other creds?

推荐答案

我要做的是从您的 .gitignore 文件中删除 keys.js .在您的 config 文件夹中创建另外两个文件.一种用于开发,另一种用于生产.将密钥放入开发文件中,然后让 .gitignore 忽略该开发文件.

What I would do is remove keys.js from your .gitignore file. Create two other files in your config folder. One for development and one for production. Put your keys in the development file and then have .gitignore ignore that development file.

然后在您的 keys.js 文件中创建一个 if 语句,如下所示:

Then in your keys.js file create an if statement like so:

// keys.js - figure out what set of credentials to return
if (process.env.NODE_ENV === 'production') {
  // we are in production - return the prod set of keys
  module.exports = require('./prod');
} else {
  // we are in development - return the dev keys!!
  module.exports = require('./dev');
}

然后在生产文件中,按照上面我的同事的建议进行操作,但是可能更像这样:

Then in your production file you are going to do as my colleague above suggested, but probably more like this:

// prod.js - production keys here
module.exports = {
  googleClientID: process.env.GOOGLE_CLIENT_ID,
  googleClientSecret: process.env.GOOGLE_CLIENT_SECRET,
  cookieKey: process.env.COOKIE_KEY
};

如果您还连接到MongoDB之类的数据库,那么您还想在上面加上 mongoURI:process.env.MONGO_URI .

If you are also connecting to a database like MongoDB then you want to add that as well above like this, mongoURI: process.env.MONGO_URI.

最后要做的是确保在Heroku环境变量上定义了所有这些不同的环境变量.

The last thing you have to do is make sure all these different environment variables are defined on your Heroku environment variables.

设置所有这些步骤非常简单,您只需要知道在Herokus的控制界面上查找的位置,或通过命令行终端(如我上面的同事所建议的那样)进行操作即可.如果您对命令行终端非常满意,请继续执行上述步骤,否则请进入浏览器并导航到 dashboard.heroku.com

Setting all these things up is straightforward, you just have to know where to look on Herokus’ control interface or do them via command line terminal as my colleague above suggested. If you are pretty comfortable with command line terminal, go ahead and follow the steps above, if not then go inside your browser and navigate to dashboard.heroku.com

找到为您的应用创建的应用,然后单击它.

Find the application you have created for your app, click on it.

然后点击设置标签,您应该会看到配置变量.单击显示配置变量",这将为您提供一个界面,以设置要添加到应用程序中的所有不同变量.

Then click on the settings tab and you should see Config Variables. Click on reveal Config Variables and that should give you an interface to set up all the different variables you want to add to your application.

您一个一个地添加一个键及其对应的值

One by one you add in a key and its corresponding value

因此,首先执行 GOOGLE_CLIENT_ID ,将其复制并输入为密钥,然后获取您的凭据.您应该已经将凭据副本粘贴到某个地方.

So first do the GOOGLE_CLIENT_ID, copy that and enter it as the key, then go grab your credentials. You should already have your credentials copy pasted somewhere.

COOKIE_KEY 执行相同的操作,如果您使用的是Mongo,则使用`MONGO_URI

Do the same for COOKIE_KEY and if you are using Mongo, `MONGO_URI

所以现在您可以隐藏配置变量,没有人可以看到它们.

So now you can hide config variables and no one will be able to see them.

您可能会想像您不希望任何人进入您的heroku帐户.如果有人进入您的heroku帐户,我不知道该告诉您什么.

As you might imagine you don’t want anyone getting into your heroku account. If someone gets into your heroku account, I don’t know what to tell you.

现在,您可以通过使用git提交代码来部署应用程序,也可以使用git来部署应用程序.

Now you can deploy your application by committing your code using git and deploy it using git as well.

执行显示您的文件和文件夹的 git状态.

Do a git status that shows your files and folders.

git add.

git commit -m完整的环境变量"

git push

git push heroku master

此错误消失后,您应该很好地解决此问题.

You should be good to go after this with that error having gone away.

这篇关于Heroku错误:找不到模块"./config/keys"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:14