rollup-plugin-typescript2: `ReferenceError: window is not defined` in 0.32.0

What happens and why it is wrong

Environment

  • Yarn 3
Versions

The command to use envinfo throws an error, so I’m resorting to copy-paste. All versions are latest.

    "rollup": "^2.75.5",
    "rollup-plugin-typescript2": "^0.32.0",
    "typescript": "^4.7.2"

The error resides here:

if (typeof commonjsRequire === "function") {
  try {
    lodash = {
      clone: require("lodash/clone"),
      constant: require("lodash/constant"),
      each: require("lodash/each"),
      filter: require("lodash/filter"),
      has:  require("lodash/has"),
      isArray: require("lodash/isArray"),
      isEmpty: require("lodash/isEmpty"),
      isFunction: require("lodash/isFunction"),
      isUndefined: require("lodash/isUndefined"),
      keys: require("lodash/keys"),
      map: require("lodash/map"),
      reduce: require("lodash/reduce"),
      size: require("lodash/size"),
      transform: require("lodash/transform"),
      union: require("lodash/union"),
      values: require("lodash/values")
    };
  } catch (e) {
    // continue regardless of error
  }
}

if (!lodash) {
  lodash = window._;
}

The line saying to use window._ throws an error because window does not exist in the Node environment.

This is newly introduced in 0.32.0. Reverting to 0.31.0 fixes this.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16

Most upvoted comments

A bugfix has been released in 0.32.1 🎉

I’ve also confirmed that 0.32.1 works inside of rpt2, in one of my own libraries, and the library that I was able to repro the bug in above, gismeteo.

Thanks to everyone for submitting this issue, providing repros, etc not too long after 0.32.0’s release so it could get addressed quickly!

Annnd yep, this exact piece of code that requires lodash changes in that exact commit: https://github.com/ezolenko/rollup-plugin-typescript2/commit/08d2f5bcad7cb9a9a0974f3707a27d29dd89da7c#diff-80441564774ccf83c60a1616524830d5c15a9bc194b62e2c3d9f43de805c62d0L22435

Updating to @rollup/plugin-commonjs to v22.0.0 once again changes that piece of code and seems to fix the issue when I test with either Yarn or NPM in gismeteo

Can install via my issue-339 branch to workaround this issue for now. Will make a PR shortly, but have to wait on @ezolenko for a release.

In the meantime, @CharlesStover can you confirm that installing my issue-339 branch fixes this for you as well?

yarn add --dev git+https://github.com/agilgur5/rollup-plugin-typescript2#issue-339

Ok was able to repro the issue with both NPM and Yarn in gismeteo. Oddly enough though, I still can’t repro it here or in my other libraries.

What I also found was that @rollup/plugin-commonjs was actually updated twice in this release, in the commit I mentioned above, and in 08d2f5bcad7cb9a9a0974f3707a27d29dd89da7c.

That commit moves up 3 majors and in particular this issue comment stood out to me as very relevant: https://github.com/rollup/plugins/pull/1005#issuecomment-954130548 . That should be fixed by https://github.com/rollup/plugins/pull/1038 in v22.0.0, so I’ll try updating that and seeing if it fixes this issue in gismeteo

@shevernitskiy I see your library is open-source: https://github.com/shevernitskiy/gismeteo. It’s not a minimal repro, but a repro still helps a lot! I’ll look into this tomorrow.

I also notice that similar to @CharlesStover , both of you were updating several dependencies at once. And both using Yarn.

I’ve upgraded to rpt2 0.32.0 in some of my other libraries and haven’t encountered this as of yet, but they all use NPM fwiw.