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

Is there a way to run a JavaScript file inside the Node.js shell? - Stack Overflow

programmeradmin0浏览0评论

I am new to Node.js and I was wondering if there was a way of executing a JavaScript file from inside the Node shell. Let me explain it this way:

Instead of doing this:

$ node file.js

which will execute the script but will close the Node shell afterwards...

Is there a way of executing a script being already inside the Node shell? Something like:

$ node
> file.js                  # We're now inside the Node.js shell

... script execution...

>                          # already inside the shell, 
                           # then being able to access to script variables

I am new to Node.js and I was wondering if there was a way of executing a JavaScript file from inside the Node shell. Let me explain it this way:

Instead of doing this:

$ node file.js

which will execute the script but will close the Node shell afterwards...

Is there a way of executing a script being already inside the Node shell? Something like:

$ node
> file.js                  # We're now inside the Node.js shell

... script execution...

>                          # already inside the shell, 
                           # then being able to access to script variables
Share Improve this question edited Jan 22, 2015 at 15:40 charliebrownie asked Jan 15, 2015 at 0:02 charliebrowniecharliebrownie 6,11711 gold badges39 silver badges56 bronze badges 1
  • 1 Maybe this is helpful: stackoverflow./questions/8425102/… – jpw Commented Jan 15, 2015 at 0:10
Add a ment  | 

2 Answers 2

Reset to default 12

Executing the following mand inside Node.js Shell worked:

.load foo.js

just do a require!

example: file.js:

console.log( "SCRIPT RUNNING" );
module.exports = "optional return";

require it from the shell

$ node
> myresult = require( "./file.js" )
SCRIPT RUNNING
'optional return'
>
发布评论

评论列表(0)

  1. 暂无评论