Can't update nodejs version on machine executor

I have the following step in my config.yml file.

e2e:
        machine: true
        working_directory: *workspace_root
        steps:
          - checkout
          - run:
              name: Install dependencies
              command: |
                wget -qO- https://deb.nodesource.com/setup_10.x | sudo -E bash && \
                sudo apt-get install -y nodejs && \
                node -v && \
                sudo npm install -g yarn
          - run: yarn run e2e

When testing this locally using circleci local execute --job e2e I can see the correct nodejs version on the node -v step:

Get:1 https://deb.nodesource.com/node_10.x stretch/main amd64 nodejs amd64 10.12.0-1nodesource1 [15.0 MB]
Fetched 15.0 MB in 1s (8549 kB/s) 
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package nodejs.
(Reading database ... 33164 files and directories currently installed.)
Preparing to unpack .../nodejs_10.12.0-1nodesource1_amd64.deb ...
Unpacking nodejs (10.12.0-1nodesource1) ...
Setting up nodejs (10.12.0-1nodesource1) ...
v10.12.0

However when running it on circleci.com the nodejs version stays the same

Get:1 https://deb.nodesource.com/node_10.x/ trusty/main nodejs amd64 10.1.0-1nodesource1 [13.5 MB]

0% [1 nodejs 15.9 kB]68% [1 nodejs 9,109 kB]Fetched 13.5 MB in 1s (10.1 MB/s)
debconf: unable to initialize frontend: Dialog
debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
debconf: falling back to frontend: Readline
Selecting previously unselected package nodejs.
(Reading database ... 75%
(Reading database ... 71348 files and directories currently installed.)
Preparing to unpack .../nodejs_10.1.0-1nodesource1_amd64.deb ...
Unpacking nodejs (10.1.0-1nodesource1) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up nodejs (10.1.0-1nodesource1) ...
v6.1.0

This stops me from installing yarn as it requires a different node version.

Thanks

Hello Alex,
I would suggest using NVM (Node Version Manager).
Here’s a great guide on using NVM: https://nodesource.com/blog/installing-node-js-tutorial-using-nvm-on-mac-os-x-and-ubuntu/

By default, there's not a way to upgrade the version of Node.js you've got from within Node.js itself. 
That said, there's a fantastic tool for the community called nvm that allows you to manage the versions of Node.js that you've got installed locally.

Hi and thanks for the reply.

I tried the following.

  e2e:
        machine: true
        user: root
        steps:
#          - checkout
          - run:
              name: Install dependencies
              command: |
                curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
                source ~/.bashrc
                nvm install v6.11.1
                export NVM_DIR="$HOME/.nvm"
                [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  
                [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"            - run: nvm --version
#          - run: yarn run e2e

This is the what I see on circleci. The step fails but without a specific error.

#!/bin/bash -eo pipefail
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
source ~/.bashrc
nvm install v6.11.1
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 12461  100 12461    0     0   160k      0 --:--:-- --:--:-- --:--:--  162k
=> nvm is already installed in /opt/circleci/.nvm, trying to update using git

=> => Compressing and cleaning up git repository

=> nvm source string already in /home/circleci/.bashrc
=> Appending bash_completion source string to /home/circleci/.bashrc
npm ERR! peer dep missing: eslint@>=3.1.0, required by eslint-plugin-node@5.2.0
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:

/opt/circleci/.nvm/versions/node/v6.1.0/lib
├── bower@1.8.2
├── coffee-script@1.12.7
├── grunt@1.0.1
├── grunt-cli@1.2.0
├── mocha@4.0.0
├── nodeunit@0.11.1
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` Nodes), you can remove them from the system Node as follows:

     $ nvm use system
     $ npm uninstall -g a_module

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="/opt/circleci/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
Downloading and installing node v6.11.1...
Downloading https://nodejs.org/dist/v6.11.1/node-v6.11.1-linux-x64.tar.xz...

######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v6.11.1 (npm v3.10.10)
Exited with code 1

It seems to be playing up when I try to load nvm with [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh. If I remove that line the output is the same but the step passes. Of course if I do nvm --version it will throw an error that the nvm command is not found as I haven’t loaded it yet.

Following some suggestions found on this discussion, we achieved easy upgrading of nodejs on machine executor (see Switch nodejs version on machine executor)

Thanks!