2

I am trying to install serverless via npm (npm install -g serverless) but get the following error:

npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/local/lib/node_modules/serverless
npm ERR! dest /usr/local/lib/node_modules/.serverless-Ls3sFIzE
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/serverless' -> '/usr/local/lib/node_modules/.serverless-Ls3sFIzE'
npm ERR!  [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/serverless' -> '/usr/local/lib/node_modules/.serverless-Ls3sFIzE'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'rename',
npm ERR!   path: '/usr/local/lib/node_modules/serverless',
npm ERR!   dest: '/usr/local/lib/node_modules/.serverless-Ls3sFIzE'
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.

Does anyone know how to do this without having to switch the node versions you're using?

EDIT: When I run this with sudo I get the following output:

npm WARN deprecated request-promise-native@1.0.9: request-promise-native has been deprecated because it extends the now deprecated request package, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@3.3.2: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142

changed 672 packages, and audited 673 packages in 25s

50 packages are looking for funding
  run npm fund for details

found 0 vulnerabilities

However, when I try to run serverless invoke local --function f1 I get zsh: command not found: serverless - after I install with sudo what do I need to do so that the command is recognised?

Sabo Boz
  • 1,683
  • 4
  • 13
  • 29
  • Did you try to run again as admin in your OS? – Cássio Lacerda Jun 21 '21 at 03:26
  • see edit - it seemed to have worked (disregarding the deprecation warnings) but the command is still not recognised. – Sabo Boz Jun 21 '21 at 03:32
  • I don't know about serverless in Linux, but I believe this help you: https://stackoverflow.com/questions/45130789/serverless-command-not-found-in-ubuntu-16-04 – Cássio Lacerda Jun 21 '21 at 03:40
  • When I try to do ```npm i -g serverless``` I get the same result that ```npm install -g serverless``` had that I described in the question - ```npm config set prefix /usr/local``` did not seem to change anything and when I run ```npm config get prefix``` I get ```/usr/local``` – Sabo Boz Jun 21 '21 at 04:01

1 Answers1

5

Your permissions are goofed. Speaking generally, you don't want to install anything from npm with sudo.

You can sudo chown yourUser:yourGroup -R /usr/local/lib/node_modules. (If you're on macOS, your group is probably staff. You can find your group by running ls -l in your home directory and looking at the label adjacent to your username)

A simple alternative is a best practice anyway - install the serverless framework as a development dependency of your project:

npm i -D serverless

And then instead of running serverless deploy you can run npx serverless deploy.

Then as you check in the package.json file to version control, other collaborators will use the same version of the serverless framework.

Aaron Stuyvenberg
  • 3,437
  • 1
  • 9
  • 21
  • If you don't know your user name or group name you can run `id` in your command line (Mac). Alternatively, run `chown -R \`id -u\`:\`id -g\` FOLDER` and replace FOLDER with the required path to change permissions for that folder – JimmyTheCode Nov 08 '22 at 17:14
  • `sudo chown -R $USER FOLDER` should also work – Gibron Jun 27 '23 at 04:47