You are currently viewing How to Fix Cannot Find Module for a Local Import in Node.js
Illustration of tracing a Node import from its source file to the correct destination.

How to Fix Cannot Find Module for a Local Import in Node.js

Node says it can’t findlabel, but you can see label.js in your editor. Before touching npm, look at the first error line and the file named after imported from. If the import begins with ./ or ../, Node is trying to find a file relative to the importer. Open that folder and check the exact path and extension. If there is no leading dot, Node may be looking for a package instead. That distinction is the quickest way out of most local import errors. You can reproduce it below with a directory calledtools, one JavaScript file, and a deliberately missing package that you create yourself. Run the cases separately so the first failed import doesn’t stop you from seeing the others.

Read the line that tells you what Node tried to load

Look for the part of the message closest to “Cannot find.” It can name the script you ran, a file imported from that script, or a package. Those point to three different checks.
A relative import points from the importing file to its tools directory, while a bare name points toward package lookup.
The leading dot changes what Node looks for. Trace a local import from the file that imports it.
    • The missing path is the entry script. If node src/local-typo.mjs names a file you never created, fix the command first. Node cannot get far enough to run its imports.
    • The error gives a path and an importer. If an ESM error says it cannot find a path ending in src/tools/label and points back tosrc/app.mjs, start in the src folder. Check the missing component against the file on disk.
    • The error says it cannot find a package. Look at the original import. tools/label.js resembles a folder path when you read it, but Node interprets it as a bare package specifier with a subpath. The local file needs ./tools/label.js.
Node often reports ERR_MODULE_NOT_FOUND for ESM resolution and MODULE_NOT_FOUND when a CommonJS require() cannot find its target. The latter also appeared when the example command named a missing entry script. Read the reported file or package as well as the code, especially in a project that mixes module formats or wraps the error before logging it. The focused examples use native Node.js modules. If the command shown in your output starts Python, head to the separate guide on Python import errors in VS Code on Windows. In a JavaScript project compiled by a bundler, find out whether Node is reading your source files directly or reading generated output before applying these rules.

Reproduce three local import errors in a clean folder

Create this small project. You don’t need any dependencies for the first three checks. The top-level package.json makes the example’s .js file an ES module. The entry file has the .mjs extension so its module format is explicit too.
module-resolution-check/
  package.json
  src/
    app.mjs
    tools/
      label.js
At the project root, create the following package.json.
{
  "name": "module-resolution-check",
  "version": "1.0.0",
  "private": true,
  "type": "module"
}
Put this export in src/tools/label.js.
export const label = "local import works";
In src/app.mjs, try one import at a time with console.log(label) beneath it. Run node src/app.mjs from the project folder after each edit. The verified check uses separate files for each form so you can reproduce a failure without commenting out other examples.
// Try one line at a time, with console.log(label) below it.
import { label } from "./tools/label";
import { label } from "../tools/label.js";
import { label } from "tools/label.js";
The first form gets to the right folder and stops at the wrong filename. Native Node ESM requires an extension for these relative or absolute file imports, so it does not search for label.js when the specifier ends in label. The second form already has an extension, but the two dots step up from src into the project root. That points at the wrong tools directory. The third form makes Node look for a package calledtools, even while the editor shows a local folder with that name. Update the import to point at the existing file. In this tree, you want to go from src/app.mjs into src/tools, so the correct form is:
import { label } from "./tools/label.js";
console.log(label);
Run the command again and look for local import works. In a real codebase, follow the import starting from the file shown in the error. A ./ inside src/routes/list.mjs starts in src/routes even when you launched Node from the project root. This is different from the first script path in the shell command, which is relative to your terminal’s current directory.

Use the filename your runtime sees

Check the case and the whole filename before you add packages. label.js, Label.js and label.mjs are different spellings. A mismatch can go unnoticed on a case-insensitive filesystem and fail on a case-sensitive build or deployment. Compare your import with the repository or build output, especially if your editor hides file extensions. If the specifier ends in the directory./tools, use an explicit file such as ./tools/index.js when that file exists. Native ESM does not import that directory by picking an index file for you. The Node ESM documentation on mandatory file extensions describes that rule. If you can run the app in your editor but it fails after a build, write down the path of the importing file in each run. A development runner might read src while the release command starts a file in dist. The same relative import can be correct in the source tree and fail if the generated folder is missing its sibling file. Inspect the actual release directory rather than changing an import to point back at a source folder that won’t be present on the deployed machine.

Show what a genuinely missing dependency looks like

Keep this in the practice project. Create a folder called packages/label. Its package.json declares the name that the app will import and gives that name a public entry point.
{
  "name": "@local-demo/label",
  "version": "1.0.0",
  "type": "module",
  "exports": "./index.js"
}
Save the following as packages/label/index.js.
export const label = "installed package works";
Create src/package-check.mjs as follows. Run it before installing anything. This time the error names the intended package, because the app is genuinely asking Node for a package that the project cannot resolve yet. The source folder under packages exists, but its presence alone does not make the bare name importable.
import { label } from "@local-demo/label";
console.log(label);
From the project root, install only this bundled folder and run the file again. The install below is offline and doesn’t run package scripts. It writes the dependency to this practice project.
npm install --offline --ignore-scripts --no-audit --no-fund ./packages/label
node src/package-check.mjs
On Windows, if PowerShell blocks the npm PowerShell shim, use npm.cmd in that same command. This runs the npm command wrapper without changing your shell’s execution policy. If Node runs but npm is unavailable, check your Node installation and reopen the terminal after correcting its path before proceeding with the package example. The second run prints installed package works. On a real project, inspect the nearest relevant package.json and run npm ls actual-package-name from the application or workspace that imports it. If the dependency is declared but not on disk, restore it according to the project’s install workflow. If you add something new, confirm the exact package name in that dependency’s documentation and add it to the application that actually uses it. The fixture flags are for an offline practice example. Follow the real project’s install policy for its dependencies. The npm install documentation covers local folders and package names.

When the package is already installed

Check whether the import uses the public package name or a deeper path such as library/internal/helper.js. A package can exist in the dependency tree but expose only selected entry points through its exports field. If the message says ERR_PACKAGE_PATH_NOT_EXPORTED, check the package’s documented import paths instead of searching its installed files for a way around that boundary. For a monorepo, check the workspace that imports the package. A dependency printed by a command in a sibling workspace doesn’t establish that it is available to this application. Local aliases need a similar check. Your editor or bundler may know what an alias means even when the runtime doesn’t. If an import only fails in a direct node run, inspect which tool is supposed to resolve the alias and whether you ran the built output. Node documents package entry points and explains how the nearest package metadata affects module format. Changing the entire project from CommonJS to ESM won’t fix a one-character path error. If the stack points to a file inside an installed package, check the importer before assuming the app is missing a dependency. Your application might have found the package successfully, then failed when that package tried to load one of its own files. Compare the installed version with the project’s lockfile and read the package’s setup or release notes. Treat a dependency’s missing bundled file differently from a name your own source code imports. Adding the missing name directly to the app can hide an incomplete build while leaving the original dependency problem in place.

Verify the fix without changing unrelated dependencies

First rerun the same entry command with one change. If the message now says a particular export was not provided, Node found the module and moved on to checking its exports. Compare the importer’s named or default import with what the file actually exports. That new error gives you a narrower problem to solve. Avoid changing the filename, export, and dependency tree all at once, because the next run won’t tell you which change mattered.
Node test output shows failures for a missing extension, wrong path and missing package, then passing corrected local and installed package imports.
The actual Node.js run distinguished missing files from missing packages and passed seven checks after applying the matching fixes.
The bundled fixture checked seven cases on Node.js v22.23.5. It captured a missing entry, the three failing local spellings, the corrected local import, and the local package before and after its offline install. These are plain JavaScript tests in a clean copy. If your problem only appears in CI or after upgrading Node, record the version, entry command, the importing file, and the file tree actually shipped there. The CyboGeek Node runtime migration check shows how to compare the same small fixture across versions without changing the scenario at the same time. On a shared project, check your candidate fix from the same directory and with the same command other developers use. If you corrected a local import, the only change may be one line and no dependency file should have to move. If the app needed a declared dependency, inspect the manifest and lockfile diff so the install that passed on your machine can be reproduced. That second run is also a good time to check that the app displays or logs the value you expected, rather than stopping at the absence of a stack trace. Use the information in the error to decide what to touch. Fix the shell command when the entry is missing. Fix the import when the existing local file has the wrong path or extension. Open the dependency tree only when Node really needs a package. That order usually gets you to one small, verifiable change.