Many setups already expect binaries to be found in ~/.local/bin/. So this answer follows that convention. Other files will get installed to ~/.local/lib/node_modules/.
1. Configure npm
Run:
npm config set prefix '~/.local/'
This modifies ~/.npmrc to include this line:
prefix=~/.local/
2. Make sure ~/.local/bin exists and is in your PATH
Run echo "$PATH" to have a look at your path. If it does not include ~/.local/bin/ already, you will need to configure your system to include it.
mkdir -p ~/.local/bin
echo 'export PATH=~/.local/bin/:$PATH' >> ~/.bashrc
Replace .bashrc with the configuration file of the shell that you are using.
3. Install packages globally
npm install -g packagename