Heroku在heroku上找不到本地模块(Node.js)

时间:2020-12-20 20:38:46

I'm working on a Node.js app, and am using a config.js file that exposes my twitter API tokens/secrets. I'm doing this because I plan on open-sourcing the application after I finish it, and would like to keep those private (and thus, have put that file in my .gitignore.

我正在使用Node.js应用程序,并且使用的是一个公开我的twitter API令牌/秘密的config.js文件。我这样做是因为我计划在完成应用程序之后开源应用程序,并希望将这些应用程序保密(因此,将该文件放在我的.gitignore中)。

Anyways, to my question - I'm getting the following error log:

无论如何,对我的问题 - 我收到以下错误日志:

2012-04-09T22:41:12+00:00 app[web.1]: node.js:134
2012-04-09T22:41:12+00:00 app[web.1]:         ^
2012-04-09T22:41:12+00:00 app[web.1]:         throw e; // process.nextTick error, or 'error' event on first tick
2012-04-09T22:41:12+00:00 app[web.1]: Error: Cannot find module './config'
2012-04-09T22:41:12+00:00 app[web.1]:     at require (module.js:348:19)
2012-04-09T22:41:12+00:00 app[web.1]:     at Function._load (module.js:266:25)
2012-04-09T22:41:12+00:00 app[web.1]:     at Function._resolveFilename (module.js:320:11)
2012-04-09T22:41:12+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:6:19)
2012-04-09T22:41:12+00:00 app[web.1]:     at Module._compile (module.js:404:26)
2012-04-09T22:41:12+00:00 app[web.1]:     at Object..js (module.js:410:10)
2012-04-09T22:41:12+00:00 app[web.1]:     at Function._load (module.js:297:12)
2012-04-09T22:41:12+00:00 app[web.1]:     at EventEmitter._tickCallback (node.js:126:26)
2012-04-09T22:41:12+00:00 app[web.1]:     at Module.load (module.js:336:31)
2012-04-09T22:41:12+00:00 app[web.1]:     at Array.<anonymous> (module.js:423:10)
2012-04-09T22:41:13+00:00 heroku[web.1]: State changed from starting to crashed
2012-04-09T22:41:13+00:00 heroku[web.1]: Process exited with status 1

My file name is config.js, and I am doing a: var config = require('./config') in my app. I don't have it listed as a dependency in my package.json file, since it's not in NPM.

我的文件名是config.js,我在我的应用程序中执行:var config = require('./ config')。我没有将它列为我的package.json文件中的依赖项,因为它不在NPM中。

Running locally with foreman and the Procfile (like heroku's docs say to do) work great. But, I'm confused why it's giving me an error when I'm requiring a local file. config.js is in the same directory level as my app.js is, so the path to require it is correct.

在本地与工头和Procfile一起运行(就像heroku的文档说的那样)工作得很好。但是,当我需要一个本地文件时,我很困惑为什么它会给我一个错误。 config.js与我的app.js位于同一目录级别,因此要求它的路径是正确的。

EDIT: Below is the portion of code I am using in app.js:

编辑:下面是我在app.js中使用的代码部分:

var express     = require('express')
  , sys         = require('sys')
  , request     = require('request')
  , config      = require('./config')

Thanks - Any help would be greatly appreciated!

谢谢 - 任何帮助将不胜感激!

1 个解决方案

#1


14  

Because you put the config.js file in your .gitignore it is not being pushed to the Heroku server, like you said you had intended. For what you say you want to do, you would typically use Heroku's config vars to set environment variables that are then read in by your application:

因为你把config.js文件放在.gitignore中,所以它没有像你说的那样被推送到Heroku服务器。对于您想要做的事情,您通常会使用Heroku的配置变量来设置环境变量,然后由您的应用程序读取:

$ heroku config:add TWITTER_TOKEN=<token> TWITTER_SECRET=<secret>

Then in your app.js file you can access these environment variables with:

然后在app.js文件中,您可以使用以下命令访问这些环境变量:

var token = process.env['TWITTER_TOKEN'];
var secret = process.env['TWITTER_SECRET'];

#1


14  

Because you put the config.js file in your .gitignore it is not being pushed to the Heroku server, like you said you had intended. For what you say you want to do, you would typically use Heroku's config vars to set environment variables that are then read in by your application:

因为你把config.js文件放在.gitignore中,所以它没有像你说的那样被推送到Heroku服务器。对于您想要做的事情,您通常会使用Heroku的配置变量来设置环境变量,然后由您的应用程序读取:

$ heroku config:add TWITTER_TOKEN=<token> TWITTER_SECRET=<secret>

Then in your app.js file you can access these environment variables with:

然后在app.js文件中,您可以使用以下命令访问这些环境变量:

var token = process.env['TWITTER_TOKEN'];
var secret = process.env['TWITTER_SECRET'];