Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing NodeJS with Vagrant shell provisioner not working

I'm trying to install node.js and some packages through Vagrant and shell provisioner.

I find three cases:

1- Simple vagrant up and introducing commands manually once inside vagrant ssh

  • This one works perfectly, thats why I dont understand why the next 2 cases doesn't

2- Installing from bootstrap.sh called in VagrantFile with privileges:false

  • During vagrant up everything is installed (it even says "Now using node v0.11.14"

  • Once inside vagrant ssh node, nvm and npm are in my vagrant user folder, but node command is not recognized, as well as npm. nvm is recognized but nvm ls lists nothing.

3- Installing from bootstrap.sh called in VagrantFile with privileges:true

  • During vagrant up everything is installed (it even says "Now using node v0.11.14"

  • node, nvm and npm are installed in /root directory. node_modules is in the vagrant user directory.

  • Commands node and npm not found

Here the contents of VagrantFile and bootstrap.sh

VagrantFile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "forwarded_port", guest: 3306, host: 8081

  config.vm.provision "shell", path: "bootstrap.sh", privileged: false

  config.vm.synced_folder "Server", "/home/vagrant/Server"

end

bootstrap.sh

#!/usr/bin/env bash

sudo apt-get update
sudo apt-get -y install build-essential libssl-dev

curl https://raw.githubusercontent.com/creationix/nvm/v0.17.2/install.sh | bash

source ~/.nvm/nvm.sh

nvm install 0.11.14

npm install express
npm install -g nodemon

Thanks!

like image 231
Didhack Avatar asked Jan 23 '26 08:01

Didhack


1 Answers

You need to either add nvm use 0.11.14 or add nvm alias default 0.11.14 to your provisioning script so that nvm know which version to use when you run an npm or node command.

like image 137
djheru Avatar answered Jan 25 '26 22:01

djheru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!