Webpack targeting webworker won't import browser version of a dependency

Viewed 330

I'm writing a CloudFlare worker and I have the following webpack.config.js:

'use strict';

module.exports = {
  devtool: 'source-map',
  entry: './src/main.ts',
  module: {
    rules: [
      {
        test: /\.html$/,
        loader: 'html-loader',
        options: {
          sources: false,
        },
      },
      {
        test: /\.(ico|jpg|png)$/,
        loader: 'url-loader',
      },
      {
        test: /\.txt$/,
        loader: 'raw-loader',
      },
      {
        test: /\.ts$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  stats: 'errors-warnings',
  target: 'webworker',
};

For some dependencies, like the npm cookie and lodash packages this works fine. However, when I try to import eta (https://eta.js.org/), webpack imports the cjs version instead of the browser version and I get the following errors:

ERROR in ./node_modules/eta/dist/eta.cjs 5:9-22
Module not found: Error: Can't resolve 'fs' in '/home/avery/dev/28pac/worker/node_modules/eta/dist'

ERROR in ./node_modules/eta/dist/eta.cjs 6:11-26
Module not found: Error: Can't resolve 'path' in '/home/avery/dev/28pac/worker/node_modules/eta/dist'

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

webpack compiled with 2 errors

Why is this happening and how do I fix it?

As far as I can tell, the eta package is written correctly and I should be getting the browser version.

0 Answers
Related