in an older blog post i described how to host node.js for free using heroku. now i show a way how to install node.js, npm and composer on any other shared host without root privileges with just a few lines of code. you can install packages, deploy vue.js applications or just start an express.js server. we always work in the user directory and use nvm - the node package manager.
It is important to make sure that composer and node are not present and that the following lines do not produce any output:
16c389ff1cdd9380cc5f9244775eb6c8
First we set temporary environment variables that we will need later. To do this, we determine the user's user directory (usually ~ /, on some shared hosts also / www / htdocs / xxxxxxx /) and the desired PHP version (for example with the provider All-Incl / usr / bin / php72, with DomainFactory / usr / local / bin / php7-72LATEST-CLI):
16c389ff1cdd9380cc5f9244775eb6c8
Now we make sure that PHP is running in the latest version on the console by creating a new folder in the home directory, creating a soft link and adding the directory to the beginning of the PATH.
16c389ff1cdd9380cc5f9244775eb6c8
Now we install global composer by downloading, checking and executing the installation script, then also placing composer.phar in the user directory and creating a symlink again:
16c389ff1cdd9380cc5f9244775eb6c8
We use nvm to install Node.js. This is a bash script that actually serves to easily switch between node versions. We use the property here that Node.js is installed without root rights:
16c389ff1cdd9380cc5f9244775eb6c8
With me a small correction in the .bashrc was still necessary:
16c389ff1cdd9380cc5f9244775eb6c8
Now we can install and activate the current stable version of Node.js:
16c389ff1cdd9380cc5f9244775eb6c8
With the help of nvm we can also easily switch versions.
16c389ff1cdd9380cc5f9244775eb6c8
I was able to carry out all of these steps with some German shared hosting providers without any problems. Some hosts have problems with symlinks, but aliase can also help . For example, the following .bashrc helps with DomainFactory:
16c389ff1cdd9380cc5f9244775eb6c8
also there are problems for example when accessing /proc/stat in virtual / shared environments, so for example the node function os.cpus() returns undefined instead of the correct core count. this leads to error messages like "Cannot read property 'length' of undefined" when installing npm packages. i was able to solve the problem by using a recent node.js version (e.g. 11.15.0) which addressed the problem.