score:1

using global worked for me.

const window = global.window
if (window && window.localstorage) {
  const storageloglevel = window.localstorage.getitem(log_level_key)
  switch (storageloglevel) {
    case loglevel.debug:
      loglevel = 0
      break
    case loglevel.info:
      loglevel = 1
      break
    case loglevel.warning:
      loglevel = 2
      break
    case loglevel.critical:
      loglevel = 3
      break
    default:
      loglevel = 1
  }
}

score:12

it may be late to answer but it may help someone else. i also had the referenceerror: window is not defined. issue with webpack 4, and found that adding globalobject: 'this' line to the output section in webpack config file fixed my problem:

  output: {
    globalobject: "this",
    filename: "[name].js",
    path: path.join(__dirname, "build/package"),
    publicpath: "/resources/",
  }

you can see the issue was discussed here and the webpack documentation about the globalobject setting here.


Related Query

More Query from same tag