Sails.js - How to use forever and not start grunt automatically on server start? - node.js

I'm running into an issue using Sails.js framework (node.js).
It seems like it runs automatically grunt when the server starts. But I don't want that. In order to prevent this, it is possible to configure in a .sailsrc file that we don't want grunt to start. (http://www.sailsjs.org/documentation/concepts/assets/disabling-grunt)
But this specific configuration file is only taken into account when we run the server using sails lift command. It won't be used if we use node app.js.
Point is, I want to use forever with sails. But I don't know how to run forever and take into account the .sailsrc file so that Sails doesn't start grunt on its own.
I'm using BrowserSync and I need to startup things in a really specific order, it works fine when using sails lift and grunt separately, but I don't get any hot refresh that way.
I found a similar issue here, but it doesn't help me much. Error when use forever in sails

I assume you get the "Your .sailsrc file(s) will be ignored." error.
Running npm install rc --save in both the project root directory and node_modules/sails, and adding
"hooks": {
"grunt": false
}
to .sailsrc solved this issue for me.
Update: if you're using an older version of Sails, such as sails#0.10.5 you have to change the line "rc": "^0.5.5” to "rc": "^0.5.0" in node_modules/sails/package.json, remove rc and reinstall it with the older version of rc (which is used in the newer versions of Sails).

In the sails book from Manning:
Note: Grunt is also optional. If for some reason you enjoy doing manual repetitive tasks simply delete the Gruntfile.js from the root of your project. No more Grunt. When you restart Sails via sails lift warnings that the “Gruntfile could not be found” and that “no grunt tasks will be run” are displayed.
Give that a shot

Setting hooks.grunt = false keeps being valid for Sails v1.x

Related

Why server restart is needed in Node.Js for every change?

I am very new in Node.Js. I just started node.js basic tutorial. But when I change my code I have to restart the server all the times. But is there any way where no need to restart the server again and again.
Nodemon is the best solution for this.
Install nodemon like this "npm i nodemon"
Then restart your project with nodemon, "nodemon app"
You are good to go...
You can install node-supervisor to restart automatically your server when you change the code.
I'm not sure on the details of the compilation process. But I think it's correct to say that on app start, your source code is parsed into computer instructions represented in memory and executed. During runtime source code files are not re-parsed. And so changing the source code will have no effect on the running application. Unless the application re-parses a file prior to execution of the code in that file. Possibly a service worker... But I'm not sure and that would be an exception.
A good way of thinking of nodejs and javascript files (imo) is that the javascript files are configuration for nodejs. Which is a c++ app. So if the configuration changes you need to restart node to read the new configuration.
There are tools such as nodemon that will monitor the source code for file saves and trigger the node application to restart.
Check out Nodemon.
nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.
nodemon does not require any changes to your code or method of development. nodemon simply wraps your node application and keeps an eye on any files that have changed. Remember that nodemon is a replacement wrapper for node, think of it as replacing the word "node" on the command line when you run your script.

heroku running gulp build

i am trying to install a node app onto heroku. My application requires both gulp build and gulp install to be called on the server upon instillation, however i am having troubles doing this.
I have tried writing this inside of the package.json:
"scripts":"gulp build && gulp install"
which works at first, but after about an hour the server must restart and recall the script causing for the contents of the server to be rebuilt. this removes all of my content such as newly written blogs or changes.
I have also tried using the heroku toolbelt
heroku run gulp build
heroku run gulp install
However, even though it produces the correct console write lines and looks asif it has made the necessary changes, it hasn't, the server produces an error which proves that the resources haven't been built meaning that the console command didnt make any changes to the actual server files.
Am i missing something out here? Thanks in advance.
So i have figured out that the problem lies with the fact that i have tried to use a LevelDB database on heroku. Apparently, heroku uses no-writable dynos, meaning that the levelDB database is unable to save.

Webstorm: debugging node.js sails app

I'm having a hard time finding the right combination of settings to allow me to debug my node.js sails application in webstorm.
These are the settings I've tried using, both configurations work (as in sails starts) but none is stopping at breakpoints.
If I go to the breakpoints menu and turn on 'Any exception enabled' - it does stop for exceptions, but never hits my breakpoints.
Any ideas? I'm on webstorm 7.0.1 and Node 0.10.18 / Sails 0.9.7
You don't need the --debug in node parameters. Here is my config and i am on 7.0.1 and node 0.10.4
Let me know if it helped. Also, you are hitting the bug to run debug right?
Also, can you please invalidate caches/ restart? That helps sometimes. Here is a snapshot in the file menu.
I had the same problem and simply installed sails local to the project and everything worked fine. Not sure why the configuration can't resolve the globally installed sails as that is the error I was getting, but the local install works fine. I should investigate further, but I'm lazy :)
When you open a new project on WebStorm, it detects the package.json file and it will ask you to install the dependencies locally. After that, by using the configuration mentioned before (not necessarily by using sails lift but using app.js directly instead), you can debug you application from WebStorm. Another way to do it, is by using this tool, that works like a charm: https://github.com/node-inspector/node-inspector :)

How to edit and deploy code without restarting server?

I have node server which i run using forever. But each time if I edit my code I'll have to restart the server. I came across the module called hotnode which can perform live edits but will it have the same performance as the forever module or can I run my code using both the modules. I am confused. Any help wil be much helpful
Have a look at nodemon.
nodemon will watch the files in the directory that nodemon was started, and if they change, it will automatically restart your node application.
As an alternative to nodemon you can use node-supervisor.
I used to use nodemon, but for some reason it didn't detect code changes on my linux box, which supervisor did flawlessly.
The downside is that it doesn't (or at least didn't) give the colorful output nodemon gives.

does node have a place to put custom console methods like ~/.irbrc?

I was thinking it would be handy if I could reload! code in the Node console, something to the effect of:
reload('./path/to/my/file.js')
that would delete the code from the cache then load it again -- handy for exercising prototype code. Seemed like a natural function to place in node's equivalent of ~/.irbrc. But I can't find that. Does node have such a thing?
I don't know there's such a tool. But I believe what you need can be achieved by using nodemon.
Use sudo npm install nodemon -g to install it. Then launch your application by nodemon app.js instead of node app.js. Then it watches for changes to nearby .js files, and automatically restart when you save.

Resources