Reputation: 1488
Why can't I run node -e ".load ./script.js"
?
$ node -e '.load ./script.js'
[eval]:1
.load
^
SyntaxError: Unexpected token .
at Object.exports.runInThisContext (vm.js:53:16)
at Object.<anonymous> ([eval]-wrapper:6:22)
at Module._compile (module.js:410:26)
at node.js:578:27
at nextTickCallbackWith0Args (node.js:419:9)
at process._tickCallback (node.js:348:13)
I tried escaping the .
with a \
but that doesn't work either.
Upvotes: 5
Views: 9850
Reputation: 178
The -e
flag tells node to run eval on the provided string. This means it is running it as pure javascript. .load
is a REPL command, which means it is not javascript. -e
also does not run the REPL, so will not do what you want.
if all you want is to run the script from the command line, you can just do node path/to/script.js
To run a one liner that loads a script, you can do node -e "require('path/to/script.js')"
, however this will simply load the script, then quit. node will return, and nothing will appear to happen. There are likely several ways to run a REPL with pre-loaded data, but the way I like to do it is this:
node -e "const repl = require('repl'); repl.start().context.script = require('path/to/script.js')"
I actually have this in a batch script (since I'm running windows) which I use to test libraries. I just have to pass in any scripts I want to include and it will start up the REPL for me with them loaded.
Upvotes: 13