sorenandersen.com

Install the Serverless Framework CLI per project, not globally

August 08, 2020

Most examples - and even the official “getting started” instructions - suggest that you install and run the Serverless Framework CLI globally on your system.

However, global package dependency may in the future cause issues between projects that depend on incompatible versions, especially when used by build- and deploy steps in your CI pipeline.

For this reason we should install the Serverless Framework CLI as a dev dependency within each project:

$ npm install serverless --save-dev

And then add an sls script to package.json:

"scripts": {
  "sls": "serverless"
},

Now we can run serverless commands using npm run sls [-- <args>], where -- delimits options for npm run and pass remaining arguments to the sls (and, in turn, serverless) script.

Examples:

$ npm run sls -- deploy
$ npm run sls -- logs -f functionName

Alternative for global install: Configure framework version range

If relying on a globally installed CLI then don’t forget to specify the framework version range in serverless.yml.

frameworkVersion: '>=1.0.0 <2.0.0'

References

I learned this technique attending Yan Cui’s “Production-Ready Serverless” workshop. I really recommend this workshop for anyone wanting to up their AWS serverlesss skills.


Søren Andersen

Digital garden of Søren Andersen.
Posts on tech that I use and learn.