How to run babel-node in deattach mode - node.js

I follow this tutorial.
It uses NODE_ENV=production node_modules/.bin/babel-node --presets 'react,es2015' src/server.js
My 1st question is:
How to I run the command line above in deattach mode? i.e. the command line above doesn't allow me to quit the terminal.
My 2nd question is:
However, from the docs (https://babeljs.io/docs/usage/..., it is said that you shouldn't use babel-node in production, as it is very heavy. So what is the proper way tot run the server?

Related

How do I use nodemon's --inspect option correctly?

Preface: Attempting to write a discord bot and I have no experience with Node and a small amount with javascript with HTML.
When I input nodemon --inspect Version 1.0.js into powershell it just gives me this as the return message:
Usage: nodemon [nodemon options] [script.js] [args]
See "nodemon --help" for more.
I am getting most of my starting resources from this How-To Geek article: https://www.howtogeek.com/364225/how-to-make-your-own-discord-bot/. I don't know how reputible they are when it comes to coding but I thought I'd try it.
From the docs:
If no script is given, nodemon will test for a package.json file and
if found, will run the file associated with the main property (ref).
You can also pass the inspect flag to node through the command line as
you would normally:
nodemon --inspect ./server.js 80

npm-run-all failing when running a "tsc-watch" command in parallel

I am trying to use npm-run-all to manage running two servers at the same time. For one of the servers, I am trying to run it in a watch mode using tsc-watch.
The command looks like such:
"start": "tsc-watch --onSuccess \"node dist/server.js\""
npm-run-all starts up both of the servers successfully. tsc-watch also watches the Typescript file for changes and automatically recompiles it successfully.
However, when tsc-watch recompiles the Typescript file and restarts the server, it seems like npm-run-all stops working properly. Specifically, if I try to kill both of the servers by pressing CTRL + C in the terminal (OSX), it will only kill the server that tsc-watch recompiled while the other server remains running.
I'm thinking that there must be a way to fix this. Anyone have some tips for me?
Found an answer. I think this is just a bug with npm-run-all. Using concurrently instead doesn't experience this issue (kills both servers when hitting CTRL-C).

How to run a 'watch' script along with a 'start' script in my node.js project

I'm writing an app that is composed of microservices (I use micro).
I really like es6, so I use Babel to make the development process easier. The problem that I have is that I need a script that would compile my es6 code and restarted the 'server'; I don't know how to achieve this.
Right now I have the following script in my package.json:
"scripts": {
"start": "yarn run build && micro",
"build": "./node_modules/.bin/babel src --out-dir lib"
},
When I run yarn start my es6 code compiles successfully and micro starts the server. However, if I make changes to my code, I'll have to manually stop the server and run yarn start again.
I've tried to change my build script
"build": "./node_modules/.bin/babel src --watch --out-dir lib"
But in this case the micro command does not get executed as the build script just watches for changes and blocks anything else from execution. My goal is to have a script that would watch for changes and restart the server if a change occurred (compiling the code beforehand) like in Meteor.
One option is using ParallelShell module to run shell commands in parallel. You can find an example of how to use it here
The simplest solution would be to yarn run build & micro (note the single & and not &&).
As mentioned by others, parallelshell is another good hack (probably more robust than &).

Waiting for webpack bundling before launching nodemon

I have a nodejs project written in Typescript. Therefore, i have webpack using a typescript loader that transpiles my code in Javascript and bundles it in a server.js file (in a dist folder)
When in developement conditions, my webpack runs with its watcher ON and so does nodemon.
Problem is, when i launch my script for the first time combining webpack and nodemon, since webpack is in watch mode it doesn't have an exit code saying that everything is ok, nodemon script can be started. If i run them simultaneously, nodemon will launch faster than webpack and since server.js file doesn't yet exist, it will crash at the start.
I want to launch thanks to one single command both scripts but make nodemon command wait for the bundling to be done.
First of all, when please provide some code when submitting questions.
and since server.js file doesn't yet exist
I think you should work around your setup a little bit s.t. webpack doesn't create your server.js file if you want to do this.
Basically you can chain multiple commands in a script like so webpack -d && nodemon index.js. This will launch node after webpack completes. However if you setup webpack in watch mode -w it never exists, so you can't chain another command to it. So webpack -d -w && nodemon index.js never gets to the nodemon part.
A solution to the above is to chain them using only &, which I guess you are doing, but in this way they both start at the same time. Hence, if you make your setup independent (webpack doesn't interfere with nodemon starting script) you can list them like so.
If for whatever reasons you can't do this or don't want to, your only option is with 2 separate scripts that you launch manually one after the other.
If I were you, I would just use nodemon-webpack-plugin:
Uses Nodemon to watch and restart your module's output file, but only
when webpack is in watch mode (ie, --watch).
Saves the need for installing, configuring and running Nodemon as a
seperate process.

prestart scripts in Loopback

I need to create an ssh tunnel to connect to my database, so the logical point would be to add the shell command in package.json under scripts/prestart.
However, as it seems, slc run does not execute this script, npm start does, but of course doesn't run the loopback module.
I could think of a few workarounds such as writing a startup script that both creates the ssh tunnel and calls slc run, or running slc run in package.json scripts/start, but all these workarounds stink.
Is there a better way to do this?
I'm not sure how useful this is but 1 option I thought of was that if you don't really make use of slc run features you could just define your own npm start run script and hit the server script directly and add in whatever other tooling you need such as nodemon or supervisor etc.:
"start": "node server/server.js"
If you do need to use slc run, I think I would tend toward just wrapping slc run in your start script as you mentioned in your question. It kinda stinks... but not that badly.

Resources