charliebrownie
charliebrownie

Reputation: 6127

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

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

Upvotes: 6

Views: 6832

Answers (2)

mpneuried
mpneuried

Reputation: 181

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'
>

Upvotes: 2

charliebrownie
charliebrownie

Reputation: 6127

Executing the following command inside Node.js Shell worked:

.load foo.js

Upvotes: 12

Related Questions