繁体   English   中英

错误代码:“MODULE_NOT_FOUND”试图在 Heroku 上部署机器人

[英]Error code: 'MODULE_NOT_FOUND' trying to deploy bot on Heroku

我正在尝试在 Heroku 上部署我的 Twitter Bot,但我不能。

我已经在 Heroku Vars 中配置了 Twitter 密钥,package.json 没问题......我已经在谷歌调查过,但我找不到解决方案。 我不知道我做错了什么。 有人可以帮我一把吗?

错误是这样的:

2020-09-04T09:05:15.326219+00:00 app[worker.1]:     at 
Object.Module._extensions..js 
(internal/modules/cjs/loader.js:1157:10)
2020-09-04T09:05:15.326219+00:00 app[worker.1]:     at 
Module.load (internal/modules/cjs/loader.js:985:32)
2020-09-04T09:05:15.326219+00:00 app[worker.1]:     at 
Function.Module._load 
(internal/modules/cjs/loader.js:878:14)
2020-09-04T09:05:15.326220+00:00 app[worker.1]:     at 
Function.executeUserEntryPoint [as runMain] 
(internal/modules/run_main.js:71:12)
2020-09-04T09:05:15.326221+00:00 app[worker.1]:     at 
internal/main/run_main_module.js:17:47 {
2020-09-04T09:05:15.326221+00:00 app[worker.1]:   code: 
'MODULE_NOT_FOUND',
2020-09-04T09:05:15.326221+00:00 app[worker.1]:   
requireStack: [ '/app/bot.js' ]
2020-09-04T09:05:15.326222+00:00 app[worker.1]: }
2020-09-04T09:05:15.389873+00:00 heroku[worker.1]: Process 
exited with status 1
2020-09-04T09:05:15.422459+00:00 heroku[worker.1]: State 
changed from up to crashed

包.json :

{
"name": "addicteddev",
"version": "1.0.0",
"description": "Addicted Dev - Twitter Bot",
"main": "bot.js",
"dependencies": {
"twit": "^2.2.11",
"twitter": "^1.7.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url":
"git+https://github.com/pablohs1986/
 addictedDEV_TwitterBot.git"
 },
"keywords": [
"bot",
"twitter",
"javascript",
"nodejs",
"dev"
],
"author": "Pablo Herrero",
"license": "MIT",
"bugs": {
"url": 
"https://github.com/pablohs1986/
 addictedDEV_TwitterBot/issues"
},
"homepage": 
"https://github.com/pablohs1986/
 addictedDEV_TwitterBot#readme"
}

机器人.js :

// Setup
    console.log('The bot is starting');

    var Twit = require('twit');

var config = require('./config/config.js');
var T = new Twit(config);

var addDevStatuses = [
    "asdasdasdsa"
];

let hashtags = [
    '#java', '#Java', '#js', '#javascript', '#JavaScript', '#python', '#Python', '#nodejs', '#nodeJS', '#100DaysOfCode', '#100daysofcode', '#100DOC', '#100doc', '#stackoverflow', 
    '#StackOverFlow', '#angular', '#Angular', '#VSCode', '#vscode', '#Netbeans', '#netbeans', '#Oracle', '#oracle', '#dev', '#Dev', '#developer', '#Developer', '#Development', '#development', '#sql', '#SQL'
]

// function randomStatus(){
//     return status = addDevStatuses[Math.floor(Math.random()*addDevStatuses.length)];
// }

function returnRandomElementFromArray(array){
    return status = array[Math.floor(Math.random()*array.length)];
}

function onlyUniqueTweets(value, index, self){
    return self.indexOf(value) === index;
}

// Post every 2 hours
function tweetIt(text){
    var params = {
        status: text
    }

    T.post('statuses/update', params, function (err, data){
        if(err){
            console.log("Shit!!");
            console.log(err);
        }else{
            console.log("It worked! Tweeting!!");
        }
    })
}

tweetIt(returnRandomElementFromArray(addDevStatuses));
setInterval(tweetIt, 1000*60*120, returnRandomElementFromArray(addDevStatuses));

// Retweet hashtags
function retweetHashtags(hashtag){
    var params = {
        q: hashtag + ' ',
        result_type: 'mixed',
        count: '5'
    }

    T.get('search/tweets', params, function(err_search, data_search, response_search){
        let tweets = data_search.statuses;
       
        if(err_search){
            console.log("Shit searching!!");
            console.log(err_search);
        }else{
            var tweetIDList = [];
            
            for(let tweet of tweets){
                if(tweet.text.startsWith("RT @")){
                    if(tweet.retweeted_status){
                        tweetIDList.push(tweet.retweeted_status.id_str);
                    }else{
                        tweetIDList.push(tweet.id_str);
                    }
                }else{
                    tweetIDList.push(tweet.id_str);
                }
            }

            tweetIDList = tweetIDList.filter(onlyUniqueTweets);

            console.log("TweetIDList = \n" + tweetIDList);

            for (let tweetID of tweetIDList) {
                T.post('statuses/retweet/:id', {id : tweetID}, function(err_rt, data_rt, response_rt){
                    if(!err_rt){
                        console.log("\n\nRetweeted! ID - " + tweetID);
                    }
                    else {
                        console.log("\nShit retweeting!! Duplication maybe... " + tweetID + "| HASHTAG - " + hashtag);
                        console.log("Error: " + err_rt);
                    }
                })
            }
            console.log("It worked! Hashtags retweeted!!!");
        }
    })
}

retweetHashtags(returnRandomElementFromArray(hashtags));
setInterval(retweetHashtags, 1000*60*10, returnRandomElementFromArray(hashtags));

抱歉,我找到了,所以我会尝试回答这个问题

最后是路由问题 在 Heroku 上部署机器人时,该程序无法访问 config.js 文件或 bot.js。

Config.js 不想将其公开,因为其中包含 Twitter 密钥。 最后我将其公开,但指的是 Config.env 文件,这是实际读取 Twitter 密钥的位置(存储在 Heroku Vars 中),我通过 [dotenv] 模块访问该文件(https://www .npmjs .com / package / dotenv),如下所示:

配置文件

const path = require('path')
const dotenv = require('dotenv')
require('dotenv').config();
// Load config
dotenv.config({ path: './config/config.env' })

module.exports = {
    consumer_key : process.env.API_KEY,
    consumer_secret : process.env.API_SECRET_KEY,
    access_token : process.env.ACCESS_TOKEN,
    access_token_secret : process.env.ACCESS_TOKEN_SECRET
};

一旦推送完成,它就完美地工作了。

如果将来有人发生类似的事情,希望这会有所帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM