Grunt: documentation on how to run these before main program - node.js

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.

Related

What are the tasks performed by "npm start" command in React?

Please help with my below questions:
"npm" is the one which comes when we install Node.js. Am I correct?
"create-react-app" package installs/loads the Babel & Webpack which are required for our React project. Am I correct?
When will the React code gets compiled & Translated? Does the below points are correct?
a. After creating React project and developing some code, we are loading our application in browser by running the "npm start" command. So while running this command, will the Babel is going to compile the React code and convert it to JavaScript code which has ES5 standards? Does this conversion to ES5 happens when we run the "npm start" command?
b. Also I learned that Webpack is going to merge all various files within the React project to a single .js file. So does this merging of all the different files by Webpack will be performed when we run "npm start" command?
So if my above understanding is correct, the React code will be Compiled, Translated & Merged into a single file when we run "npm start" command. Correct?
npm start is really just a command that exists in the package.json which can be configured to do whatever you please. It is typically use to kick off all things needed to "start" the app. In the context of a react app created using create-react-app the start command will call react-scripts start and that points to a file called react-scripts which sits in your node_modules/.bin. If you want to see everything that happens you can read through that file.
In short tho, you are correct that it will use babel to transpile the code to something the browser understands, it will use webpoack to create one bundle file (or multiple if you are using code splitting). It will also start a webpack dev server which will usually listen on port 3000 and it will open your default browser to your app. These default settings can be overwritten in the package.json.
Hope this gives you some clarity.

Lint node.js code on save

I want to lint my node.js code when saving the file (so I don't have to to run npm run eslint manually). If I were to write the frontend, I'd use webpack to bundle and lint my files on save. However, as I currently don't need to bundle my Node.js code (or do I?), I'm not sure if this is the way to go or if I have any other alternative?
How is this usually done with Node.js? I wasn't able to find any answer to that question using Google's or Stackoverflow's search but I might have looked for the wrong thing.
You could use gulp or grunt or any other build tool to watch your project and run es-lint on save.
Or you could just use a text-editor or ide with a js-lint plugin.

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.

nodejs unit testing framework that does not require an external test runner

All of the unit testing frameworks that I know of / can find require a test runner.
They all require you to globally install and run some program that runs your tests. Is there a well supported testing framework that can run as an npm require() ?
I need one like this because I want to be able to debug my tests, and it is much easier for me to do this though webstorm. Also, the project I'm working on is very small and I don't want to get fancy
The node.js project itself simply uses the built-in assert module and JavaScript exceptions. They have a fairly straightforward script that runs every .js file in a directory tree and if the file doesn't throw any exceptions, the test is considered passing. You could use something like that.
However, although most frameworks do have a command line runner, you absolutely never need to install them (or anything) with -g. If you understand the basic concept of the unix PATH environment variable, you can npm install --save-dev mocha (for example) and then run your tests with ./node_modules/.bin/mocha. No -g required.
See also http://peterlyons.com/problog/2012/09/managing-per-project-interpreters-and-the-path

NodeJS + CoffeeScript Workflow

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

Resources