-1

I am trying to work on a project right now using node js and html. Unfortunately, i've been struck to a problem for 2 hours and i cannot figure out how to solve it.

Everytime i tried to launch the project, i got this error :

ReferenceError: document is not defined

Here is my code :

    const getElement = id => document.getElementById(id);

const setVisible = (el, state) => {
  let element = el;
  if (typeof el === 'string') {
    element = getElement(el);
  }

  element.style.display = state ? 'block' : 'none';
};

const setRowVisible = (row, state) => {
  row.style.display = state ? 'block' : 'none';
};

module.exports = {
  getElement,
  setVisible,
  setRowVisible,
};

Thank you for your help !

Vincent Tng
  • 145
  • 4
  • 12
  • Why would it be defined? You aren't running JS that is embedded in a document! What HTML would you expect this code to modify? – Quentin Jul 19 '19 at 14:01

1 Answers1

0

document object is not defined in NodeJS backend, it's a DOM property, are you trying to run this code server-side?

If you use server-side rendering on a project then you should wrap all this kind of functions into an if statement.