Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

webpack cannot find its entry file ./src/index.js

ErrorModule not found: Error: Can't resolve './src/index.js' in '/home/user'

Short answer

Webpack resolves the entry from its context folder, which is the directory you ran the command in unless you set context. With no config file it expects src/index.js. So the error means the file is not where webpack looks. Run it from the project root, or set context and entry explicitly.

  • Applies to webpack 5, webpack-cli 6, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Compare the folder named at the end of the error with the folder that holds src/index.js. If they differ, you started webpack from the wrong place.

    pwd
    ls src/index.js
  2. 2Match what you see, then jump to its fix

    1. 1The folder in the error is a parent of your project, or an unrelated directorywebpack was started from a different folderFix
    2. 2No webpack.config.js is found, and the project keeps its code in another folder such as app/ or client/There is no config file and no src/index.jsFix
    3. 3Your config sets context and the error path contains that foldercontext points at the wrong folderFix

What does the error look like?

  • Module not found: Error: Can't resolve './src/index.js' in '/home/user'

Cause 1 of 3

webpack was started from a different folder

1Check

By default context is the Node.js working directory, so a relative entry is looked up from where you typed the command.

pwd
ls webpack.config.js package.json

2Fix

Change into the project folder first, or run the command through an npm script, which npm starts in the package folder.

cd path/to/project
npx webpack

3Verify

The build should now find the entry.

npx webpack --mode development

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 3

There is no config file and no src/index.js

1Check

Without a config file, webpack assumes the entry is src/index.js and the output is dist/main.js. A different layout needs a config file.

ls webpack.config.js
ls src

2Fix

Add a config file at the project root with the entry you really use.

// webpack.config.js
const path = require('path');
module.exports = {
  context: path.resolve(__dirname),
  entry: './client/index.js',
};

3Verify

Run the build.

npx webpack

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 3

context points at the wrong folder

1Check

The entry is resolved against context, so entry: './src/index.js' with context set to src/ looks for src/src/index.js.

grep -n "context\|entry" webpack.config.js

2Fix

Set context to an absolute path built from the config file's location, and write the entry relative to it.

const path = require('path');
module.exports = {
  context: path.resolve(__dirname, 'src'),
  entry: './index.js',
};

3Verify

Run the build from any folder, for example from the parent.

npx webpack --config ./webpack.config.js

Confirmed by webpack.js.org

Did this fix it?

How do I stop it coming back?

  • Always set context from __dirname so the build does not depend on the folder you start it in.
  • Run webpack through npm scripts instead of by hand.
  • Keep the entry path in one place and mention it in the README.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.