SOLVED:
This happens because only Webpack versions < 5 used to include polyfills for Node.js core modules by default, whereas latest versions do not. If you're using a Webpack version > 5 you'll need to add this manually to your Webpack config.
In a gatsby.js project,in your gatsby-node.js file, add the following:
const webpack = require("webpack");
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
plugins: [
new webpack.ProvidePlugin({
Buffer: [require.resolve("buffer/"), "Buffer"],
}),
],
resolve: {
fallback: {
"crypto": false,
"stream": require.resolve("stream-browserify"),
"assert": false,
"util": false,
"http": false,
"https": false,
"os": false
},
},
})
}