ACLClouds
ACLClouds

Node.js shows MODULE_NOT_FOUND

Tell apart an uninstalled dependency, a wrong local import and a wrong working folder, then reinstall the dependencies or fix the path in your bot.

MODULE_NOT_FOUND gives the name of the package or file searched. The fix depends on the target: an npm package reinstalls, a local file requires a correct path.

Step-by-step procedure

  1. Read missing module

    If the name does not start with ./ or ../, it is usually an npm package. Otherwise, check the local file and its case.

  2. Check package.json

    The package should appear in dependencies, not just on your development machine.

  3. Reinstall cleanly

    On ACLClouds, dependencies are installed at start from package.json and the lockfile. To start from scratch, use “Reinstall dependencies” in the “Startup” tab; on your computer, npm ci does the same with a package-lock.json.

  4. Check directory

    package.json must be at the root of the bot files, where the platform installs the dependencies and starts the bot. Locally, run npm in that folder or use --prefix.

Add the package locally, then upload package.json and the lockfile

npm install discord.js dotenv
npm ls --depth=0

How the platform installs dependencies

You do not need to run any command. On every start, the platform compares package.json and the lockfile (package-lock.json, yarn.lock or pnpm-lock.yaml) with the last successful installation. If they changed, or if the Node.js version changed, the console shows “Installing dependencies (npm install)” and then “Dependencies installed in N s”.

If the installation fails, the console shows “Dependency installation failed” with the relevant error line. The Cannot find module that follows is then preceded by “Module not found: the last dependency installation failed”. Fix the installation error first: the missing module is only its consequence.

The “Install” button of the diagnosis

When the missing name is an npm package, the console opens the “Missing module” window with an “Install” button. It adds the package to the dependencies of package.json with the latest version, creates that file if it does not exist, then restarts the bot to install it.

  • Then replace latest with the version you tested locally, for example ^14.16.3, to avoid an unexpected major upgrade;
  • a path such as lodash/fp belongs to the lodash package, and @discordjs/voice is installed under that full name;
  • a name starting with ./, ../ or / is not a package: no button is offered, fix the path in your code.

Check that everything works

After the restart, the console should show the end of the installation with no npm error line, then the command that was run, for example $ node index.js, and the output of your bot. Locally, npm ls --depth=0 should list the package without the word missing.

Frequently asked questions

How do I fix Error: Cannot find module 'discord.js'?

The discord.js package is not installed next to the bot. Check that it is listed in the dependencies of package.json; if not, run npm install discord.js locally and upload package.json with the lockfile. On ACLClouds, the “Install” button of the “Missing module” window adds it for you, then restarts the bot.

What does Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'dotenv' mean?

It is the same error with ES modules (import). The dotenv package is imported by the code but missing from package.json, or its installation failed. Add it to dependencies, or use the “Install” button, then restart.

Why Cannot find module '/app/commands/ping' imported from /app/index.js when ping.js exists?

With import, Node.js does not add the extension: write ./commands/ping.js. With require, check the case of the folder and file names, because Commands and commands are two different folders on Linux.

I uploaded my node_modules folder and the bot still crashes: why?

A node_modules copied from Windows or macOS contains native modules built for another system, hence errors such as invalid ELF header. Delete this folder in Files, keep package.json and the lockfile, then click “Reinstall dependencies” in the “Startup” tab.

The installation fails with npm error 404 Not Found: what should I do?

The package name is misspelled or the requested version does not exist in the npm registry. Check the line in package.json, compare it with the package page on npmjs.com, correct it and restart the bot.