How can I edit on my server files without restarting nodejs when i want to see the changes?

后端 未结 9 1104
天命终不由人
天命终不由人 2020-12-02 09:50

I\'m trying to setup my own nodejs server, but I\'m having a problem. I can\'t figure out how to see changes to my application without restarting it. Is there a way to edit

相关标签:
9条回答
  • 2020-12-02 10:15

    A simple direct solution with reference to all answers available here:

    Node documentation says that fs.watch is more efficient than fs.watchFile & it can watch an entire folder.

    (I just started using this, so not really sure whether there are any drawbacks)

    fs.watch("lib", (event_type, file_name) => {
        console.log("Deleting Require cache for " + file_name);
        delete require.cache[ require.resolve("./lib/" + file_name)];
    });
    
    0 讨论(0)
  • 2020-12-02 10:18

    There should be some emphasis on what's happening, instead of just shotgunning modules at the OP. Also, we don't know that the files he is editing are all JS modules or that they are all using the "require" call. Take the following scenarios with a grain of salt, they are only meant to describe what is happening so you know how to work with it.

    1. Your code has already been loaded and the server is running with it

      • SOLUTION You need to have a way to tell the server what code has changed so that it can reload it. You could have an endpoint set up to receive a signal, a command on the command line or a request through tcp/http that will tell it what file changed and the endpoint will reload it.

        //using Express
        var fs = require('fs');
        app.get('reload/:file', function (req, res) {
            fs.readfile(req.params.file, function (err, buffer) {
                //do stuff...
            });
        });
        
    2. Your code may have "require" calls in it which loads and caches modules

      • SOLUTION since these modules are cached by require, following the previous solution, you would need a line in your endpoint to delete that reference

        var moduleName = req.params.file;
        delete require.cache[moduleName];
        require('./' + moduleName);
        

    There's a lot of caveats to get into behind all of this, but hopefully you have a better idea of what's happening and why.

    0 讨论(0)
  • 2020-12-02 10:19

    I think node-inspector is your best bet.

    Similar to how you can Live Edit Client side JS code in Chrome Dev tools, this utilizes the Chrome (Blink) Dev Tools Interface to provide live code editing.

    https://github.com/node-inspector/node-inspector/wiki/LiveEdit

    0 讨论(0)
  • 2020-12-02 10:25

    Use this: https://github.com/remy/nodemon

    Just run your app like this: nodemon yourApp.js

    0 讨论(0)
  • 2020-12-02 10:25

    What's “Live Coding”?

    In essence, it's a way to alter the program while it runs, without restarting it. The goal, however, is to end up with a program that works properly when we (re)start it. To be useful, it helps to have an editor that can be customized to send code to the server.

    Take a look: http://lisperator.net/blog/livenode-live-code-your-nodejs-application/

    0 讨论(0)
  • 2020-12-02 10:26

    Nodules is a module loader for Node that handles auto-reloading of modules without restarting the server (since that is what you were asking about):

    http://github.com/kriszyp/nodules

    Nodules does intelligent dependency tracking so the appropriate module factories are re-executed to preserve correct references when modules are reloaded without requiring a full restart.

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