Webpack cannot resolve Node.js modules

Webpack cannot resolve core Node.js modules

I’m aware that this isn’t the only Node.js module query related to Webpack on here, but when applying the solutions that I’ve read elsewhere, yet another core module seems to be causing another ReferenceError.

I have an index.js file that requires the Crypto module to log a hash to the console:

index.js

const crypto = require('crypto')

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
                   .update('I love cupcakes')
                   .digest('hex');
console.log(hash);

When setting up my webpack.congif.js file, I got back this error:

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:

  • add a fallback ‘resolve.fallback: { "crypto": require.resolve("crypto-browserify") }’
  • install ‘crypto-browserify’

After following the above guidance, I got the same error for ‘buffer’ and ‘stream’ modules, and to get the config file to compile, I’ve added fallback statements and installed the relevant modules to cover these missing dependencies:

webpack.config.js

const path = require('path');

module.exports = {
    mode: 'development',
    node: {
        global: true,
    },
    resolve: {
        fallback: { 
            "crypto": require.resolve("crypto-browserify"),
            "buffer": require.resolve("buffer/"),
            "stream": require.resolve("stream-browserify")
        }
    },
    entry: './src/index.js',
    output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
    },
};

Webpack.config now compiles, but when I run index.html in the browser I get the following error in the browser console:

Uncaught ReferenceError: process is not defined

As stated in the ‘Breaking Change’ error, Webpack 5 no longer polyfills for node core modules, but does this mean that a polyfill is required for every single node module before you can run a node script?

23 thoughts on “Webpack cannot resolve Node.js modules”

  1. Wonderful goods from you, man. I have understand your stuff previous to and you are just too fantastic.

    I really like what you have acquired here, certainly like
    what you are saying and the way in which you say it.
    You make it enjoyable and you still care for to keep it sensible.
    I can’t wait to read much more from you. This is actually a wonderful website.

    Reply
  2. Hi there terrific website! Does running a blog such as this take a large amount of
    work? I’ve no understanding of coding however I
    had been hoping to start my own blog soon. Anyways, should you have
    any suggestions or tips for new blog owners
    please share. I know this is off topic however I just needed to ask.
    Thanks!

    Feel free to surf to my blog post: agen 918kiss malaysia

    Reply
  3. Saya telah telah berselancar online lebih dari tiga
    jam hari ini, namun saya tidak pernah menemukan artikel menarik seperti milik Anda.
    It is cukup berharga bagi saya. Menurut pendapat saya , jika semua pemilik situs
    web dan blogger membuat konten yang baik seperti yang Anda
    lakukan, internet akan menjadi a jauh lebih berguna dari sebelumnya.

    my blog :: Login joker slot

    Reply
  4. Having read this I thought it was extremely enlightening.
    I appreciate you spending some time and effort to put this article together.
    I once again find myself spending a significant
    amount of time both reading and commenting. But
    so what, it was still worthwhile!

    My web page: wigoslot live22

    Reply
  5. I’m really impressed with your writing skills and also with the format
    on your weblog. Is that this a paid subject or did
    you modify it yourself? Either way stay up the excellent
    high quality writing, it is rare to see a nice blog like this one nowadays..

    Also visit my site … xe88 android (sg918kiss.com)

    Reply

Leave a Comment