Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

webpack cannot resolve a file and mentions the browser field

ErrorModule 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.

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

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1Can't resolve './Button' and a Button.tsx or Button.jsx file exists next to itThe extension is not in resolve.extensionsFix
    2. 2Can't resolve 'components/Button' (no ./ at the start)A bare import points at a project folder, not a packageFix
    3. 3Can'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

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 button

2Fix

Add the extensions you use. The '...' entry keeps webpack's defaults.

resolve: {
  extensions: ['.ts', '.tsx', '...'],
},

3Verify

Run the build; the import now resolves.

npx webpack

Confirmed by webpack.js.org

Did this fix it?

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 | head

2Fix

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 webpack

Confirmed by webpack.js.org

Did this fix it?

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-package

2Fix

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 webpack

Confirmed by webpack.js.org

Did this fix it?

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

Back 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 fix

Back 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 fix

Next step

Want this handled for good?

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