Following Github Actions tutorial but getting an npm EACCES error

I’m following the tutorial here: Introduction to GitHub Actions - GitHub Docs

I’ve followed the instructions exactly, including copy-pasting the example code, but when I push and Github Actions runs it produces the following error:

Run npm install -g bats
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2021-07-01T15_33_20_179Z-debug.log
Error: Process completed with exit code 243.

You are correct, the instructions are flawed.

Reported at:

To learn one of many ways to fix this, you could read:

I’m sorry you hit a document that gave incorrect instructions.

1 Like

Thank you for reporting the problem.

I attempted to implement the suggested fix, .github/workflows/learn-github-actions.yml now looks like this:

name: learn-github-actions
on: [push]
jobs:
  check-bats-version:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
      - run: mkdir ~/.npm-global
      - run: npm config set prefix '~/.npm-global'
      - run: echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.profile
      - run: source ~/.profile
      - run: npm install -g bats
      - run: bats -v

However now I get a different error:

Run bats -v
/home/runner/work/_temp/d705b0bc-8901-4f83-8cef-8b2c049df00e.sh: line 1: bats: command not found
Error: Process completed with exit code 127.

Problem

Each run statement almost certainly gets its own environment.

The one where it mattered the most was source ~/.profile, where you really needed it to last to the next step (npm install ...).

Recommendation

I think you want this (untested):

name: learn-github-actions
on: [push]
jobs:
  check-bats-version:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
      - run: |
           mkdir ~/.npm-global
           npm config set prefix '~/.npm-global'
           echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.profile
           source ~/.profile
           npm install -g bats
           bats -v

For reference, I picked the | based on:
https://yaml-multiline.info/
I pretty much always consult this webpage…

Note: technically yo don’t need to use ~/.profile here, you could just do:

name: learn-github-actions
on: [push]
jobs:
  check-bats-version:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
      - run: |
           mkdir ~/.npm-global
           npm config set prefix '~/.npm-global'
           export PATH=~/.npm-global/bin:$PATH
           npm install -g bats
           bats -v

Alternative

FWIW, there is an alternative,

https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
https://docs.github.com/en/actions/reference/environment-variables

However, in reading that pair of pages in context w/ a goal here, I expect that the average reader will be mislead. So, I guess I get to file another bug.

2 Likes

Thanks Jsoref:

Confirming this solution worked for me:

name: learn-github-actions
on: [push]
jobs:
  check-bats-version:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
      - run: |
           mkdir ~/.npm-global
           npm config set prefix '~/.npm-global'
           export PATH=~/.npm-global/bin:$PATH
           npm install -g bats
           bats -v

Output from GitHub Job:

/home/runner/.npm-global/bin/bats -> /home/runner/.npm-global/lib/node_modules/bats/bin/bats
+ bats@1.3.0
added 1 package from 1 contributor in 0.531s
Bats 1.3.0

Best~ lar-mo

2 Likes