Frontend Masters Boost RSS Feed https://frontendmasters.com/blog Helping Your Journey to Senior Developer Tue, 16 Apr 2024 22:09:46 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 225069128 require(esm) in Node.js https://frontendmasters.com/blog/requireesm-in-node-js/ https://frontendmasters.com/blog/requireesm-in-node-js/#comments Tue, 16 Apr 2024 22:09:17 +0000 https://frontendmasters.com/blog/?p=1707 Joyee Cheung made some waves in Node land last month:

Since ESM was shipped in Node.js, for many years, it was possible to import cjs, but not possible to require(esm). The frustration of ERR_REQUIRE_ESM has bothered many and probably has been the primary source of wasted hours in the Node.js ecosystem. If package authors wanted to make sure that both CJS and ESM users can consume their package, they either had to continue shipping their modules as CJS, or ship both the CJS and ESM version

Apparently, it will go out in v22, which isn’t out yet but should be soon.

It seems to me this should alleviate some of the pain the Node world is still feeling where projects and their dependencies are still a mix of both. But the true impact remains to be seen. Will you be upgrading to v22 to soothe any pain of your own?

]]>
https://frontendmasters.com/blog/requireesm-in-node-js/feed/ 1 1707
Node.js Debugging in Chrome DevTools https://frontendmasters.com/blog/node-js-debugging-in-chrome-devtools/ https://frontendmasters.com/blog/node-js-debugging-in-chrome-devtools/#comments Mon, 08 Apr 2024 23:10:27 +0000 https://frontendmasters.com/blog/?p=1616 I was pairing with my co-worker last week. They had a super different debugging style than I do. I’m aware that I can do fancy things in DevTools, like set breakpoints and whatnot right from within DevTools and use well-placed debugger; statements to halt JavaScript execution and inspect things at that point.

But I hardly ever do any of that.

I’m quite sure I don’t even know all the cool tricks. I’m a console.log() type. I just absolutely litter the code in them until I learn what I need to learn. It’s a little amateur-ish, I admit. I’ve made it this far, but there’s no time like now to level up.

I especially don’t know any tricks with Node.js debugging. To begin with, I didn’t even know you could use Chrome DevTools with Node.js. It sort of makes sense since Node.js uses v8 just like Chrome does, but it never clicked with me to use a browser tool for server code. It’s not a new thing! Paul Irish was telling people how to do this in 2016.

The kind of debugging my co-worker and I were doing was complex enough that having DevTools to help us really worked. I think I’m a believer. For this article though, I’m going to show you how to do it with a simple example. You probably wouldn’t need DevTools for this, but the point is learning how to do it.

1) Have Chrome Open

Then go to chrome://inspect/. The first tab, Devices, has the link you’re looking for.

These settings work for me, but if you run Node at some specific port or local URL, you should add it here.

2) Have Node Code

Kinda goes without saying, but you’re going to need to execute some Node code, likely from your terminal, in order for there to be anything to expect. Might be an npm script or something. Let’s just assume we have a file called app.js and we’re going to run it directly like node app.js.

3) Have a Problem

My goal with this code was to have…

Hello World

… turn into:

Hello 👋 World!!!

That is, put a wave emoji between each word. But the output I was getting was:

undefinedHello 👏 World 👏 !!!.

That’s a problem because:

  1. I don’t want undefined there
  2. It put a wave at the end too
  3. There is a space before the “!!!”.

We must fix this super contrived example!

4) Execute the Node Code with an Inspection Flag

Like this:

node --inspect-brk app.js

That --inspect-brk flag is the real magic here.

When I do that, the terminal output will tell me where Node is running and that the Debugger is attached:

It will automatically pause before it starts executing.

5) Go through the Code

These buttons are of particular interest:

If you press “Play” here (that first blue one), the code will just run and be done without much a chance to inspect anything.

You could use the next button, the dot with a little curved arrow up and over it, to start “stepping” through the code. But Node is complicated, and you’ll soon find yourself in the bowels of Node doing god-knows-what.

The trick is to put a debugger; statement in the code where you start caring about stuff. So…

Now when I run again, I can hit the blue Play button and hop to this exact point the first time it gets there. Now I can start “stepping” through the code to see values. As I step through, I can see the values of relevant variables as I go.

I can also hover over variables to see values as well as see locally-scoped variables in the sidebar on the right.

Here I learned that when I declared the variable and then appended a string to it, I got that undesirable result. Lemme start with a blank string instead.

We also essentially want to skip the last iteration of the loop. So instead we’ll use an old school for loop and run it over the length of all the words minus one. Then plop the last word (and !!!) at the end separately.

5) Restart Things Anytime

As I change my code, I can always Control-C out of the Node process and run it again, and DevTools (thank god) will automatically re-attach.

Here I can start over and step through the code, seeing my changes yield the correct output.

The other debugger buttons (like the down arrow and up arrow) allow you to step through code quicker by fast forwarding to the next function call or finishing the function you’re currently in.


This just feels like a more grown-up way to debug code when things get complicated. Especially for apps where you are jumping between lots of files, you can see all that happening in the DevTools file explorer on the left.

Good luck out there!

]]>
https://frontendmasters.com/blog/node-js-debugging-in-chrome-devtools/feed/ 3 1616
Node.js: The Documentary https://frontendmasters.com/blog/node-js-the-documentary/ https://frontendmasters.com/blog/node-js-the-documentary/#respond Mon, 08 Apr 2024 16:16:09 +0000 https://frontendmasters.com/blog/?p=1568 I enjoyed watching Honeypot’s recent Node.js Documentary. Props to Honeypot for taking the time and resources to do these historical dives into some of our industries most major technologies. I like seeing the human side of these things. I feel like if too much more time passed, documentaries like these would get a lot harder to pull off and may never happen at all. Not to assign bloated importance to my own industry, but web technology affects a massive slice of the population of Earth, so major changes in how it works are a big deal.

They’ve also done Ruby on Rails, React, Vue, Ember, GraphQL, and others. All stuff you can learn on Frontend Masters, weird 😛.

]]>
https://frontendmasters.com/blog/node-js-the-documentary/feed/ 0 1568