The technicalities 5 min read 11 September 2023

NPM & N on MacOSX

Author
Amadeusz Kosik
Amadeusz Kosik

Big Data Engineer

Share
Share:

Most programming languages have their SDK available for non-system-wide installation. Python offers venv, while Java Development Kit needs only the JAVA_HOME variable to be set, etc. At first glance, this does not seem to be the case with Node.js for JavaScript, where developers are expected to install all tools system-wide, but there are ways to achieve it. One is to use a tool called n – a simple-to-use wrapper and manager for node.js, as described below.

Installation

Three short steps that work for MacOSX:

# Install n

brew install n

# Set N_PREFIX to avoid writing into system directories

export N_PREFIX=$HOME/.n

export PATH=$PATH:$N_PREFIX/bin

# Install latest node.js

n latest

The full installation guide is available on the project’s GitHub page: (https://github.com/tj/n#third-party-installers). 

IntelliJ IDEA

I want to use an IDE to work with node.js apps. Unfortunately, editors like IntelliJ fail to find the node command that is not on the system $PATH. One can work around this issue and use the terminal to execute the framework’s command. However, there is also a more comfortable approach – updating the environment variables.

N_PREFIX

This one is pretty simple: create or update the `/etc/launchd.conf` file (source: https://stackoverflow.com/questions/135688/setting-environment-variables-on-os-x): 

setenv N_PREFIX <absolute_path_to_n_prefix>

PATH

In case of $PATH variable there is a different way to set it than using launchd:

# Create a file in /etc/paths.d/ with any file name

# and value of directory you want to append to $PATH

echo "/path/to/.n/bin" | sudo tee -a /etc/paths.d/n

Share
Share:

More insights