Restart node upon changing a file

后端 未结 8 685
轮回少年
轮回少年 2020-11-28 17:54

For someone who is coming from PHP background the process of killing node and starting it again after every code change, seems very tedious. Is there any flag when starting

相关标签:
8条回答
  • 2020-11-28 18:18

    node-dev

    node-dev is great alternative to both nodemon and supervisor for developers who like to get growl (or libnotify) notifications on their desktop whenever the server restarts or when there is an error or change occur in file.

    Installation:

    npm install -g node-dev
    

    Use node-dev, instead of node:

    node-dev app.js
    

    Notification on Changing file so server start automatically

    console out put

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

    A good option is Node-supervisor and Node.js Restart on File Change is good article on how to use it, typically:

     npm install supervisor -g
    

    and after migrating to the root of your application use the following

     supervisor app.js
    
    0 讨论(0)
  • 2020-11-28 18:29

    forever module has a concept of multiple node.js servers, and can start, restart, stop and list currently running servers. It can also watch for changing files and restart node as needed.

    Install it if you don't have it already:

    npm install forever -g
    

    After installing it, call the forever command: use the -w flag to watch file for changes:

    forever -w ./my-script.js
    

    In addition, you can watch directory and ignore patterns:

    forever --watch --watchDirectory ./path/to/dir --watchIgnore *.log ./start/file
    
    0 讨论(0)
  • 2020-11-28 18:29

    You can also try nodemon

    To Install Nodemon

    npm install -g nodemon
    

    To use Nodemon

    Normally we start node program like:

    node server.js
    

    But here you have to do like:

    nodemon server.js
    
    0 讨论(0)
  • 2020-11-28 18:30

    Follow the steps:

    1. npm install --save-dev nodemon

    2. Add the following two lines to "script" section of package.json:

    "start": "node ./bin/www",
    "devstart": "nodemon ./bin/www"
    

    as shown below:

    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node ./bin/www",
        "devstart": "nodemon ./bin/www"
    }
    
    1. npm run devstart

    https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website

    0 讨论(0)
  • 2020-11-28 18:32

    You should look at something like nodemon.

    Nodemon will watch the files in the directory in which nodemon was started, and if they change, it will automatically restart your node application.

    Example:

    nodemon ./server.js localhost 8080
    

    or simply

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