0

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)```

1
  • 2
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3 indicates that you try to run the script using nodejs. And the stack trace doe snot indicated that you use any module that tries to emulate a browser environment that would provide document. And document.addEventListener('DOMContentLoaded', UI.displayBooks); is clearly a code that belongs in a browser environment (or in the ui part of e.g. electron - which is actually a browser). Commented Oct 19, 2019 at 18:05

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it worked.I had forgotten to add the script tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.