r/javascript • • Oct 27 '16

Chrome DevTools can now hotswap (live edit) Node.js code natively, made a gif of how to try it out

https://umaar.com/dev-tips/119-node-js-hot-code-swap/
366 Upvotes

39 comments sorted by

9

u/PlNG Oct 27 '16

post in /r/node!

4

u/umaar Oct 27 '16

Good idea, done!

16

u/bogas04 Oct 27 '16

Brilliant!

12

u/PM_ME_YOUR_HIGHFIVE Oct 27 '16

Chrome as an IDE? Not bad. Does this work with typescript?

7

u/leadzor Oct 27 '16

If node was able to run typescript directly I would assume that yes it would be possible. As typescript needs to be compiled to JS I highly doubt it, but don't quote me on that.

55

u/QuoteMe-Bot Oct 27 '16

If node was able to run typescript directly I would assume that yes it would be possible. As typescript needs to be compiled to JS I highly doubt it, but don't quote me on that.

~ /u/leadzor

12

u/[deleted] Oct 27 '16

This bot is brilliant.

3

u/RonGnumber Oct 28 '16

Can I quote you on that?

3

u/acloudbuster Oct 28 '16

I think you can quote Anoian but don't quote me on that.

7

u/QuoteMe-Bot Oct 28 '16

I think you can quote Anoian but don't quote me on that.

~ /u/acloudbuster

4

u/9thHokageHimawari Oct 27 '16

ts-node can run .ts files

1

u/Derimagia Oct 28 '16

ts-node and babel-node and other programs like this are just wrapper scripts to node. Aka they are node scripts.

All most of them are doing is transpiling it down and then just running it through node proper

2

u/cyanydeez Oct 28 '16

people, who uderstand the power of typescript, dont seem to understand how it works or limits production

1

u/PM_ME_YOUR_HIGHFIVE Oct 27 '16

you can do it with front-end typescript

1

u/[deleted] Oct 28 '16

Yeah. I'm assuming it would work here as well if the source maps are properly set up.

1

u/[deleted] Oct 28 '16

Actually it might be possible if the source maps are properly configured :) I debug my browser typescript in chrome all the time.

1

u/leadzor Oct 28 '16

That's a bit different. Source maps help map the runtime code back to the original. The code being ran is still the compiled version. This would require a the file to be watched for changes and compilation to JavaScript. Depending on how long it takes to compile the file back, love edit might not be feasible.

3

u/[deleted] Oct 27 '16

This is actually a phenomenal change. Hopefully we get full ES7 in chrome ASAP so I can ditch transpilation

5

u/BONUSBOX _=O=>_();_() Oct 28 '16

not without all the shiny ES8 features that will be out at that point D:

1

u/[deleted] Oct 28 '16

Yeah, we'll always be transpiling for the latest features and tricks realistically.

2

u/Zeludon JaSON Oct 29 '16

ES7(es2016)s only features were the exponential operator and array.prototype.includes, as well as some changes to strict mode scoping, features like the bind operator, decorators or the async/await syntax are all proposals to the language specification ecmascript, I don't know why people keep calling such features ES7 when that is already released and finalized.

1

u/Knotix Oct 27 '16

Can it save changes that have been made?

3

u/umaar Oct 27 '16

Yep, it can save your changes to disk. You just need to do a one-off right-click and 'save as' on the file, from DevTools.

2

u/FrozenCow Oct 27 '16

Wow, that is awesome! I do wonder how this works exactly though. It seems quite 'magical'. What happens when you rename a variable? What happens with variables in closures? It doesn't seem trivial to handle all of that.

Whatever the case, this is pretty cool.

2

u/[deleted] Oct 27 '16

That is a very good question. I suppose its probably at least a little simplified since JS is interpreted.

1

u/hackel Oct 27 '16

This is really cool, but can someone explain why this would be useful? Just so that you can see your server messages in the same place as client messages? Or am I just hopelessly confused?

If there was a way to live edit client code, now that would be something!

4

u/TheAceOfHearts Oct 28 '16

For client side code, if you're using webpack you can enable and configure hotloading. It's pretty amazing. The best examples of hotlodaing are CSS and React, where you save and the styles / DOM are updated without a refresh. You can also configure it for any module.

This is most useful during development. One such scenario is writing API handlers. While you're developing you probably wanna try stuff out. Restarting the server isn't usually a big deal, but having one less tool to setup is a nice win.

1

u/rizer_ Oct 28 '16

Don't you get all that with regular livereload? This is neat, but I too, fail to see the real day-to-day use.

2

u/[deleted] Oct 28 '16

Well, when you have clicked 5 times to get the app to a certain state you don't want to lose that. With a normal live reload your app would be in a "fresh" state, with hot reload you would keep the state

1

u/[deleted] Oct 28 '16

This right here. Just moved to Webpack and it makes working with forms so much less painful.

1

u/tbranyen Oct 28 '16

Livereload doesn't reload the page with css changes, it swaps out the stylesheet...

1

u/[deleted] Oct 28 '16

So what about changing HTML?

1

u/rizer_ Oct 28 '16

That's fair, I could see it being useful on certain projects :)

1

u/[deleted] Oct 28 '16

I really recommend trying it, I didn't see the use in the beginning but when you get to try it, it's great and you won't wanna go back

2

u/[deleted] Oct 27 '16

for development, perhaps. Although i suppose hotswapping is not quite that useful. What would be truly useful was if i could edit some code, and then immediately test the functions in the console below.

0

u/janlaureys9 Oct 27 '16

Wow, my twitter world and reddit world just collided.

-3

u/IDCh Oct 27 '16

Can't safely reach website. I have an error in chrome, problem with https. :/

-4

u/bart2019 Oct 28 '16

I'm using Node.js 7.0

One more project where the major version number has run out of control.

5

u/charrondev Oct 28 '16

I don't see the issue with following semvar. The v8 engine its based on is up to v55 already so..