Search code examples
herokudiscord

Discord Bot and Heroku


Hi I need some help with my discord bot. I searched up the errors and tried to fix it, but it just doesn’t work. It could have been a coding error on my end. PLEASE HELP and Thanks! Link for the GitHub repository: https://github.com/Verggz/Electrolite

Edit: errors that keep occuring Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

2022-01-23T15:14:15.475287+00:00 app[worker.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2022-01-23T15:14:15.476783+00:00 app[worker.1]: /app/main.ts:1
2022-01-23T15:14:15.476784+00:00 app[worker.1]: import express from 'express';
2022-01-23T15:14:15.476785+00:00 app[worker.1]: ^^^^^^
2022-01-23T15:14:15.476785+00:00 app[worker.1]:
2022-01-23T15:14:15.476785+00:00 app[worker.1]: SyntaxError: Cannot use import statement outside a module
2022-01-23T15:14:15.476786+00:00 app[worker.1]:     at wrapSafe (internal/modules/cjs/loader.js:1001:16)
2022-01-23T15:14:15.476786+00:00 app[worker.1]:     at Module._compile (internal/modules/cjs/loader.js:1049:27)
2022-01-23T15:14:15.476786+00:00 app[worker.1]:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
2022-01-23T15:14:15.476787+00:00 app[worker.1]:     at Module.load (internal/modules/cjs/loader.js:950:32)
2022-01-23T15:14:15.476787+00:00 app[worker.1]:     at Function.Module._load (internal/modules/cjs/loader.js:790:12)
2022-01-23T15:14:15.476787+00:00 app[worker.1]:     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
2022-01-23T15:14:15.476788+00:00 app[worker.1]:     at internal/main/run_main_module.js:17:47

package.json:

{
  "name": "electrolite",
  "version": "0.0.1",
  "description": "literally just project scyll v2 but better",
  "main": "./build/main.js",
  "scripts": {
    "start": "node --max-old-space-size=512 ./build/main.js && export NODE_ENV=production",
    "dev": "tsc && node ./build/main.js",
    "bot": "node ./build/bot/main.bot.js",
    "botdev": "tsc && node ./build/bot/main.bot.js"
  },
  "keywords": [
    "minecraft"
  ],
  "author": "PenguinDetox",
  "license": "ISC",
  "dependencies": {
    "@discordjs/builders": "^0.11.0",
    "@discordjs/rest": "^0.2.0-canary.0",
    "axios": "^0.24.0",
    "discord-api-types": "^0.26.1",
    "discord.js": "^13.5.0",
    "express": "^4.17.2",
    "fs-extra": "^10.0.0",
    "hypixel-api-reborn": "^9.0.3",
    "prismarine-nbt": "^2.0.0",
    "set-interval-async": "^2.0.3"
  },
  "devDependencies": {
    "@types/express": "^4.17.13",
    "@types/fs-extra": "^9.0.13",
    "@types/node": "^17.0.4",
    "@types/set-interval-async": "^1.0.0"
  }
}

Procfile:

worker: node main.ts

Solution

  • I'm not totally sure what you're trying to do here, but the main issue at the moment is that you're asking Heroku to run your TypeScript code, not the compiled JavaScript. This is being done via your Procfile:

    worker: node main.ts
    

    You have a completely different command in your package.json:

    "start": "node --max-old-space-size=512 ./build/main.js && export NODE_ENV=production",
    

    This is probably closer to what you want, but we can't just use it like this.

    The && export NODE_ENV part is unlikely to do anything useful: it will wait until your code stops running, then export an environment variable, then exit. Let's remove it:

    "start": "node --max-old-space-size=512 ./build/main.js",
    

    Now the issue is that build/main.js doesn't exist. You need to tell Heroku to build it during deployment. Based on your dev script, I believe that just involves running tsc.

    Let's add a build script to do that:

    "build": "tsc",
    "start": "node --max-old-space-size=512 ./build/main.js",
    

    (I don't see typescript in your devDependencies, which means tsc might fail to run. Make sure to include all dependencies in your package.json.)

    Okay, so your build script will now tell Heroku to build your application during deployment and your build/main.js should exist. Your start script has been updated.

    Let's deal with your Procfile. Update it to match the new start script:

    worker: node --max-old-space-size=512 ./build/main.js
    

    Commit all those changes and redeploy.