@mkelley33:

Some npm and npx commands to know and use (regularly)

Definitely typed magic

If you're not using TypeScript, then you can probably skip this one, but if you are, then I think it will save you time and typing. Open a shell and type the following:

$ npx typesync

The npx typesync command will output something like this in your package.json under your devDependencies:

"@types/eslint": "~8.56.2",
"@types/nodemailer": "~6.4.14",
"@types/react": "~18.2.54",
"@types/react-dom": "~18.2.18",

Then run npm i to install the dependencies. Note the use of the ~ (tilde) rather than the ^ (caret). In the world of SemVer, using npm up (or another command that I prefer below in this blog post) see tilde dependencies will update all of you packages where the version is prefixed with a tilde to the latest patch version.

Note that this will also update all of your packages prefixed with a caret to the latest minor version, which you may or may not want to do.

Patch verions are generally considered safe, recommended, and non-breaking in terms of changes to a package's public API and usage. Bugfixes and vulnerabilities often get patched, so just do it, run your unit tests, open your app, and test it thoroughly.

Regarding outdated dependencies

You usually don't want outdated packages or dependencies, but you should use following commands early and often:

npm outdated

$ npm outdated

If you run this command, you will reap the benefits of knowing how close or far behind your dependencies are in version.

It is a good practice to do it as frequently as you can, so that you can possibly update to the latest and best versions of software that you depend on before it becomes to cumbersome of a chore.

npm-check-updates

The really cool package to have installed globally is npm-check-updates. I'll tell you why as I roll out the commands beneath the following paragraphs. But first, trust me, and open a shell and install it like so:

$ npm i -g npm-check-updates

Now you have the power of more commands to see and update those outdated packages. You can see outdated packages much like using npm outdated by using the following command instead.

$ npx ncu

If you're up-to-date with your package.json, then you'll see the following message:

"All dependencies match the latest package versions :)"

Otherwise, you'll see what your packages would be updated to if you ran these two commands:

$ npx ncu -ut patch
$ npm i

While npm up may bump all your packages to the latest patch or minor version, it will not update package.json to reflect the latest dependencies that were installed, whereas npx ncu -u will change your package.json and package-lock.json, something I prefer.

Let's say you just want to update on package based on the versioning and semver you've set in your package.json. Then run the following command to do so (in this case only update to the patch level):

npx ncu -ut patch react

The help on npx ncu is noteworthy:

npx ncu -h

Note: npm outdated is still useful if you want to see what the latest package versions are and where they're located on your machine.

A TypeScript REPL

$ npx tsx

Nuff said. Or read more about tsx on npm Let's you test out and play around with TypeScript in a REPL (Read Eval Print Loop) from your terminal.

Other useful npx commands

I have to give major credit to this GitHub gist for some awesome npx commands, and to its author: Gokulakrishnan Kalaikovan aka @gokulkrishh.