David Walsh Blog | Node.js
822 FOLLOWERS
My name is David Walsh. I'm a 33-year old web developer and software engineer from Madison, Wisconsin. This blog is targeted toward all levels of web designers and developers. Follow this blog for tutorials on Node.js.
David Walsh Blog | Node.js
2y ago
I’ve been writing a bunch of jest tests recently for libraries that use the underlying window.crypto methods like getRandomValues() and window.crypto.subtle key management methods. One problem I run into is that the window.crypto object isn’t available, so I need to shim it.
To use the window.crypto methods, you will need Node 15+. You can set the window.crypto by importing the crypto package and setting it on the global:
const crypto = require('crypto').webcrypto;
// Shims the crypto property onto global
global.crypto = crypto;
I really loathe creating mock functions for missing libraries ..read more
David Walsh Blog | Node.js
3y ago
One of the first commands you learn when experimenting with command line is rm, the utility for deleting files and directories. Deletion is a core computer UI operation but operating systems use a “Trash” paradigm, where files are stored before truly deleted. With the rm utility, however, files are immediately, permanently deleted.
If you’re like me and afraid to automate permanent file deletion, you can opt for a utility named trash. This nice Node.js library moves files to the trash instead of instant deletion.
// Install with `yarn add trash`
// Move a file to trash
const trash = require ..read more
David Walsh Blog | Node.js
3y ago
As I’ve shown you in the past, nvm an excellent utility for switching between Node.js versions. Whether your host machine or CI, building and testing your apps on different Node versions is a necessity. I’ve recently found a few nvm commands that I found really useful during local development.
To set a default node version on your machine, without touching your system’s install, you can use:
nvm alias default [version_here]
To automatically switch to the node version that best suits a project’s package.json file, simply type:
nvm use
I’ve always loved nvm but I still catch myself manually ..read more
David Walsh Blog | Node.js
4y ago
When I wanted to refresh my React.js skills, I quickly moved to create a dashboard of cryptocurrencies, their prices, and and other aspects of digital value. Getting rolling with React.js is a breeze — create-react-app {name} and you’re off and running. Getting the API working isn’t quick, especially if they don’t accept cross-origin requests.
I set out to find the easiest possible Node.js proxy and I think I found it: http-proxy-middleware; check out how easy it was to use:
// ... after `npm install express http-proxy-middleware`
const express = require('express');
const { createProxyMiddle ..read more
David Walsh Blog | Node.js
5y ago
QR codes aren’t everyone’s cup of tea but I quite like them. If I see something I want to remember or check out later, especially when on the road, it’s super easy to take a quick picture — it’s much easier than trying to remember a URL and much faster than typing it in on a tiny keyboard.
If you need to generate QR codes, for a client or yourself, there’s a really nice JavaScript project: node-qrcode. Let’s look at the different wys and output formats you can use to create a QR code!
Start by installing the library:
yarn add qrcode
Create QR Code Image Data
With the QR cod ..read more
David Walsh Blog | Node.js
5y ago
The webpack JavaScript utility has taken over the modern JavaScript landscape, so much so that it’s hard to be a JavaScript developer and not use it. JavaScript build utilities are the point where they do best practices implicitly, like minify code, caching, and more.
I was recently debugging a bundled webpack app and it quickly became clear that the only way forward was debugging the actual source, not the minified code. Duh.y
To prevent webpack from minifying the source, add the following to your webpack config:
{
// .... other webpack, like output, etc.
optimization ..read more
David Walsh Blog | Node.js
5y ago
These days, most front-end projects are going to involve NPM packages of some kind. Occasionally, when browsing documentation for these packages, I’ll see a recommendation to install a package like this.
yarn global add <package>
Or like this.
npm install --global <package>
In both of these examples, the package is installed globally. This means you can run the <package> command from any directory on your system.
This works, but installing packages globally has a couple downsides.
If you’re working with a team of developers, it’s hard to guarantee everyone i ..read more
David Walsh Blog | Node.js
5y ago
Images are a great way to communicate without text but oftentimes images are used/abused to spread text within social media and advertisements. Text in images also presents an accessibility issue. The truth is that it’s important, for any number of reasons, to be able to detect text in image files. The amazing open source tool that makes detecting text in images possible is tesseract OCR!
I recommend using Homebrew to install tesseract:
brew install tesseract
To run tesseract to read text from an image, you can run the following from command line:
tesseract ~/Downloads/MyImage.png ..read more