Inputmask: "ReferenceError: window is not defined" when running with Node js

Created on 25 Aug 2017  路  5Comments  路  Source: RobinHerbots/Inputmask

Enhancement / Feature help wanted

Most helpful comment

Also seeing this issue, using the window object before checking if it exists breaks our server side render of a react app.

All 5 comments

Also seeing this issue, using the window object before checking if it exists breaks our server side render of a react app.

I've fixed the problem doing something like this (React code):

state = {
  inputMask: null,
}

componentWillUnmount() {
  this.state.inputMask.remove(this.inputRef)
}

setInputRef = ref => {
  import(`inputmask`)
    .then(module => {
      this.inputRef = ref
      this.setState({inputMask: module}, () => {
        this.state.inputMask(YOUR_MASK).mask(this.inputRef)
      })
    })
}

@kolyabokov ,
@TigerC10 ,
@augustolima ,

What would be the best approach for this? I created window.js to deal with this in the future. So maybe I can fix this now.

Would checking if window isn't defined requiring window through jsdom?

if (typeof define === "function" && define.amd)
    define(function () {
        return window || new (eval("require('jsdom')")('')).window;
    });
else if (typeof exports === "object")
    module.exports = window || new (eval("require('jsdom')")('')).window;

Bump. Still an issue.

My workaround:

const Inputmask=(typeof(window)!="undefined")?require("inputmask"):{};
Was this page helpful?
0 / 5 - 0 ratings

Related issues

RipDevil picture RipDevil  路  5Comments

ErikPham picture ErikPham  路  5Comments

pikaclint picture pikaclint  路  3Comments

movedoa picture movedoa  路  4Comments

webmastervinay picture webmastervinay  路  4Comments