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

前端 未结 11 1568
执念已碎
执念已碎 2020-11-30 17:59

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

相关标签:
11条回答
  • 2020-11-30 18:10

    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: 
    
    0 讨论(0)
  • 2020-11-30 18:11

    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

    0 讨论(0)
  • 2020-11-30 18:11

    Old answer

    type test.js|node -i
    

    Will open the node REPL and type in all lines from test.js into REPL, but for some reason node will quit after file ends

    Another problem is, that functions will not be hoisted.

    Better answer

    node -e require('repl').start({useGlobal:true}); -r ./test2.js
    

    Then all globals declared without var within test2.js will be available in the REPL

    not sure why var a in global scope will not be available

    0 讨论(0)
  • 2020-11-30 18:13

    replpad is cool, but for a quick and easy way to load a file into node, import its variables and start a repl, you can add the following code to the end of your .js file

    if (require.main === module){
        (function() {
            var _context = require('repl').start({prompt: '$> '}).context;
            var scope = require('lexical-scope')(require('fs').readFileSync(__filename));
            for (var name in scope.locals[''] )
                _context[scope.locals[''][name]] = eval(scope.locals[''][name]);
            for (name in scope.globals.exported)
                _context[scope.globals.exported[name]] = eval(scope.globals.exported[name]);
        })();
    }
    

    Now if your file is src.js, running node src.js will start node, load the file, start a REPL, and copy all the objects declared as var at the top level as well as any exported globals. The if (require.main === module) ensures that this code will not be executed if src.js is included through a require statement. I fact, you can add any code you want to be excuted when you are running src.js standalone for debugging purposes inside the if statement.

    0 讨论(0)
  • 2020-11-30 18:15

    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())
    
    0 讨论(0)
  • 2020-11-30 18:16

    Another suggestion that I do not see here: try this little bit of code

    #!/usr/bin/env node
    'use strict';
    
    const repl = require('repl');
    const cli = repl.start({ replMode: repl.REPL_MODE_STRICT });
    cli.context.foo = require('./foo'); // injects it into the repl
    

    Then you can simply run this script and it will include foo as a variable

    0 讨论(0)
提交回复
热议问题