使用 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/