the browser console says require is not defined
Uncaught ReferenceError: require is not definedShort answer
Browsers have no require function; it exists in Node.js. The error means code written for Node.js reached the browser without webpack turning it into a bundle. Load the built bundle, not your source file, and check that target is a browser target, because target node emits code that calls require.
Fix it fast
1Find out which cause you have
Open the console error and see which file it names. Then check which script the page really loads.
grep -n "<script" public/index.html dist/index.html2Match what you see, then jump to its fix
What does the error look like?
Uncaught ReferenceError: require is not definedrequire is not defined
Cause 1 of 2
The page loads source code instead of the bundle
1Check
webpack only rewrites require in files it bundles. A script tag aimed at src/index.js delivers raw Node.js-style code.
grep -rn "src=" public/index.html2Fix
Point the script tag at the output bundle (output.filename), and let webpack build it. webpack-dev-server does not add the tag for you.
<script src="/main.js"></script>3Verify
Reload; the console error is gone.
npx webpack serveConfirmed by webpack.js.org
Cause 2 of 2
target is node but the bundle runs in a browser
1Check
The node target generates code that uses Node.js require to load chunks. Look for target in the config, or a browserslist file that is not what you expect.
grep -n "target" webpack.config.js2Fix
Use the web target for code that runs in a browser (it is the default when target is not set).
module.exports = {
target: 'web',
};3Verify
Rebuild and open the page.
npx webpackConfirmed by webpack.js.org
How do I stop it coming back?
- Keep browser and server builds in separate configs, each with its own target.
- Always load the bundle from output.filename in HTML.
- Do not mix a Node.js-targeted config into a browser build.
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 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 fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix