javascriptnode.jsnpmherokudiscord.js

Error: cannot find module './discord.js' on Heroku


This is my first time deploying on Heroku, and I've come across this hurdle that won't let me successfully deploy my Discord bot. I've been following a tutorial, and the creator seemed to deploy his project on the platform with no problem. However, as with most things, it hasn't been as straightforward the past hour.

A screenshot of error logs on Heroku The just being the Error: cannot find module './discord.js'

I tried the solution from this answer, but no luck.

Here is my package.json:

    "name": "booster-bot",
    "version": "1.0.0",
    "main": "index.js",
    "engines": {
        "node": "12.14.1",
        "npm": "6.13.4"
    },
    "repository": {
        "type": "git",
        "url": "git+link"
    },
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node bot.js",
        "devStart": "nodemon bot.js"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "description": "",
    "dependencies": {
        "dotenv": "^16.4.5",
        "requirements-txt": "^0.0.5"
    },
    "devDependencies": {
        "nodemon": "^3.1.4"
    }
}

As well as the block of code it points back to:

const {
  Client,
  Events,
  GatewayIntentBits,
  EmbedBuilder,
} = require("./discord.js");

const client = new Client({
  partials: ["GUILD_MEMBER"],
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildPresences,
  ],
});

To note is that I initially did not have the ./ in the ./discord.js, but as per this other answer and my desperation, I thought I'd try it (didn't work). I also have the Procfile turned on and the requirements.txt installed, showing in this snippet on my package.json file:

    "name": "booster-bot",
    "version": "1.0.0",
    "lockfileVersion": 3,
    "requires": true,
    "packages": {
        "": {
            "name": "booster-bot",
            "version": "1.0.0",
            "license": "ISC",
            "dependencies": {
                "dotenv": "^16.4.5",
                "requirements-txt": "^0.0.5"
            },
            "devDependencies": {
                "nodemon": "^3.1.4"
            },
            "engines": {
                "node": "12.14.1",
                "npm": "6.13.4"
            }
        },

Thank you for any input/help!


Solution

  • Your package.json does not include discord.js

    Run the command npm install discord.js --save to install it and add it to your package.json file.

    Additionally, do not use relative path(./) to import a package, it would be best to import it using require('discord.js');