Execute code each time the Node.js REPL starts
Join the DZone community and get the full member experience.Join For Free
I have not found a way to perform the code execution via a configuration file, but you can make it happen via the following trick: Write a script that executes the code and then programmatically starts a REPL. Example: The following are the contents of a file called startnode.js.
#!/usr/local/bin/node var repl = require("repl"); var context = repl.start("$ ").context; // Configure what’s available in the REPL context.util = require("util");Under Unix, the first line lets you run startnode.js as a command, if you make it executable.
rauschma> chmod u+x startnode.js rauschma> ./startnode.js $ util.log("hello") 17 Nov 21:39:29 - helloYou can also use the following command to run it (with or without the first line):
rauschma> node startnode.jsThe Node.js documentation has more information on the REPL API and on commands that are available while running it. For example:
$ .help .break Sometimes you get stuck, this gets you out .clear Break, and also clear the local context .exit Exit the repl .help Show repl options
Opinions expressed by DZone contributors are their own.