Need Assistance Compiling Dgraph Lambda Server With Webpack

What I want to do

I want to use an external module, e.g. lodash as a module in my dgraph-lambda code as brielfly described here: GitHub - dgraph-io/dgraph-lambda

This is experimental since I want to use a number of external modules. Lodash seemed easy to experiment with.

I use a webpack config to compile lodash and then bind mount that in my docker-compose file

What I did

My folder structure is like

├── dist
│ ├── script.js
│ └── script.js.LICENSE.txt
├── docker-compose.yml
├── lambda
│ └── script.js
├── package-lock.json
├── package.json
└── webpack.config.js

My Webpack config looks like this:

 const path = require('path')
 
 module.exports  = {
    target: "webworker",
    context: path.join(__dirname, "lambda"),
    entry: {serverEntry: ["./script.js"]},
    mode:"production",
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'script.js',
        libraryTarget: 'commonjs2'
    },
    resolveLoader: {
        modules: [
            path.join(__dirname, 'node_modules')
        ]
    },
    resolve: {
        modules: [
            path.join(__dirname, 'node_modules')
        ],
        fallback: {
            "fs": false,
            "path": false

        },
    }
}

The code compiles, but when the dgraph-lambda container runs I get the following error:

ReferenceError: module is not defined
    at evalmachine.<anonymous>:2:70674
    at evalmachine.<anonymous>:2:70693
    at Script.runInContext (vm.js:143:18)
    at Object.evaluateScript (/app/src/evaluate-script.ts:90:10)
    at Object.scriptToExpress (/app/src/script-to-express.ts:16:18)
    at startServer (/app/src/index.ts:24:15)

This means that any request to a defined type such as “Example” as seen below will evaluate fine for fullName but will not with the “name” field which is evaluated by the lambda.

type Example {
name String! @lambda
fullName String!

}

I know this is quite obscure, but I’m not sure how to move forward. since there aren’t any instructions on the github.

Dgraph metadata

dgraph version Dgraph version :v20.11.3
1 Like

Solved it myself!

Simply remove: libraryTarget: 'commonjs2' so webpack (v5) uses the default and it will compile and work fine!