Skip to content

Buffer is not defined Error ​

The "Uncaught ReferenceError: Buffer is not defined" error occurs when using Node.js-specific modules like Buffer in browser environments. This became more common after Webpack 5 stopped automatically polyfilling Node.js core modules.

Root Cause ​

In Webpack 4 and earlier, Node.js core modules like Buffer, stream, and process were automatically polyfilled for browser compatibility. Webpack 5 removed this automatic polyfilling to reduce bundle size, requiring explicit configuration when these modules are needed.

Solutions ​

For Webpack-based projects, configure polyfills in your webpack.config.js:

javascript
const webpack = require('webpack');

module.exports = {
  // ... other config
  resolve: {
    extensions: ['.ts', '.js'],
    fallback: {
      "stream": require.resolve("stream-browserify"),
      "buffer": require.resolve("buffer"),
      "process": require.resolve("process/browser")
    }
  },
  plugins: [
    new webpack.ProvidePlugin({
      Buffer: ['buffer', 'Buffer'],
      process: 'process/browser',
    }),
  ],
};

WARNING

After modifying Webpack configuration, always delete node_modules and run npm install again to ensure proper dependency resolution.

React Projects with Create React App ​

For React projects using react-scripts, you'll need to use react-app-rewired to customize Webpack:

  1. Install dependencies:
bash
npm install buffer process stream-browserify react-app-rewired --save
  1. Update package.json scripts:
json
{
  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject"
  }
}
  1. Create config-overrides.js:
javascript
const webpack = require("webpack");

module.exports = function override(config, env) {
  config.resolve.fallback = {
    ...config.resolve.fallback,
    stream: require.resolve("stream-browserify"),
    buffer: require.resolve("buffer"),
  };
  
  config.plugins = [
    ...config.plugins,
    new webpack.ProvidePlugin({
      process: "process/browser",
      Buffer: ["buffer", "Buffer"],
    }),
  ];

  return config;
};

Vue.js Configuration ​

For Vue.js projects using Vue CLI, modify vue.config.js:

javascript
const { defineConfig } = require('@vue/cli-service');
const webpack = require('webpack');

module.exports = defineConfig({
  transpileDependencies: true,
  configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        Buffer: ['buffer', 'Buffer'],
      })
    ],
    resolve: {
      extensions: ['.ts', '.js'],
      fallback: {
        "stream": require.resolve("stream-browserify"),
        "buffer": require.resolve("buffer")
      }
    },
  }
});

Global Polyfill (Quick Fix) ​

For a quick solution, add this to your main entry file (e.g., index.js, App.js):

javascript
import { Buffer } from 'buffer';
window.Buffer = Buffer;

Or directly in your HTML:

html
<script type="module">
  import { Buffer } from 'buffer';
  window.Buffer = Buffer;
</script>

Using node-polyfill-webpack-plugin ​

Alternatively, use a comprehensive polyfill plugin:

bash
npm install node-polyfill-webpack-plugin --save-dev

Then configure Webpack:

javascript
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');

module.exports = {
  // ... other config
  plugins: [
    new NodePolyfillPlugin()
  ]
};

Node.js Environment ​

If you're working in a Node.js environment (not browser), use the native Buffer module:

javascript
import { Buffer } from 'node:buffer';
// or
const { Buffer } = require('node:buffer');

Troubleshooting ​

If you're still encountering issues:

  1. Check your CSS files - Webpack 5's CSS minimizer is stricter about syntax
  2. Verify all dependencies - Some packages may have browser-specific versions
  3. Ensure proper imports - Use ES6 imports or CommonJS requires consistently

TIP

When debugging, check the call stack in the error message to identify which dependency is causing the Buffer reference.

Best Practices ​

  1. Prefer explicit imports over global polyfills when possible
  2. Use tree-shaking friendly approaches to minimize bundle size
  3. Only polyfill what you need rather than importing entire polyfill libraries
  4. Keep dependencies updated to benefit from browser-compatible versions

By properly configuring your build tools to handle Node.js core modules, you can resolve the "Buffer is not defined" error while maintaining optimal application performance.