NodeJS + CoffeeScript Workflow - node.js

I have recently started learning nodeJS. Being a long-time coffeescript lover I naturally decided to use it along with node. Writing long-running processes with node I found myself restarting the program frequently. After a quick google I found node-supervisor. Node-supervisor simply watches the current directory for file changes and restarts your app for you automatically.
Before I started using supervisor I was using coffeescript with the --watch option to automatically recompile my coffescripts when they changed.
So the problem is this, supervisor and the coffeescript recompiler don't play nice together.
First I run coffee --compile --watch .
Then in a new terminal I run supervisor app.js
After that supervisor keeps restarting my app forever, even when there has been no changes to the source files.
So the question is this, what is your workflow for working with nodeJS and CoffeeScript?

What you are doing is some kind of redundant.
Here are some hints:
after installing CoffeeScript you have an executable called coffee so you can do (no need to compile your coffee-script files):
coffee yourfile.coffee
how to combine this with supervisor?
if you would have read the Readme on the Github page you would have noticed that supervisor can execute CoffeeScript files, too. All you need to do is:
supervisor yourfile.coffee

Related

Deploy NodeJS server on production server with grunt

I'm currently developing a web application with NodeJS.
Grunt is used for deployment and I used to run the server using following shell command.
NODE_ENV="development" grunt
This automatically merges, minifies and starts the node server.
On production server, I run following command to start the server in production environment.
NODE_ENV="production" grunt
This starts the server, but when I disconnect from the server (from ssh), it stops after sometime.
Is it possible to keep the NodeJS server running using grunt-deploy npm?
You will have to daemonize the node process. There are a lot of ways to do this and a lot of libraries out there that will handle this for you such as forever and pm2.
As for how you do this specifically, it depends on many factors like what grunt is actually doing, the availability of libraries, etc. etc. You could use grunt-forever to use forever within Grunt (this seems to be an old library, though).
PM2 also has documentation on deployments. You can update the script to be /path/to/grunt and pass the tasks to run as arguments.
The simplest solution may be to use
nohup NODE_ENV=production grunt &
...but this has not worked consistently for me in the past.

Grunt: documentation on how to run these before main program

I want to run my app like normal:
node app.js
but before that happens, I want to ensure that a particular grunt task is run first.
I am having trouble finding documentation on how to automatically run (and complete) a Grunt task before starting a node command.
To be more specific, I have TypeScript files that I want/need to compile before running my node.js application.
Is there a way to always run the compilation (with Grunt or another way) before running node? Perhaps I could do a simple synchronous call in my app.js to run Grunt, but that wouldn't be cool would it?
You could use grunt to automatically compile typescript into a folder. Then you use nodemon in another window which is watching for changes in that folder.
Then when you change the typescript files, grunt runs (if you have set up grunt-watch) and after compile nodemon restarts the application.
You can run any scenario with Grunt. Here, you can use both grunt-ts and grunt-shell tasks to run your program/server.
First one compiles/transpiles your code. Second one allows you to run any shell command, such as node my_file.js.

How does node.js compile coffeescript?

I'm using coffeescript for my current node.js project, starting the project with the following command (inside my project folder)
coffee app.coffee
Which starts the node application. I am, however, at a loss as to how node.js can interact with the coffeescript - is it compiled to a temporary folder?
Do this:
cat `which coffee`
And you'll see that coffee is actually a node script, which compiles your .coffee file and then runs it.

How do I make grunt watch compass while keeping an express server up?

I understand how to make grunt watch the sass files for changes then compile and how to start an express server as well but I can't figure out how to keep an express server running while watching changes for sass files. I've been googling for an hour and have given up, is this an impossible task? I'm new to grunt.
Did you take a look at Nodemon? https://github.com/remy/nodemon
nodemon will watch the files in the directory where nodemon was started, and if they change, >it will automatically restart your node application.
Does integrate with grunt: https://github.com/ChrisWren/grunt-nodemon

Coffeescript and node-supervisor together?

I'm using coffeescript with --watch option to rebuild javascript on changes to .coffee files.
Is it safe to combine that with node-supervisor to restart Node on changes to the compiled javascript?
I'm worried it won't be robust because of atomicity when coffeescript is recompiling multiple files. node-supervisor could jump the gun and restart Node on detecting the first filesystem change. Is it robust enough to realize there were additional changes while it was busy restarting Node?
Is there a better way? Ideally I'd have only one filesystem watcher recompile my coffeescript and restart node.
Create a JavaScript launcher, i.e. run.js, like this:
require('coffee-script');
require('./launch');
Then run this file with supervisor and appropriate options:
supervisor -e "node|js|coffee" run.js
This worked well for me on Windows.
You can use nodemon, it even has a delay feature (to restart server after a number of seconds have passed), for example:
nodemon --debug ./server.coffee 80
Another good feature of nodemon is ignoring files, example:
# this is my ignore file with a nice comment at the top
/vendor/* # ignore all external submodules
/public/* # static files
./README.md # a specific file
*.css # ignore any CSS files too
Other than that, read the documentation on the github repo and watch this Nodetuts video about nodemon: http://nodetuts.com/tutorials/14-some-nodejs-tools.html
You can use supervisor with -x option set to coffee. This will enable it to run script with right executable:
supervisor -x coffee your-script.coffee
Inspired by Lemming's answer.
In some of my Cakefiles, such as the one for connect-assets, I do the watching myself and simply spawn coffee -co lib src each time something changes, then restart the server when that child process finishes. That gets around the atomicity issue. If every .coffee file changes at once (or if you upgrade the coffee runtime), all of the JS files will update at once as well.
My foreman centric solution looks like this:
Procfile.dev
web: ./node_modules/supervisor/lib/cli-wrapper.js -n exit server.js
watch: ./node_modules/iced-coffee-script/bin/coffee --watch --compile server.iced
and then merely foreman start -f Procfile.dev
Then gitignore the resulting .js files. I like this approach because it keeps a constantly-updating vanilla JS file alongside my .iced files, so I can doublecheck my work as I go (I definitely make mistakes in coffeescript that I might not in vanilla).

Resources