Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load my script into the node.js REPL?

People also ask

How do I import a file into node REPL?

You can type ". load " (note the space) into the REPL and drag/drop the file into the Terminal from the Finder to add the correct path to your command.

How do I run a shell script in node JS?

Node. js can run shell commands by using the standard child_process module. If we use the exec() function, our command will run and its output will be available to us in a callback. If we use the spawn() module, its output will be available via event listeners.


There is still nothing built-in to provide the exact functionality you describe. However, an alternative to using require it to use the .load command within the REPL, like such:

.load foo.js

It loads the file in line by line just as if you had typed it in the REPL. Unlike require this pollutes the REPL history with the commands you loaded. However, it has the advantage of being repeatable because it is not cached like require.

Which is better for you will depend on your use case.


Edit: It has limited applicability because it does not work in strict mode, but three years later I have learned that if your script does not have 'use strict', you can use eval to load your script without polluting the REPL history:

var fs = require('fs');
eval(fs.readFileSync('foo.js').toString())

i always use this command

node -i -e "$(< yourScript.js)"

works exactly as in Python without any packages.


I made Vorpal.js, which handles this problem by turning your node add into an interactive CLI. It supports a REPL extension, which drops you into a REPL within the context of your running app.

var vorpal = require('vorpal')();
var repl = require('vorpal-repl');

vorpal
  .delimiter('myapp>')
  .use(repl)
  .show()
  .parse(process.argv); 

Then you can run the app and it will drop into a REPL.

$ node myapp.js repl
myapp> repl: 

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.


I created replpad since I got tired of reloading the script repeatedly.

Simply install it via: npm install -g replpad

Then use it by running: replpad

If you want it to watch all files in the current and all subdirectories and pipe them into the repl when they change do: replpad .

Check out the videos on the site to get a better idea of how it works and learn about some other nice features that it has like these:

  • access core module docs in the repl via the dox() function that is added to every core function, i.e. fs.readdir.dox()
  • access user module readmes in the repl via the dox() function that is added to every module installed via npm, i.e. marked.dox()
  • access function's highlighted source code, info on where function was defined (file, linenumber) and function comments and/or jsdocs where possible via the src property that is added to every function, i.e. express.logger.src
  • scriptie-talkie support (see .talk command)
  • adds commands and keyboard shortcuts
  • vim key bindings
  • key map support
  • parens matching via match token plugin
  • appends code entered in repl back to file via keyboard shortcut or .append command

See: https://github.com/thlorenz/replpad


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