0

When I tried to import an valid node_module it throws an error?

<script >
    var Twit = require('twit');
</script>

Error:

Uncaught ReferenceError: require is not defined

My doubt is the require() function is running fine on js file but not in HTML file.

It also not working when I import the js file in the script tag.

<script src="s.js"></script>

NOTE My nodejs version is v14.15.4

I am new to nodejs and I am sorry if my approach is wrong

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

1 Answers1

0

Node is used to run a 'backend' server so the user asks the node.js server for the web page and the node server then serves the page. Express is a popular module for this.

You need to use an external file then import modules and write your javascript in that external file. require at the top of this external file.

You may also need to run npm init and then npm install twit in command line in the directory of your project.

charlie scott
  • 136
  • 1
  • 9