cli: Issue installing `netlify-cli` with resulting EACCES permission denied error

Describe the bug

For some reason, when installing the netlify-cli we ran into an EACCES denied error to the autocompletion.json file. It seems to pass through when we use sudo in front of installing but this is even from a fresh install of nvm. And invoking the cli with sudo seems to work fine, but any other way fails.

To Reproduce

Steps to reproduce the behavior:

  1. Run npm install netlify-cli -g
  2. Error outputs

Configuration

This is at the global install level even outside of a given project.

Here is the output of npx envinfo --system --binaries --npmPackages netlify-cli --npmGlobalPackages netlify-cli:

System:
    OS: macOS 12.1
    CPU: (8) arm64 Apple M1
    Memory: 1.24 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    npm: 8.3.0 - ~/.nvm/versions/node/v16.13.1/bin/npm

Expected behavior

It should properly install without any error output. The CLI should be usable without sudo in front.

CLI Output

Here is the error output we’re seeing:

EACCES error

francisagulto@franciss-MBP recipes % npm install -g netlify-cli 
npm WARN deprecated flatten@1.0.3: flatten is deprecated in favor of utility frameworks such as lodash.
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated rollup-plugin-inject@3.0.2: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject.
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated node-pre-gyp@0.13.0: Please upgrade to @mapbox/node-pre-gyp: the non-scoped node-pre-gyp package is deprecated and only the @mapbox scoped package will recieve updates in the future
npm ERR! code 7
npm ERR! path /Users/user_name/.nvm/versions/node/v16.13.1/lib/node_modules/netlify-cli
npm ERR! command failed
npm ERR! command sh -c node ./scripts/postinstall.js
npm ERR! /Users/user_name/.nvm/versions/node/v16.13.1/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:116
npm ERR!       throw ex;
npm ERR!       ^
npm ERR!
npm ERR! Error: EACCES: permission denied, open '/Users/user_name/Library/Preferences/netlify/autocompletion.json'
npm ERR!     at Object.openSync (node:fs:585:3)
npm ERR!     at writeFileSync (node:fs:2153:35)
npm ERR!     at createAutocompletion (/Users/user_name/.nvm/versions/node/v16.13.1/lib/node_modules/netlify-cli/src/lib/completion/generate-autocompletion.js:33:3)
npm ERR!     at postInstall (/Users/user_name/.nvm/versions/node/v16.13.1/lib/node_modules/netlify-cli/scripts/postinstall.js:36:5)
npm ERR!     at Object.<anonymous> (/Users/user_name/.nvm/versions/node/v16.13.1/lib/node_modules/netlify-cli/scripts/postinstall.js:56:1)
npm ERR!     at Module._compile (node:internal/modules/cjs/loader:1101:14)
npm ERR!     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
npm ERR!     at Module.load (node:internal/modules/cjs/loader:981:32)
npm ERR!     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
npm ERR!     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
npm ERR!   errno: -13,
npm ERR!   syscall: 'open',
npm ERR!   code: 'EACCES',
npm ERR!   path: '/Users/user_name/Library/Preferences/netlify/autocompletion.json'
npm ERR! }

npm ERR! A complete log of this run can be found in: npm ERR! /Users/user_name/.npm/_logs/2021-12-17T22_38_12_208Z-debug-0.log

Additional context

This was on a fresh M1 MacBook installation. This was on a fresh nvm installation as well. The correct path for node and npm seemed to be fine as well.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 31 (2 by maintainers)

Most upvoted comments

We use https://github.com/sindresorhus/env-paths to get the recommended user specific directory: https://github.com/netlify/cli/blob/5ba83d8f4446d0d17ac993e4b85e8317e73db664/src/lib/settings.js#L26

I was able to reproduce this issue by running:

  1. On a fresh install (with no prior Library/Preferences/netlify directory) run npm install -g netlify-cli
  2. Run sudo ntl status to make sure the config.json is created (it will be created as root).
  3. Run ntl status and see that it fails with the following error:
You don't have access to this file.

This is basically this issue https://github.com/yeoman/configstore/issues/34#issuecomment-171968299, where if you ran the CLI using sudo once, it can overwrite the permissions.

You can find what Configstore uses here https://github.com/yeoman/configstore/blob/02f07ead1cbbc1c8b49e3c9ba6e7a4896594368d/index.js#L12

Not sure if this was solved by the new version of the library here: https://github.com/sindresorhus/conf#configfilemode

@Fran-A-Dev a possible workaround is to delete the directory and let the CLI regenerate the files via:

  1. sudo rm -rf /Users/francisagulto/Library/Preferences/netlify/
  2. npm install -g netlify-cli
  3. ntl login

Going forward I believe the fix is to ensure we don’t generate config.json under root, but as the current underprivileged user.

@lukasholzer and @Fran-A-Dev please let me know what you think.

Hello! I just wanted to share a solution I came up with in the permission error as well. So what I did was headed to the ~/Library/Preferences/netlify folder and opened the “get info” option. Then I manually added my user in the “Sharing and Permission” section, together with read & write beside my user. After that, reload the terminal and all’s good to go!

@lukasholzer @erezrokah Perfect! Success! It worked, it logged me into my Netlify dashboard. Thanks so much for the help! Screen Shot 2022-01-21 at 7 19 25 AM