Mawadda
Mawadda

Reputation: 7

Why this Error showing while running a file using Node.js?

I was trying to run a javascript file "script" using "node script" command, and it does showing an error. before using node there was no error. and I alsoConfirmed that the script link has a defer attribute so it will load after the HTML code.

PS C:\Users\user\Desktop\my port> node script
C:\Users\user\Desktop\my port\script.js:1
const tabs=document.querySelectorAll('[data-tab-target]');
         ^

ReferenceError: document is not defined
  at Object.<anonymous> (C:\Users\user\Desktop\my port\script.js:1:12)
  at Module._compile (internal/modules/cjs/loader.js:1251:30)
  at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
  at Module.load (internal/modules/cjs/loader.js:1100:32)
  at Function.Module._load (internal/modules/cjs/loader.js:962:14)
  at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
  at internal/main/run_main_module.js:17:47

    

here is javascript code:

const tabs= document.querySelectorAll('[data-tab-target]');
const pannels= document.querySelectorAll('[data-tab-content]');
tabs.forEach(tab => {
    tab.addEventListener('click',()=>{
       const target=document.querySelector(tab.dataset.tabTarget);
       pannels.forEach((pannel)=>{
           pannel.classList.remove('active');
       });
       tabs.forEach((tab)=>{
        tab.classList.remove('active');
    });


       target.classList.add('active');
       tab.classList.add('active');
    
});
});

Upvotes: 0

Views: 213

Answers (4)

Biswajit Biswas
Biswajit Biswas

Reputation: 1045

DOM (Document object model) is related to the browser, not NodeJS or javascript. DOM operation is provided by the browser. You could use JSDom to add Dom support to Node. Browserify also might be helpful.

Upvotes: 0

Ben Steward
Ben Steward

Reputation: 2408

What you’re trying to do is run browser JavaScript (which deals with windows and documents, among other things) in a server environment (which deals with file systems and processes, among other things). It is possible to write JavaScript that can run in both without modification, but there are some “global” variables that are unique to each environment. In your code here, document would be one of them. This code is intended specifically for the browser, where it can read and write the “document” (probably your index.html). In nodejs, there is no document, as the server is not intended to display anything itself, but only reads/writes to a console or to files.

Upvotes: 0

Kevin Hoopes
Kevin Hoopes

Reputation: 507

Although NodeJS is run using the JavaScript engine, it is not being run in the browser like it would be when one would normally use the document variable. That is something that only exists on a web page, because it is specifically part of the DOM.

Since this is being run in your terminal, it has no document to reference.

What I think you're looking for is a Node library that will start a server to send your HTML and JavaScript to the client. If this is the case, I recommend looking into ExpressJS or KoaJS.

Upvotes: 1

prabhu
prabhu

Reputation: 978

document is only available on a browser not when running on a server

Upvotes: 0

Related Questions