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:
我是Node.js的新手,我想知道是否有一种从Node shell中执行JavaScript文件的方法。让我用这种方式解释一下:
Instead of doing this:
而不是这样做:
$ node file.js
$ node file.js
which will execute the script but will close the Node shell afterwards...
这将执行脚本,但之后将关闭Node shell ...
Is there a way of executing a script being already inside the Node shell? Something like:
有没有办法在Node shell中执行一个脚本?就像是:
$ 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
2 个解决方案
#1
10
Executing the following command inside Node.js
Shell worked:
在Node.js Shell中执行以下命令:
.load foo.js
#2
2
just do a require!
只是做一个要求!
example: file.js:
console.log( "SCRIPT RUNNING" );
module.exports = "optional return";
require it from the shell
要求它来自shell
$ node
> myresult = require( "./file.js" )
SCRIPT RUNNING
'optional return'
>
#1
10
Executing the following command inside Node.js
Shell worked:
在Node.js Shell中执行以下命令:
.load foo.js
#2
2
just do a require!
只是做一个要求!
example: file.js:
console.log( "SCRIPT RUNNING" );
module.exports = "optional return";
require it from the shell
要求它来自shell
$ node
> myresult = require( "./file.js" )
SCRIPT RUNNING
'optional return'
>