107

Currently is seems that for any code change in a sails.js app you have to manually stop the sails server and run sails lift again before you can see the changes.

I was wondering if there is any way when running in development mode to automatically restart the sails server when it detects a code change?

8 Answers 8

74

You have to use a watcher like forever, nodemon, or something else...

Example

  1. Install forever by running:

    sudo npm install -g forever

  2. Run it:

    forever -w start app.js


To avoid infinite restart because Sails writes into .tmp folder, you can create a .foreverignore file into your project directory and put this content inside:

**/.tmp/**
**/views/**
**/assets/**

See the issue on GitHub: Forever restarting because of /.tmp.

3
  • 7
    You might also want to add views to .foreverignore as they do not require restarts to see changes immediately. e.g. **/views/** Feb 23, 2014 at 3:59
  • but unfortunately these will lead to bootstrap code executed again and loss of sessions. Jul 28, 2016 at 5:16
  • 1
    I used this but I receive the following warning: --minUptime not set. Defaulting to: 1000ms. How do I set this variable?
    – Golinmarq
    Aug 29, 2016 at 15:58
48

You can use sails-hook-autoreload

Just lift your app as normal, and when you add / change / remove a model or controller file, all controllers and models will be reloaded without having to lower / relift the app.

3
  • 1
    This seems to be the simplest/easiest solution. Install the npm and add the sample autoreload.js into the config folder and you are basically done. Mar 19, 2015 at 23:08
  • 1
    Absolutely amazing. Simplest and most native to Sails.
    – Alexus
    May 13, 2015 at 4:06
  • Im getting Details: Error: ER_ROW_IS_REFERENCED: Cannot delete or update a parent row: a foreign key constraint fail thrown by 5/lib/node_modules/sails/lib/hooks/orm/index.js:221 when I save (using sails 11.3)
    – Gab
    Dec 2, 2015 at 21:16
45

For example with nodemon to watch api and config directories

.nodemonignore contents

views/*
.tmp/*
.git/*

Run the command after creating .nodemonignore

$> nodemon -w api -w config

Example for supervisor to ignore 3 directories

$> supervisor -i .tmp,.git,views app.js
3
  • 4
    I like the nodemon solution over the forever solution given how nodemon pipes output back to stdout w/o additional configuration. Makes development workflow easier. Nov 20, 2014 at 22:10
  • For the command I just used your .nodemonignore example and run nodemon. It works so far. Dec 12, 2014 at 18:25
  • For anyone else confused about how running the above launches (the equivalent of) sails lift, nodemon knows to use app.js. Jan 25, 2016 at 23:54
9

If you're using Sails 0.11, you can install this hook to automatically reload when you change models or controllers (views do not require reloading):

npm install sails-hook-autoreload

https://www.npmjs.com/package/sails-hook-autoreload

1
  • 1
    This was already mentioned by Vitalii Maslianok's earlier answer. Jan 25, 2016 at 21:24
9

install nodemon globally or locally.

npm install nodemon --save
npm install nodemon -g

install sails locally in you project as follows

npm install sails --save

then change package.json

from

"scripts": {
  "debug": "node debug app.js",
  "start": "node app.js"
},

to

"scripts": {
   "debug": "node debug app.js",
   "start": "node app.js",
   "dev": "export NODE_ENV=development && nodemon --ignore 'tmp/*' app.js && exit 0"
},

then

npm run dev
7

I had the same problem and I have solved it using grunt-watch and grunt-forever with sails@beta tasks. The result is 4 grunt commands:

UPDATE: tasks are available in the current sails version (it's no longer beta :>)

  • start Starts the server
  • stop Stops the server
  • restart Restarts the server
  • startWatch Starts the server and waits for changes to restart it (using grunt-watch). This is probably your solution, but the other commands are also useful.

Here's the code - I'm using sails@beta, which includes a tasks directory, I don't know if this is included in previous versions:

  • First of all you have to install forever in your sails directory:

    npm install grunt-forever --save-dev
    
  • tasks/config/forever.js Configure forever task.

    module.exports = function(grunt) {
      grunt.config.set('forever', {
        server: {
           options: {
              index: 'app.js',
              logDir: 'logs'
           }
        }
      });
    
      grunt.loadNpmTasks('grunt-forever');
    };
    
  • tasks/config/watch.js (edit) Edit watch task in order to add a new rule

    // api and assets default rules
    ,
    server: {
        // Server files to watch:
        files: [
            'api/**/*',
            'config/**/*'
        ],
    
        // Restart server
        tasks: ['forever:server:restart']
    }
    
  • tasks/register/watchForever.js Register your custom tasks (this file can be renamed to whatever you want)

    module.exports = function(grunt) {
    // Starts server
      grunt.registerTask('start', [
        'compileAssets',
        'linkAssetsBuild',
        'clean:build',
        'copy:build',
        'forever:server:start'
      ]);
    
      // Restarts the server (if necessary) and waits for changes
      grunt.registerTask('startWatch', [
        'restart',
        'watch:server'
      ]);
    
      // Restarts server
      grunt.registerTask('restart', [
        'forever:server:restart'
      ]);
    
      // Stops server
      grunt.registerTask('stop', [
        'forever:server:stop'
     ]);
    };
    

With this you should be able to use

    grunt startWatch

and make your server wait for changes to be restarted :>

Hope this helped!

7

Better you use

npm install -g nodemon

i am using this, and it will helps to improve my developing speed. no need to edit any files for this one!.

after installation

nodemon app.js
2
  • Nodemon is good if you want to restart your server for each save. But if you are using webstorm or any ide with auto save it will continuesly restart your server.
    – Vishnu KR
    Mar 14, 2017 at 2:30
  • Exactly Mr.Vishnu KR.
    – BINFAS K
    Mar 14, 2017 at 2:35
1

For anyone coming to this question now, it seems that this is no longer necessary - an application launched with sails lift will have a grunt watch task running, and code changes will be visible without a restart.

I didn't realise this was happening at first because there's nothing to indicate what's happening in the console, but it does seem to work without a restart (I'm using Sails 0.11)

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.