sarat
sarat

Reputation: 11130

Node.js: Syntax Error on exporting

This is really fundamental... I am just getting started with Node.js.

I have a simple module

server.js

var http = require("http");

function start() {
    function onRequest() {
        console.log("Request received");
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("Hello World");
        response.end();
    }

    http.createServer(onRequest).listen(8888);
    console.log("Server has started...");
}

export.start = start;

and it's being called from index.js as follows

var server = require("./server");
server.start();

but running node index.js gives me this!

$ node index.js

d:\SourceRepo\node-sample\server.js:15
export.start = start;
^^^^^^
SyntaxError: Unexpected reserved word
    at Module._compile (module.js:437:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (d:\SourceRepo\node-sample\index.js:1:76)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)

How to sort this out? I am a newbie to JavaScript!

Upvotes: 2

Views: 2537

Answers (1)

Michelle Tilley
Michelle Tilley

Reputation: 159115

You need to use exports, with an "s".

Upvotes: 13

Related Questions