static-site-generator-webpack-plugin: Error: 'window is not defined' with Webpack 4
I’m posting this issue for reference more than anything else, in case others are searching for a fix. The temporary fix is at the end, perhaps it’s worth adding that to the README?
When using this plugin with a fresh install of Webpack 4 (4.6.0 in my test) I get the following error:
ERROR in ReferenceError: window is not defined
Having dug into it further, it appears it is a side effect of this issue where the umd target generates invalid code for running inside a Node.js context.
The temporary fix is detailed here, where you can add the following to your Webpack config to change the global keyword from window to this:
output: {
// ...
globalObject: "this"
}
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 59
- Comments: 20
i added
globalObject: "this"still not fix ERROR in window is not definedup
this worked for me
as documented https://github.com/markdalgleish/static-site-generator-webpack-plugin#globals
Bump
In addition to the fix I outlined at the top of the issue, if you’re still getting
”window” is not definederrors, I’ve occasionally found it is because my application code is making a call to a DOM API (e.g.window.location) which doesn’t exist in Node-land.Often wrapping these lines in a
if (typeof window !== ‘undefined’) {}has helped, although that may not be the issue you’re having. Bear in mind this conditional check might have unexpected side effects depending on your exact implementation, so you may need to do some testing around that behaviour.Starting with a hello-world React setup (no router or anything) and this (partial) Webpack config:
I got this error on running
webpackorwebpack-dev-server:Following @chrishutchinson’s links, and nested ones, I resolved it by adding this to my Webpack config:
However, now I get new errors. On running
webpack, I get:And on running
webpack-dev-server, I get:Having the same issue here with bootstrap-loader.
ERROR in ./node_modules/bootstrap-loader/no-op.js (./node_modules/mini-css-extract-plugin/dist/loader.js!./node_modules/style-loader!./node_modules/css-loader/dist/cjs.js?sourceMap!./node_modules/resolve-url-loader?sourceMap!./node_modules/postcss-loader/src!./node_modules/sass-loader/lib/loader.js?sourceMap!./node_modules/bootstrap-loader/lib/bootstrap.styles.loader.js?{“bootstrapVersion”:4,“bootstrapCustomizations”:“/Users/wendel.costa/Desktop/qantas/src/shared/styles/01_settings/_bootstrap.scss”,“extractStyles”:true,“styleLoaders”:[“style-loader”,“css-loader?sourceMap”,“resolve-url-loader?sourceMap”,“postcss-loader”,“sass-loader?sourceMap”],“styles”:[“mixins”,“grid”],“scripts”:false,“configFilePath”:“/Users/wendel.costa/Desktop/qantas/.bootstraprc”,“bootstrapPath”:“/Users/wendel.costa/Desktop/qantas/node_modules/bootstrap”,“bootstrapRelPath”:“…/bootstrap”}!./node_modules/bootstrap-loader/no-op.js) Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): ReferenceError: window is not defined
Not sure if relevant, but I was working on a library meant to run in node env, and was getting this error even after setting
output.globalObject = "this. Settingtarget: nodegot it working for me@kavehsajjadi same here. “self” is not defined now
@chrishutchinson is there some magic involved with getting that fix to work? I’m getting Invalid configuration object errors