Home > other >  'path\node_modules\verror\lib' BREAKING CHANGE: webpack < 5 used to include polyfill
'path\node_modules\verror\lib' BREAKING CHANGE: webpack < 5 used to include polyfill

Time:06-28

When I try tu use a firebase cloud function

import * as functions from 'firebase-functions';

exports.userDeleted = functions.auth.user().onDelete(event => { 
  console.log("user deleted  ")
 });

I'm getting this error :

[webpack-dev-server] ERROR in ./node_modules/verror/lib/verror.js 6:15-30
Module not found: Error: Can't resolve 'util' in 'C:\Users\MyPC\Documents\AIC-partage\workspace\myproject\node_modules\verror\lib'

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: { "util": require.resolve("util/") }'
    - install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "util": false }

Someone knows how to fix this error ?

CodePudding user response:

From the error, it looks like you are using Webpack 5 to bundle you code. The quick short answer is that till Webpack 4, Webpack would simply include polyfills for native node.js modules like fs, util, path, etc. This is not longer true with Webpack 5. You need to explicitly tell Webpack to either polyfill it and not to provide it. Read more about this here: Webpack Resolve Fallback.

module.exports = {
  //... Webpack configuration
  resolve: {
    fallback: {
      assert: require.resolve('assert'),
      buffer: require.resolve('buffer'),
      console: require.resolve('console-browserify'),
      constants: require.resolve('constants-browserify'),
      crypto: require.resolve('crypto-browserify'),
      domain: require.resolve('domain-browser'),
      events: require.resolve('events'),
      http: require.resolve('stream-http'),
      https: require.resolve('https-browserify'),
      os: require.resolve('os-browserify/browser'),
      path: require.resolve('path-browserify'),
      punycode: require.resolve('punycode'),
      process: require.resolve('process/browser'),
      querystring: require.resolve('querystring-es3'),
      stream: require.resolve('stream-browserify'),
      string_decoder: require.resolve('string_decoder'),
      sys: require.resolve('util'),
      timers: require.resolve('timers-browserify'),
      tty: require.resolve('tty-browserify'),
      url: require.resolve('url'),
      util: require.resolve('util'),
      vm: require.resolve('vm-browserify'),
      zlib: require.resolve('browserify-zlib'),
    },
  },
};

Long answer

If we literally go by the error, then it means above answer is correct. But that may not solve your problem. It looks like Webpack is trying to bundle code for browser runtime. Thus the above suggested answer may work but ,in reality, you are bundling code to run on Firebase Cloud Functions. You must configure Webpack to bundle your code for Node.js environment and also specify it to ignore native node.js module from your bundle. You need to use target: 'node' and optionally webpack-node-externals package to ignore any third-party module. Your configuration should be:

const nodeExternals = require('webpack-node-externals');


module.exports = {
  // ... Webpack configuration

  // in order to ignore built-in modules like path, fs, etc.
  target: 'node',

  // [OPTIONAL] - Depending on your use case
  // In order to ignore all modules in node_modules folder.
  externals: [nodeExternals()],
};

CodePudding user response:

it was only the syntax of the function

exports.userDidDeleted = functions.auth.user().onDelete((event) => {
    const userSearchLocationModelPath = "/users/"   event.uid;
    admin.database().ref(userSearchLocationModelPath).remove();
  });
  • Related