How to suspend node REPL and resume at a later stage with all the environment preserved?

后端 未结 2 1551
难免孤独
难免孤独 2021-02-20 09:37

I wish to suspend a REPL session so that I could shut down the system and then at a later time continue to work on the REPL session as if I\'d never closed it, i.e. without havi

相关标签:
2条回答
  • 2021-02-20 09:42

    So if you want to be able to "suspend" a REPL session and then pick up where you left off after a shut down doesn't seem to be directly available in Node.js's REPL. The closest thing to this is the Persistent History feature of the REPL which was added (i think) in Node 4.2.1. This will allow you to view the history of the commands in your REPL in plain text but thats the closest thing available out of the box with Node.

    Persistent History

    By default, the REPL will persist history between node REPL sessions by saving to a .node_repl_history file in the user's home directory. This can be disabled by setting the environment variable NODE_REPL_HISTORY="".

    Previously in Node.js/io.js v2.x, REPL history was controlled by using a NODE_REPL_HISTORY_FILE environment variable, and the history was saved in JSON format. This variable has now been deprecated, and your REPL history will automatically be converted to using plain text. The new file will be saved to either your home directory, or a directory defined by the NODE_REPL_HISTORY variable, as documented below.

    Full docs for the REPL module are available here.

    However, there is a REPL "wrapper" node module that will do what you're asking. What you can do is, save your REPL history out to a file and then load the history file on the next session and gain access to what you saved to the file in your next REPL session.The module is Nesh. It has a lot of additional features including configuring your shell and evaluating different version of JS such as ES6/ES7 (Using Babel) & Coffeescript.

    Install nesh:

    npm install -g nesh

    Launch nesh in the terminal by simply typing nesh. Work as you normally would within any other REPL session and when you want to save you can type the following in nesh to save your REPL history to the given file:

    .save <filepath>

    In your next REPL session, even after a shutdown, you can relaunch your nesh session and reload your history by typing:

    .load <filepath>

    This will re-evaluate the entire history file and will makes any variables or functions available in the current REPL/nesh session.

    Hope this is helpful and I think it meets your needs.

    0 讨论(0)
  • 2021-02-20 09:47

    What I think you are looking for is how to suspend and resume a process. See this answer on how to suspend and resume a process

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