Error Cannot find module '//../cursor' - while trying to use mongo javascript driver

I have a series of services that I deploy on AWS Lamba Node using serverless framework using the webpack plugin for serverless. The code is written in Typescript.

One of the services uses mongo javascript client. This is the handler.ts code, pretty basic indeed


export const getDocs = (event: APIGatewayEvent, context: Context, callback: Callback) => {
    context.callbackWaitsForEmptyEventLoop = false;

    MongoClient.connect('mongoConnectionString', function(err, client) {
      if (err) {
        callback(err, null);
      }
      const db = client.db('mydb');
      db.collection('mycollection').find().toArray()
      .then(
        val => callback(null, JSON.stringify(val)
      );
    });
  
};

When I try to execute the service which uses the mongo driver I get an error. Looking at the log trace on Lambda I see the following message

Error: Cannot find module '//../cursor'

Other services that do not use Mongo actually work fine, and the service using mongo works also fine if it is invoked in a Node server, which makes me think that the problem is around how serverless and the rest of the components actually interact.

Here is the serverless.yml


provider:
  name: aws
  runtime: nodejs6.10



  hello12:
    handler: handler.hello12
    events:
      - http:
          path: hello12/get
          method: get


package:  
    individually: true
    
plugins:  
   - serverless-webpack
   - serverless-offline

The webpack.conifg.js file is pretty standard and is the same I have successfully used other times to deploy services which use mongo javascript client.


module.exports = {
  devtool: 'source-map',
  entry: slsw.lib.entries,
  resolve: {
    extensions: [
      '.js',
      '.json',
      '.ts',
      '.tsx'
    ]
  },
  output: {
    devtoolModuleFilenameTemplate: '[absolute-resource-path]',
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js'
  },
  target: 'node',
  module: {
    rules: [
      {
        test: /\.ts(x?)$/,
        use: [
          {
            loader: 'ts-loader'
          }
        ],
      }
    ]
  }
};

Also tsconfig.json seems pretty innocuous.

    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "outDir": "dist",
        "sourceMap": true,
        "allowJs": true,  
        "noUnusedLocals": true,  
        "noUnusedParameters": true   
    },
    "include": [
        "*.ts",
        "src/*.ts",
        "routes-express/*.ts",
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules"
    ],
    "typeRoots": [
        "node_modules/@types"
    ],
    "lib": [
        "es2016",
        "dom"
    ]
}

The versions of the various components are

  • serveless version 1.36.2
  • webpack version 4.29.0
  • serverless-webpack version 5.2.0
  • mongodb version 3.1.12
  • typescript version 2.7.2

To complete the scenario, if I remove the use of webpack I get the following error in the lambda trace

Unable to import module 'handler': Error at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.require (module.js:497:17) at require (internal/module.js:20:19)

Any suggestion on where to look at would be very much appreciated.