webpack cannot find its entry file ./src/index.js
Module 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.
Fix it fast
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.js2Match what you see, then jump to its fix
- 1
The folder in the error is a parent of your project, or an unrelated directorywebpack was started from a different folderFix - 2
No 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
Your config sets context and the error path contains that foldercontext points at the wrong folderFix
- 1
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.json2Fix
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 webpack3Verify
The build should now find the entry.
npx webpack --mode developmentConfirmed by webpack.js.org
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 src2Fix
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 webpackConfirmed by webpack.js.org
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.js2Fix
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.jsConfirmed by webpack.js.org
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 fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix