... in the previous chapters...

How to call child component method with forwardRef and useImperativeHandle in React

Calling a child method is a common architecture problem that occurs in a React app. You can pass individual properties to child components, you can alter the component state inside of the same component, but how to actually call another component's method?
There's a way to do that by using a combination of forwardRef with useImperativeHandle.
What exactly are those functions for? Let's quote the documentation:
forwardRef lets...

[!] RollupError: Node tried to load your configuration as an ES module even though it is likely CommonJS. To resolve this, change the extension of your configuration to ".cjs" or pass the "--bundleConfigAsCjs" flag.

Sounds familiar?
It took me a while to figure out that the actual error is written lower:
Original error: module is not defined in ES module scope.
What this means is there's no variable named module in the rollup.config.mjs file.
To fix that, you just need to replace CommonJS export to ES export, which means your module.exports = { should become export...

Animated.loop in React Native doesn't make the animation loop on web

It took me a while of testing, but whatever I tried resulted in one animation loop when compiling a React Native component for the Web. Turns out, there's one parameter that was causing it.
Whenever you create a looped animation with Animated.loop() and Animated.timing(), remember about the parameter useNativeDriver in Animated.timing: it has to be false for web, as there's no native driver per sē. For example:
import MaterialCommunityIcons from '@expo/vector-icons/MaterialCommunityIcons'; const AnimatedIconComponent = Animated.createAnimatedComponent(MaterialCommunityIcons);...

View summary of all Docker containers memory usage

You can get pretty much all resource usage information with docker stats, but what you will lack there is the summary.
However, with some command line magic you can get the summary. You can put this in a .sh script, and make it executable with chmod +x docker-memory.sh:
mem_amount_total_with_unit=$(docker system info \ | grep 'Total Memory: ' \ | tr -d 'Total Memory: ') unit=$(echo ${mem_amount_total_with_unit} \ | sed 's/[0-9\.]*//g') mem_amount_total=$(echo ${mem_amount_total_with_unit} \ | sed...

Error: Cannot find module 'node:process' after Rollup upgrade

As silly as it gets, the solution is to upgrade your NodeJS version.
> rollup -c -w internal/modules/cjs/loader.js:905 throw err; ^ Error: Cannot find module 'node:process' Require stack: - ...
My current version was 14.17.1, but after I upgraded to 14.21.1 the error was gone.

Beautify JSON object with built-in JSON.stringify()

You don't need to write a recursive loop to nice-format an object or an array. There is a JSON.stringify() that you can utilize.
Let's imagine we have a relatively simple object like this: let o = { a: 1, b: "test", c: [1, 2, 3] };
In its simplest form, to format this object into a string, you would run JSON.stringify(o);. This will produce a string '{"a":1,"b":"test","c":[1,2,3]}'. Now, to make it nicer, one would run:
...

Simple load balancer with Docker, Nginx and Node.js

It is a common knowledge that Node.js is a single-threaded single-core process. It sounds like an issue, but it gives you full freedom over how many processes you can run and how many cores want to utilize.
There are several ways you can scale Node over whole machine: run multiple processes, use Node to run subprocesses, or use virtualization software. We will focus on the latter, and see how we can also load-balance requests between all of them with a quiet primitive Nginx configuration.
...

Block user based on IP address in Express or any other Node HTTP server

This is an easy task, but you will need to find a way to get the user's IP address. And here's how.
First, let's assume we have a simple Express server:
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { ... }); app.listen(port, () => console.log(`App listening on port ${port}!`))
Now we only need to get the IP address and end...

Running Node.js with Docker

You don't have to install Node and manage different Node versions on your system. Everything can be done with Docker, including packaging your Node.js app into a distributable Docker image.
This assumes that you have Docker installed, you can verify it works with docker -v or anything similar, and you understand the basics behind images, containers, volumes, and such.
Node is available as an official Docker image at Read More →

error TS2792: Cannot find module 'moment'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?

Got this error when trying to import moment into a TypeScript React (.tsx) module?
To fix the issue, you will need to adjust your tsconfig.json file and add "moduleResolution": "node" to your compilerOptions, so it looks like this:
{ "compilerOptions": { ... "moduleResolution": "node", ... } }
The "moduleResolution": "node" setting in tsconfig.json tells the TypeScript compiler to use Node.js module resolution logic when resolving module names. This allows...

Detect user country by IP in Node.js

It shouldn't be hard to detect a user's country based on the IP address, right? Right... I really liked the simplicity of ip2location module: load the BIN file, get the location, and close the BIN file.
Prerequisites Before you install the module, you will need to download the BIN file by registering at ip2location and choosing the appropriate option. You will have a choice between the country, country + city, or other options....

Cannot augment module 'console' with value exports because it resolves to a non-module entity.

This error message appears when typings are exposed globally, yet TypeScript tries to validate module typings and the names conflict.
I got this with a Figma widget module @figma/plugin-typings, and the solution was quite simple - add this to your .tsconfig.json:
{ "compilerOptions": { ... "skipLibCheck": true, ...
Et voilà!

About Code with Node.js

This is a personal blog and reference point of a Node.js developer.

I write and explain how different Node and JavaScript aspects work, as well as research popular and cool packages, and of course fail time to time.