Search code examples
node.jspackage.jsonserverlessserverless-offlineconcurrently

npm run multiple services including serverless offline from different directories


Currently

  1. I have a local test environment setup.
  2. I am trying to migrate to serverless and have setup serverless-offline for local testing.

I cannot get my existing client + server to run, alongside serverless-offline.

DIRECTORY:

root:
- package.json
- /my-project
-- /serverless
--- ...
-- handler.js
-- serverless.yml

PACKAGE.JSON <-- uses concurrently to run multiple services simultaneously

"scripts": {
    "start": "concurrently \"npm start --prefix client\" \"node index.js\" \"serverless offline start\""
  }

Problem

When running npm start at root directory, the following error is returned:

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

Note: when I run serverless offline start in \my-project, the command executes successfully and serverless-offline runs.

Question

  1. Is there a way to run the serverless offline start command in the context of /my-project or
  2. Do I need to reorganise my directory structure?

Notes

I have tried answer run npm script from different repository but failed to get it working. Perhaps because I am trying to run a command (i.e. serverless) not a script file from a different directory context.


Solution

  • The issue had nothing to do with serverless, but rather my novice experience with setting up scripts within package.json. There were 2 options to run serverless offline start from the parent project folder.

    Option 1

    Inserting cd my-project && before invoking serverless offline start

    "scripts": {
        "start": "concurrently \"npm start --prefix client\" \"node index.js\" \"cd my-project && serverless offline start\""
      }
    

    Option 2

    Adding --prefix my-project to serverless offline start command. Note: I have not tested option 2

    "scripts": {
        "start": "concurrently \"npm start --prefix client\" \"node index.js\" \"serverless offline start --prefix my-project\""
      }