samre
samre

Reputation: 1

ReferenceError: document is not defined at Object.<anonymous>

I am following a youtube tutorial the guy in the tutorial is able to execute the code in the browser but i am unable to do the same. The following is the error that pops up in the terminal.

C:\vs projects\booklistapp\bookapp.js:47
document.addEventListener('DOMContentLoaded
', UI.displayBooks);
^



ReferenceError: document is not defined
    at Object.<anonymous> (C:\vs projects\b
ooklistapp\bookapp.js:47:1)
    at Module._compile (internal/modules/cj
s/loader.js:778:30)
    at Object.Module._extensions..js (inter
nal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/lo
ader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/
loader.js:593:12)
    at Function.Module._load (internal/modu
les/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/mo
dules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:
283:19)
    at bootstrapNodeJSCore (internal/bootst
rap/node.js:622:3)```

Upvotes: 0

Views: 2458

Answers (1)

Yuri Khomyakov
Yuri Khomyakov

Reputation: 380

You are running in Node.js, which does not have a window or document. If you want to run it in a browser, add the script tag to an html file and run the html file in the browser.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Hello from the browser</title>
</head>
<body>
  <script src="C:\vs projects\booklistapp\bookapp.js"></script>
</body>
</html>

Upvotes: 2

Related Questions