Node.js · Modules
Error: Cannot find module
Node looked for something your code asked for and it was not there. Which kind of "something" it was tells you the fix: a package name, or a file path.
What you are seeing
Node.js
Error: Cannot find module 'discord.js'Node.js
Error: Cannot find module '/home/container/index.js'A package name
If the quoted name is a package, like 'discord.js' or 'dotenv', it was not installed where the bot runs. On a server that installs dependencies from your package.json, as ours does on deploy, the package is missing from the dependencies list in that file. That happens when it was installed globally, or the package.json you uploaded is an older copy.
Open package.json and check the package is under "dependencies". If not, run npm install with its name on your own machine, which adds it, and upload the updated file. Do not upload node_modules itself: it is large, and it is installed for you.
package.json
{
"main": "index.js",
"dependencies": {
"discord.js": "^14.16.0",
"dotenv": "^16.4.0"
}
}A file path
If the quoted name is a path — your own entry file, or something beginning with ./ — the file is not where Node looked. The usual causes:
The entry file is named differently. The server starts the file you told it to; if that is index.js and your bot is bot.js, or it sits in a src folder, update the entry file setting to match.
The project was uploaded one folder too deep. Uploading a folder can leave everything inside my-bot/ rather than at the top level, so package.json and index.js are not where the server expects them. Move the contents up.
Capital letters. Windows and macOS treat Commands and commands as the same folder; Linux, which servers run, does not. require('./Commands/ping') works on your machine and fails on a server when the folder is called commands. Match the case exactly.
FAQ
Questions
It works on my computer and not on the server.
That is the signature of the capital-letters problem or of a package installed globally on your computer. Check the exact spelling and case of the path, and that the package is in package.json.
What about "Cannot use import statement outside a module"?
A different problem: the code uses import syntax while Node is treating the file as CommonJS. Add "type": "module" to package.json, or use require instead.