From c2026074462b7dd22af7f2807bfd4c904d191eea Mon Sep 17 00:00:00 2001 From: Em Ruszanowski Date: Mon, 17 Mar 2025 22:12:06 -0400 Subject: [PATCH] Add npm article --- _posts/2025-03-17-node.md | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 _posts/2025-03-17-node.md diff --git a/_posts/2025-03-17-node.md b/_posts/2025-03-17-node.md new file mode 100644 index 0000000..d58e6ed --- /dev/null +++ b/_posts/2025-03-17-node.md @@ -0,0 +1,47 @@ +--- +title: "NPM Global Without Sudo" +categories: + - programming +tags: + - nodejs + - npm + - linux + - security +--- + +## Don't Use `sudo` With `npm` + +I have seen quite a few programmers install global packages on their machine with `sudo npm`. Generally, that isn't a great idea. I was inspired by Andrew Crites's article "[Don’t Use `sudo` with `npm` …still](https://www.grammarly.com/blog/punctuation-capitalization/italics/)" to post this quick guide on configuring npm prefixes. + +Why shouldn't you use `sudo` with `npm`? Go read the Crites's article! + +### Creating a `npm` Prefix + +Set a prefix in your npm configuration using the `npm config` command. + +```bash +npm config set prefix './local/' +``` + +This modifies your `~/.npmrc` to include the following line. + +```bash +prefix=~/.local/ +``` + +### Updating `$PATH` + +If you intend to run globally installed packages from the command line you must add the new prefix location to your `$PATH`. Replace `~/.zshrc` with the appropriate configuration file for your shell e.g. `~/.bashrc`. + +```bash +mkdir -p ~/.local/bin +echo 'export PATH="$HOME/.local/bin/:$PATH"' >> ~/.zshrc +``` + +### Installing Global Packages + +You can now install global packages in the scope of your user _without_ `sudo`! + +```bash +npm i -g packagename +```