0
// index.js
let intro = document.getElementById('intro');
let name = "Harry";
module.exports = name;
    
// new.js
let name = require("./index.js");
console.log(name);

I am trying to build an application with Node.js. When I want to export a string or specific data with module.exports to use it in the new.js file, an error is logged in the console saying that "document is not defined". All I need is to export a variable having a string to my Node.js (new.js). Also my index.js have multiple document.getElements. I also noticed that when I remove document.getElements then I successfully import that data from index.js without any error. How could I export data from index.js file with that document.getElement present in that?

Subhan
  • 21
  • 3
  • The global variable `document` exists in a web browser, but not in Node. This is a good explanation - https://stackoverflow.com/questions/32126003/node-js-document-is-not-defined – sifriday Sep 23 '22 at 06:53
  • But I just want to export a variable data doesnot have to do anything with document in node.js. Also what is the solution of that problem how could I get data from my index.js to node.js. – Subhan Sep 23 '22 at 07:01
  • The document you are trying to import is having an Error, so it won't work. The execution will end when the error occurs. Remove the `document` statement and try again. – Sankar Sep 23 '22 at 07:03
  • As I mentioned in my Question that when I remove document from index.js there is no error and I successfully import the data. But in my app my index.js has multiple document.getElements due to the fact that I have an html file and I have to manipulate certain elements using js. So my concern is to export some data present in a variable to my node.js or new.js file. I already know that document is messing with node but whats the solution. – Subhan Sep 23 '22 at 07:09
  • @Subhan — You need to redesign your JS structure. Don't mix "code you want to run on Node.js and in browsers" with "code that can only run in browsers". Move the shared code into its own module them import it into both the Node.js and browser specific modules. – Quentin Sep 23 '22 at 10:20

1 Answers1

0

Your issue is with document.getElementById() as in Node.js the browser DOM API is not defined.

So the first line of the index.js module is failing, and you can't reach the module.exports line.

UPDATE

To postpone the evaluation of the offending code, one solution can be to wrap the code in a function, that can be exported as well and run just when the file is inside a browser.

here the example:

// index.js
function myGetElementBy(d, selector) {
    let intro = d.getElementById(selector);
}

let name = "Harry";
module.exports = {
 name,
 myGetElementBy,
};

// new.js
let {name, myGetElementBy} = require("./index.js");
console.log(name);

if (window && 'document' in window) {
   myGetElementBy(document, 'intro')
}

Now the module is exporting an object that contains the function other than the variable name.

Mario Santini
  • 2,905
  • 2
  • 20
  • 27