Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable nvm in steps in circleci 2.0?

Tags:

npm

nvm

circleci

Here are my steps in my

steps:
  -run:
      name: Setup nvm and npm
      command: |
        wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
        export NVM_DIR=$HOME/.nvm
        source $NVM_DIR/nvm.sh
        nvm install 8.9 && nvm alias default 8.9
   -run: npm install && npm run lint && npm test

The second step always fails with this error message

/bin/bash: npm: command not found

I checked .bashrc and I can see the following lines are added to the end of the file

export NVM_DIR="$HOME/.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

Circleci 2.0 invokes the step command by starting a new shell with #!/bin/bash -eo pipefail

If I starts a docker (docker run -i -t buildpack-deps:xenial) and apply the first step, and then start a new shell via #!/bin/bash -eo pipefail, I can see npm is available on the path

I am using docker for this project

version: 2
jobs:
  test_main:
    docker:
      - image: buildpack-deps:xenial

So why does it fail in circleci 2.0 environment? How can I ensure npm will be available to step 2 from step 1?

I have tried to add [ -s "$HOME/.bashrc" ] && \. "$HOME/.bashrc" to ~/.bash_profile (in case .bashrc is not executed due to the non-interactive/non-login shell)

To reproduce the issue you can run circleci build with this .circleci/config.yml file

version: 2
jobs:
  build:
    docker:
      - image: buildpack-deps:xenial
    steps:
      - run:
          name: Setup nvm and npm
          command: |
            wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
            # Activate nvm
            export NVM_DIR=$HOME/.nvm
            touch $HOME/.nvmrc
            source $NVM_DIR/nvm.sh
            # Use node 8.9
            nvm install 8.9 && nvm alias default 8.9
            echo 8.9 > $HOME/.nvmrc
            # Enable nvm in following steps
            echo '[ -s "$HOME/.bashrc" ] && \. "$HOME/.bashrc"' >> $HOME/.bash_profile
            # To fix npm install : "node-pre-gyp: Permission denied"
            npm config set user 0
            npm config set unsafe-perm true
            npm install -g npx webpack webpack-cli jest
            node --version
            npm --version
      - run: npm install

You will see the following error message:

====>> npm install
  #!/bin/bash -eo pipefail
npm install
/bin/bash: npm: command not found
Error: Exited with code 127
Step failed
Task failed
like image 319
Anthony Kong Avatar asked Dec 24 '22 09:12

Anthony Kong


1 Answers

The problem lies with these lines:

        # Enable nvm in following steps
        echo '[ -s "$HOME/.bashrc" ] && \. "$HOME/.bashrc"' >> $HOME/.bash_profile

I was hoping to source .bashrc from .bash_profile. However since the shell of circleci is non-interactive, the environment variable PS1 is blank. Hence .bashrc basically quits immediately once it is sourced, because of this line in .bashrc

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

I have to put the following lines directly in the file specified by $BASH_ENV

echo 'export NVM_DIR=$HOME/.nvm' >> $BASH_ENV
echo 'source $NVM_DIR/nvm.sh' >> $BASH_ENV
like image 119
Anthony Kong Avatar answered Dec 27 '22 21:12

Anthony Kong