I have a script foo.js
that contains some functions I want to play with in the REPL.
Is there a way to have node execute my script and then jump into a
Here's a bash function version of George's answer:
noderepl() {
FILE_CONTENTS="$(< $1 )"
node -i -e "$FILE_CONTENTS"
}
If you put this in your ~/.bash_profile
you can use it like an alias, i.e.:
noderepl foo.js
i always use this command
node -i -e "$(< yourScript.js)"
works exactly as in Python without any packages.
Another way is to define those functions as global.
global.helloWorld = function() { console.log("Hello World"); }
Then preload the file in the REPL as:
node -r ./file.js
Then the function helloWorld
can be accessed directly in the REPL.
Currently you can't do that directly, but you can mylib = require('./foo.js')
in the REPL. Remember methods are exported, not declared as globals.
Why not load the file into an interactive node repl?
node -h
-e, --eval script evaluate script
-i, --interactive always enter the REPL even if stdin
node -e 'var client = require("./build/main/index.js"); console.log("Use `client` in repl")' -i
Then you can add to package.json scripts
"repl": "node -e 'var client = require(\"./build/main/index.js\"); console.log(\"Use `client` in repl\")' -i",
tested using node v8.1.2