最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js: Syntax Error on exporting - Stack Overflow

programmeradmin0浏览0评论

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._pile (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._pile (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!

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._pile (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._pile (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!

Share Improve this question asked Dec 21, 2012 at 17:20 saratsarat 11.2k7 gold badges48 silver badges76 bronze badges 1
  • 1 export is in fact a reserved word in javascript – Aaron Kurtzhals Commented Dec 21, 2012 at 17:24
Add a ment  | 

1 Answer 1

Reset to default 13

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

发布评论

评论列表(0)

  1. 暂无评论