Skip to content

How to setup NodeJS for global package installation in Guix

Posted on:November 3, 2022 at 07:00 PM

Table of contents

Open Table of contents

Install node from guix repo

Either install it into user’s guix profile using the following command:

guix pull
guix install node

Or install it system-wide by adding the line (specification->package “node”) to your system’s config file /etc/config.scm as shown below:

(operating-system
  ...
  (packages
    (append
      (list
        ...
        (specification->package "node")
        ...)
      %base-packages))
...)

Reconfigure the system if installing node system-wide:

guix pull
sudo guix system reconfigure /etc/config.scm

Setup custom install folder for npm packages

Create a folder .vnode for npm global installation of packages in home instead of root and enable it:

mkdir $HOME/.vnode
npm config set prefix "$HOME/.vnode"

Add the bin folder inside .vnode directory to /.bashrc or /.zshrc and then source the file. It’s recommended that you logout and log back in. Otherwise, you’ll need to source the directory everytime you need to use the npm.

echo "export PATH=$HOME/.vnode/bin:$PATH" >> ~/.bashrc
source ~/.bashrc

Install npm globally and it will install into that directory.

npm install -g npm

Setup preinstall script

Now add the following preinstall script into $HOME/.vnode/lib/node_modules/.hooks/preinstall using a text editor.

#/usr/bin/env bash

pkg_path=$PWD

function patch_shebang() {
 file=$1
 python_bin=`type -p python`
 ruby_bin=`type -p ruby`
 env_bin=`type -p env`
 bash_bin=`type -p bash`

 if [ -n "$env_bin" ]; then
   sed -i -uE "s|^#!.+/env|#!${env_bin}|" $file
 elif [ -n "$bash_bin" ]; then
   sed -i -uE "s|^#!.+/bash|#!${bash_bin}|" $file
 elif [ -n "$python_bin" ]; then
     sed -i -uE "s|^#!.+/bash|#!${python_bin}|" $file
 elif [ -n "$ruby_bin" ]; then
     sed -i -uE "s|^#!.+/bash|#!${ruby_bin}|" $file
 fi
}

files=`find $pkg_path -type f -exec grep -lE '^#!(.+ )' {} \;`

for file in $files; do
 patch_shebang $file
done

Allow execution of the script.

chmod a+rx $HOME/.vnode/node_modules/.hooks/preinstall

If everything went well, you will now be able to run npm install -g package-name and install global packages into the newly created directory .vnode. For local packages just copy the preinstall script into myprojectdir/node_modules/.hooks/preinstall.