webpack cannot resolve a file and mentions the browser field
Module not found: Error: Can't resolve './utils/format' in '/app/src'Short answer
This line is only the last step of a failed lookup, not the cause. webpack could not find the file or package you imported, then checked the browser field in package.json for an alias and found none. Find the import named above it, then fix its extension, its base folder or the package entry fields.
Fix it fast
1Find out which cause you have
Ignore the browser line and read the first line of the error: it names the exact import and the folder webpack searched. Then check that the file exists there.
ls -la src/utils/2Match what you see, then jump to its fix
- 1
Can't resolve './Button' and a Button.tsx or Button.jsx file exists next to itThe extension is not in resolve.extensionsFix - 2
Can't resolve 'components/Button' (no ./ at the start)A bare import points at a project folder, not a packageFix - 3
Can't resolve 'some-package' in '/app/src' although node_modules/some-package existsA package's entry fields point to a file it does not containFix
- 1
What does the error look like?
Module not found: Error: Can't resolve './utils/format' in '/app/src'Field 'browser' doesn't contain a valid alias configuration
Cause 1 of 3
The extension is not in resolve.extensions
1Check
webpack only tries the extensions in resolve.extensions (default .js, .json, .wasm). A .ts, .tsx or .jsx file is not found by a bare import.
ls src/components/ | grep -i button2Fix
Add the extensions you use. The '...' entry keeps webpack's defaults.
resolve: {
extensions: ['.ts', '.tsx', '...'],
},3Verify
Run the build; the import now resolves.
npx webpackConfirmed by webpack.js.org
Cause 2 of 3
A bare import points at a project folder, not a package
1Check
An import without ./ is looked up in node_modules, not in src.
grep -rn "from 'components/" src | head2Fix
Either use a relative path, add src to resolve.modules, or define an alias.
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
// or: alias: { Components: path.resolve(__dirname, 'src/components/') },
},3Verify
Rebuild and check the error for that import is gone.
npx webpackConfirmed by webpack.js.org
Cause 3 of 3
A package's entry fields point to a file it does not contain
1Check
For web builds webpack reads the browser, module and main fields in that order. Open the package's package.json and check that the file named there exists.
grep -n -E "\"(browser|module|main)\"" node_modules/some-package/package.json
ls node_modules/some-package2Fix
Reinstall the package first. If its browser field is the broken one, make webpack skip it so it falls through to module or main.
rm -rf node_modules/some-package && npm install
# webpack.config.js, only if the browser field stays broken:
resolve: { mainFields: ['module', 'main'], aliasFields: [] },3Verify
Build again; the package resolves.
npx webpackConfirmed by webpack.js.org
How do I stop it coming back?
- Read the first line of a webpack resolve error: it carries the real import and folder.
- List every extension your source uses in resolve.extensions.
- Use relative paths or an alias for your own folders; keep bare names for packages.
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
webpack cannot resolve an import (Module not found)
Module not found: Error: Can't resolve 'react-dom' in '/app/src'
Can't resolve means webpack searched every place its resolver knows and found no file.
4 causes, each with a fixBack end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fix