Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NextJS Webworker `window is not defined`

Getting a window not defined error when using NextJS and next-web-worker package.

require('dotenv').config();
const withPlugins = require('next-compose-plugins');
const withCSS = require('@zeit/next-css');
const withWorkers = require('@zeit/next-workers');
const Dotenv = require('dotenv-webpack');

const webpackConfig = (config) => {
  config.plugins = config.plugins || [];
  config.target = 'universal';
  config.output = {
    ...config.output,
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist'),
    globalObject: '(this || global)',
    publicPath: 'http://localhost:3000'
  };

  config.plugins = [
    ...config.plugins,

    // Read the .env file
    new Dotenv({
      path: path.join(__dirname, '.env'),
      systemvars: true
    })
  ];

  return config;
};

module.exports = withPlugins([[withCSS], [withWorkers]], webpackConfig);

The recommended work around appears to be setting the globalObject to this or global But that isn't working. Are there any other work arounds for this?

like image 675
GN. Avatar asked Jul 02 '26 14:07

GN.


1 Answers

I was just experiencing this same error and it was solved by following the advice here:

yarn add @zeit/next-workers@canary
like image 83
imjared Avatar answered Jul 05 '26 09:07

imjared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!