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

javascript - Node (SyntaxError: unexpected identifier ) js files in terminal - Stack Overflow

programmeradmin0浏览0评论

When I try running a javascript file through the terminal with node I get "SyntaxError: unexpected identifier"


Here is my code saved as example.js

console.log('hello world');

When I try running a javascript file through the terminal with node I get "SyntaxError: unexpected identifier"


Here is my code saved as example.js

console.log('hello world');


Here is what is happening in my terminal.

> Thoms-MacBook-Pro:desktop thomvaladez$ node
> console.log('hi');
hi
undefined
> node example.js
SyntaxError: Unexpected identifier
    at Object.exports.createScript (vm.js:44:10)
    at REPLServer.defaultEval (repl.js:117:23)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:279:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:214:10)
    at REPLServer.Interface._line (readline.js:553:8)
    at REPLServer.Interface._ttyWrite (readline.js:830:14)
    at ReadStream.onkeypress (readline.js:109:10)

Node responds to mands and code, but I cannot open files. Does anyone know what the issue may be?

Share Improve this question asked Jul 13, 2015 at 0:35 Thomas ValadezThomas Valadez 1,7572 gold badges24 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Most people run that mand outside of the node session itself.

> Thoms-MacBook-Pro:desktop thomvaladez$ node example.js

If you're already in the node session as you've done then--as t3dodson has suggested--you do a require at that point. Only you need to prepend that with a "./" so that it will find your file.

> Thoms-MacBook-Pro:desktop thomvaladez$ node
> require ('./example.js')
> Hello World!

I'm guessing that "node" then in your version is the "Unexpected identifier".

发布评论

评论列表(0)

  1. 暂无评论