Babel doesn't watch for file change
For now I just restart manually after making changes because babel doesn't automatically transpile original code when changes are made.
So basically I feel getting no benefits from using nodemon from this setup.
Should I use another library for watching purpose?
I agree with this. I can see that nodemon notices a change and restarts; however, it doesn't transpile the code and rerun the new code. That kind of defeats the purpose of nodemon watching for changes, doesn't it?
This was how I decided to setup my scripts. It restarts upon file changes and transpiles the code (using babel) correctly:
{
"scripts": {
"build": "babel -d lib src",
"watch": "nodemon --watch src --exec yarn start",
"start": "yarn build && yarn serve",
"serve": "node lib/index",
"test": "echo \"No test specified\" && exit 0"
},
}
@bryandbor Thank you for help! It works nicely. but it seems little bit slow because it builds whole code for every changes. Do you think any better solution exist?
@ifndefdeadmau5 Maybe this could work.
"serve": "babel -w lib -d dist & nodemon dist --delay 800ms "
babel should just watch changes and convert to dist. Then nodemon picks up the new changes in the dist directory. The delay is be needed as nodemon will go crazy and restart everytime babel creates new files.
@bryandbor Thank you for help! It works nicely. but it seems little bit slow because it builds whole code for every changes. Do you think any better solution exist?
@ifndefdeadmau5 I think the solution you want is something like
"start": "nodemon --exec babel-node lib/index.js",
So what happens here - we just say nodemon to look for the initial file which is lib/index.js
@SmolinPavel Thank you for the solution. Just to note that Babel do not recommend using babel-node for production as they state here: https://babeljs.io/docs/en/babel-node#not-meant-for-production-use
Not meant for production use You should not be using babel-node in production. It is unnecessarily heavy, with high memory usage due to the cache being stored in memory. You will also always experience a startup performance penalty as the entire app needs to be compiled on the fly.
@SaulSilver Oh yeah, good shout! 👍 Thanks
You can set up your scripts like this:
"scripts": {
"dev": "nodemon lib/index.js",
"watch": "babel src --watch --out-dir lib"
}
and run them in separate terminals; yarn watch or npm run watch will watch changes on src folder and yarn dev or npm run dev will run the compiled file with nodemon. 🙏