0

I have the below code in Nodejs and tried to execute the same through command line as well as in browser, but getting "unexpected identifier" error.

var http = require('http'); 
http.createServer(function(request, response) {
  response.writeHead(200, {'Content-Type': 'text/html'});
  response.end('<b>Running in server...</b>');
}).listen('3000', '127.0.0.1');

enter image description here

File name created is module3.js I am very new to nodejs. So can anyone please help me to resolve this. Thanks!!

Ba.Lal
  • 180
  • 2
  • 16

1 Answers1

0

If you like to run file, do it as usual for shell comman line (interpreter file):

D:\nodejs> node module3.js

If you like to work in nodejs command line, you need to load module by require and it will be loaded and evaluated:

D:\nodejs> node
> require('./module3')

If your module exports some function you may to call it:

D:\nodejs> node
> require('./module3')()

Check about exports here

oklas
  • 7,935
  • 2
  • 26
  • 42
  • 1
    He's error indicates that it's something else - otherwise he would have got `module3 is not defined` – nicholaswmin Feb 27 '18 at 10:59
  • I edited the answer. The propblem solved by adding `./` at the begin of module pathname. If path name is absent in require then it search library with that name. If module path name is present then module loaded. – oklas Feb 28 '18 at 05:03
  • I just restarted my PC and found my code is working fine. I dont understand the actual problem.. Anyway thanks all for your valuable suggestions. – Ba.Lal Feb 28 '18 at 06:18