16

I am running my nodejs code and also installed serverless(npm i -g serverless) but while running it with the command sls offline start --CacheInvalidations I am getting error as:-

Serverless Error ---------------------------------------

Serverless command "offline" not found. Did you mean "config"? Run "serverless help" for a list of all available commands.

Get Support --------------------------------------------

 Docs:          docs.serverless.com
 Bugs:          github.com/serverless/serverless/issues
 Issues:        forum.serverless.com

Your Environment Information ---------------------------

 Operating System:          linux
 Node Version:              12.18.2
 Framework Version:         1.79.0
 Plugin Version:            3.7.1
 SDK Version:               2.3.1
 Components Version:        2.34.6
Mohammad Moallemi
  • 628
  • 1
  • 5
  • 13
Nikhil Srivastava
  • 181
  • 1
  • 1
  • 6
  • 1
    In my case, I had the module correctly under node_modules and still it was not working. I found the issue that I was executing the command >sls offline start from the base application folder, but you have to execute the command from the SERVICE_FOLDER. Hope this might help somebody.... – Sudheer Kumar Mar 17 '21 at 17:08
  • in my case i had to run `sudo npm install serverless -g` and worked like a charm – Sami Ullah Dec 14 '21 at 13:59

9 Answers9

18

You need to install the serverless-offline plugin using npm in order to use sls offline command.

Just simply run:

npm i -g serverless-offline

to install globally on your device or

npm i serverless-offline --save-dev

to install it as a development dependency in the active project. and then add this configuration to your serverless template:

plugins:
  - serverless-offline

For more information on serverless-offline plugin take a look at serverless official documents:

Serverless Offline | Emulate AWS λ and API Gateway locally when developing your Serverless project

Serverless Offline NPM

Mohammad Moallemi
  • 628
  • 1
  • 5
  • 13
  • make sure you hit save on your serverless template, I'm used to work with auto saving IDE, so when I switched to something else I forgot to save after adding the plugin – yonadav bar ilan Feb 04 '21 at 15:25
  • 3
    ok, one more important thing is that `serverless.yaml` has to be in the same folder where the `node_modules` folder is – Matteo Apr 28 '21 at 01:04
  • @Madeo but serverless generally while creating a project it's going to create folder right, can we point out the plugin to refer one folder up is that possible ? – balajivaishnav Apr 28 '21 at 07:09
4

I was facing the same issue while setting up serverless.yml with nodejs and running it on local. Two steps resolved the problem.

  1. Install serverless-offline package globally.

npm i -g serverless-offline

  1. Add the same package under the plugin key of your serverless.yml file.

plugins:
- serverless-offline

2

Installing the dependency with yarn you can run the command typing the following:

Install:

yarn add serverless-offline -D

Run:

yarn serverless offline start
rigobcastro
  • 1,258
  • 11
  • 18
1

There is another way to try with

 npx serverless plugin install --name serverless-offline

this is for install serverless-offline plugin. you also can view available plugin names bu

npx sls plugin list 
KhangCao
  • 11
  • 2
0

You have to install the package (or locally in your project or globally). I do recommend install globally.

npm i -g serverless-offline

or

yarn global add serverless-offline

Inside your serverless.yml file, add in the plugins session the following code:

plugins:

  • serverless-offline

It will sove your problem

0

Not sure if you solved this issue but I was having the same problem, for me it was a silly error, the indentation of the YML file was wrong, after fixing the indentation it started to work just fine

Roberto Guajardo
  • 490
  • 1
  • 4
  • 15
0

First, you have to install serverless offline globally.

npm i -g serverless-offline

Next, you should check a serverless.yml file. Otherwise, you must create a serverless.yml file.

service: your-service-name
app: app-name
provider:
  name: aws
  runtime: nodejs10.x
  timeout: 60
  memorySize: 128
  deploymentBucket: bucket-name
# you can overwrite defaults here
  stage: prod
  region: your-aws-region
functions:
  your-function-name:
    handler: handler.dispatch
    memorySize: 128
    timeout: 60
    events:
      #- http: POST /hello
      - http: 'ANY {proxy+}'
plugins:
  - serverless-offline
  - serverless-aws-alias
0
  1. make sure serverless-offline is installed (globaly or at the project)
  2. Add these lines to serverless.yml

plugins:
  # Needed to run & debug locally
  - serverless-offline
Elad Rubi
  • 593
  • 4
  • 7
0

Try in your project with the npx prefix. So npx sls offline or npx serverless offline. It worked for me.