Nvm does not change Node version on machine

There are dozen of half solutions or automatically closed subject WITH NO definitive answers:

https://discuss.circleci.com/search?q=nvm

We need nvm to work for ALL commands not just while doing this:

      - run: 
          command: |
            export NVM_DIR="/opt/circleci/.nvm"
            [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
            nvm install 10
            node -v
            npm -v
            npm i -g npm@6.9.0
            npm -v

Right now any other step won’t get the correct node version!

I just spent a least 2 hours in trying different solutions… sshing in the machine is no good since nvm is loaded correctly when I ssh, that’s crazy… for such a simple thing :((

@drazisil I implore you to provide us with a solution I can’t just put

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

to all of my npm run commands!

Switch nodejs version on machine executor [solved] leads no where

That’s very strange you don’t have access. At any rate, I should be able to come up with a fix, give me a couple mins please.

is the nvm thing a known issue? why we can’t set nvm use 10 or nvm alias default 10 once?

Sorry, it’s driving me nuts :frowning:

It’s not a known issue that I’m aware of. I’ll run some tests in a bit to check, but let’s get you something working first.

Add the following as an initial step. This should add the command to the file that runs for every step.

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

I already tried, but I will retry

to be clear to run the above in a run block:

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

Yep. Running some tests on my own right now as well. Machine executor, correct?

Edit: Sorry, not run: and :command together.

yes machine, so nvm is found in other commands but:

steps:
      - checkout
      - run: 
          command: |
            echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
            echo ' [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> $BASH_ENV
      - run: nvm install 10
      - run: node -v
      - run: npm -v
      - run: npm i -g npm@6.9.0
      - run: npm -v
      - run: nvm alias default 10
      - run: nvm use 10
      - run: node -v
      - run: npm -v

at the last - run: node -v I still get the old one:

so in a run: or command: ??

Running some tests, please standby

1 Like

Ok, Thanks for your patience. The primary issue, I think, is the alias command has to mention v10, not 10. Nvm was throwing an error, but thanks to it returning 0 as an exit code, we mark the step green.

(To be clear, the step being marked success on a zero exit code is expected behavior)

nvm alias default v10 works. See the below build.

https://circleci.com/gh/drazisil/circleci-testing/513

Here is my full config for posterity in case I nuke the repo, and the above thread is hard to follow,

version: 2
jobs:
  build:
  build:
    machine: true
    steps:
      - checkout
      - run: |
            echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
            echo ' [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> $BASH_ENV
      - run: |
          node -v
      - run: |
          nvm install v10
          node -v
          nvm alias default v10
      - run: |
          node -v
2 Likes

thanks, trying now

1 Like

it seems that it is resolved, I got a failure on npm install but now it work… ok thanks marking as resolved!

2 Likes

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