node.js - NextJS Webworker `window is not defined`

标签 node.js webpack web-worker next.js

使用 NextJS 和 next-web-worker 包时出现窗口未定义错误。

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);

建议的解决方法似乎是将 globalObject 设置为 this 或 global 但这不起作用。还有其他解决方法吗?

最佳答案

我刚刚遇到了同样的错误,通过以下 the advice here 解决了这个问题:

yarn add @zeit/next-workers@canary

关于node.js - NextJS Webworker `window is not defined`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56203586/

相关文章:

javascript - 箭头函数是否比 v8 中的普通独立函数声明更快(性能更高、更轻)?

css - Vuejs.vue 文件 : dynamic less import

google-chrome - Web Worker 调试在开发人员工具中不可用

HTML5 Messaging API 和使用 web workers 时的消息顺序

javascript - 从中间件nodejs传递参数

javascript - 发送并处理 POST 请求

node.js - Webpack 怎么直接引入了 webpack.config.babel.js 呢?

node.js - 后端和前端的 babel 预设不同,但环境相同

javascript - 如何进行 worker 与 worker 的沟通?

Node.js 中间件组织和参数验证