Install the most recent node.js version on your QNAP

QNAP considers node.js V0.8 as good enough and only supplies this version for direct download. I found several user made packages with newer versions and instructions how to hack the package supplied by QNAP, but none of the suggested solutions was satisfying me.

Here you find a step-by-step instruction how I installed node.js V0.12.3 on my QNAP TS-253.

Prerequisites: create shared folder for your node apps

Create a shared folder in your QNAP admin tool (the web UI, found on port 8080) used for your node applications and logs. I called it node.

In the filesystem, this folder has the following path: /share/CACHEDEV1_DATA/node

For the next steps we need ssh access, make sure you enabled it on your QNAP.

Install node.js

First I created a lib folder intended to contain all libraries and binaries I’ll download for my QNAP in future (not only node).

# cd /share/CACHEDEV1_DATA
# mkdir lib
# cd lib

Get node.js (check the current version on their web site), and extract it into this library folder:

# wget http://nodejs.org/dist/v0.12.3/node-v0.12.3-linux-x86.tar.gz
# tar zxf node-v0.12.3-linux-x86.tar.gz

Create a symbolic link (should make future upgrades easier) and also create the lib and bin folders:

# ln -s node-v0.12.3-linux-x86 node
# mkdir lib
# mkdir bin
# cd bin
# ln -s /share/CACHEDEV1_DATA/lib/node/bin/node /share/CACHEDEV1_DATA/lib/bin/node
# ln -s /share/CACHEDEV1_DATA/lib/node/bin/npm /share/CACHEDEV1_DATA/lib/bin/npm

As found in Panshins blog, a file in an npm library has to be modified in order to run npm as expected:

# vi /share/CACHEDEV1_DATA/lib/node/lib/node_modules/npm/node_modules/uid-number/uid-number.js

change the line ,uidSupport = process.getuid && process.setuid to ,uidSupport = false

Make node.js available after reboot

Up to now we only installed the binaries of node. As the system is set up every time after reboot, it makes no sense to set symbolic links into the /usr/bin directory at this time – they will be lost. That’s why we need to edit (respectively create) the autorun.sh file which is executed every time the system boots. This file is not easily accessible (the partition where it resides is not mounted during normal operation of the QNAP), so I decided to keep it small and simple while adding the needed functionality in an own shell script in my node folder (having also the name autorun.sh).

Create / edit /dev/sdc6/autorun.sh

The directory /dev/sdc6 is not valid for all QNAPs, if it doesn’t fit for your device, ask Google which directory you have to mount.

# mount -t ext2 /dev/sdc6 /tmp/config
# vi /tmp/config/autorun.sh

My autorun.sh has the following content:

#!/bin/sh
/share/CACHEDEV1_DATA/node/autorun.sh

Check the access rights (should be executable), the unmount again:

# umount /tmp/config

Create /share/CACHEDEV1_DATA/node/autorun.sh

This is the autorun.sh file in the share folder, being called during startup.
In this autorun file I have the following actions:

  • extend the PATH variable to our lib/bin folder (for every user logging in, resulting in extending the bash profiles)
  • configure npm to store its data in our shared folder (not the root partition which is to small)
    My file has the following contents:
#!/bin/sh 
# Set the path to the node bin folder 
PATH=/share/CACHEDEV1_DATA/lib/bin:$PATH 
export PATH 

# set directories of npm 
/share/CACHEDEV1_DATA/lib/bin/npm set tmp /share/CACHEDEV1_DATA/node/tmp 
/share/CACHEDEV1_DATA/lib/bin/npm set cache /share/CACHEDEV1_DATA/node/.npm

# set the bash profiles: add PATH and npm settings 
echo "PATH=/share/CACHEDEV1_DATA/lib/bin:$PATH" >> /root/.bash_profile 
echo "export PATH" >> /root/.bash_profile 
echo "npm set tmp /share/CACHEDEV1_DATA/node/tmp" >> /root/.bash_profile 
echo "npm set cache /share/CACHEDEV1_DATA/node/.npm" >> /root/.bash_profile

echo "PATH=/share/CACHEDEV1_DATA/lib/bin:$PATH" >> /etc/profile 
echo "export PATH" >> /etc/profile 
echo "npm set tmp /share/CACHEDEV1_DATA/node/tmp" >> /etc/profile 
echo "npm set cache /share/CACHEDEV1_DATA/node/.npm" >> /etc/profile 

Set the access rights to executable, otherwise the script won’t run. Now we are done, after rebooting the QNAP and login using ssh, node and npm is available from the command line.

How to start your node application automatically using PM2 will be part of a next blog entry.

Thanks to Erik Dietz for his feedback and improving this tutorial!


Leave a Reply