CircleCI forgetting node version on machine executor

Driving me crazy. I’m setting the node version to 10.15.1 with nvm and in the next run step it’s back to 6.1.0 . I’ve tried several variations including this one : https://www.cloudesire.com/how-to-upgrade-node-on-circleci-machine-executor/

Am I missing something obvious? I just need each run step to remember the node version I set in the first one so they will all use 10.15.1 in this case.

Here is the job in my workflow:

dev:
  environment:
    BASH_ENV: run/env/test/.env
  machine:
    image: circleci/classic:latest
  steps:
  - checkout
  - run:
      name: Install node@10
      command: |
        set +e

        curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
        export NVM_DIR="/opt/circleci/.nvm"
        [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
        
        nvm install 10
        nvm alias default 10

        rm -rf ~./node-gyp

        node -v # prints 10.15.1 as expected
        
  - run:
      name: Install yarn and rsync
      command: |
        node -v # prints 6.1.0

        export NVM_DIR="/opt/circleci/.nvm"
        [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

        node -v # prints 6.1.0

        curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
        echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
        sudo apt-get update && sudo apt-get install yarn rsync

  - run:
      name: Install node modules
      command: |
        node -v # prints 6.1.0

        yarn install # this is what is failing because of the unexpected node version

  - run:
      name: Deploy to Dev Server
      command: |
        if [ "${CIRCLE_BRANCH}" == "master" ]; then rsync -arhvz --exclude .git/ -e "ssh -o StrictHostKeyChecking=no" --progress \
        ./ ubuntu@xxx.xxx.xxx.xxx:/var/www/xxx/xxx/; fi
        if [ "${CIRCLE_BRANCH}" == "master" ]; then ssh -o StrictHostKeyChecking=no ubuntu@xxx.xxx.xxx.xxx 'cd /var/www/xxx/xxx && pm2 restart all --update-env'; fi

I think this gets added to a file that isn’t sourced on each step due to a non-login shell. Can you try writing them to $BASH_ENV as explained here https://circleci.com/docs/2.0/env-vars/#using-bash_env-to-set-environment-variables and see if that helps?

Not working when I replace that code with this :

echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
echo "[ -s \"$NVM_DIR/nvm.sh\" ] && . \"$NVM_DIR/nvm.sh\"" >> $BASH_ENV

If I do that, then I get :

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
/bin/bash: line 6: nvm: command not found
/bin/bash: line 7: nvm: command not found
v6.1.0

I have a feeling my syntax is off. I also tried adding source $BASH_ENV which didn’t help

This seems to work for now :

Seems the alias doesn’t hold but nvm can still pull up the previously installed version.

1 Like

get we get admins to have a look at this, having to source nvm each time is crazy!

Solved with this post

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.