Linking Local Dependencies For Serverless Functions

I am having trouble linking dependencies for serverless functions. My project structure is replicated in [1]. I have a ‘simulate.js’ serverless function which calls ‘./pkg/webworker.cjs’ inside a webworker. The webworker in turn calls ‘sim_lib.js’ which executes ‘sim_lib_bg.wasm’. The dependencies of each file are shown in [2] along with netlify.toml.

[1 - Project File Structure]

[2 - File Dependencies + netlify.toml]

// 'netlify/functions/simulate.js'
import Worker from 'web-worker';
exports.handler = async function (event, context) {
    const worker = new Worker('./pkg/webworker.cjs');

// 'pkg/webworker.cjs'
let fs = require('fs');
let sim = require("./sim_lib.js");
const data = fs.readFileSync("./assets/data.csv");

// 'pkg/sim_lib.js' - generated by Rust wasm-pack
const path = require('path').join(__dirname, 'sim_lib_bg.wasm');
const bytes = require('fs').readFileSync(path);

// 'netlify.toml'
    node_bundler = "esbuild"
    external_node_modules = ["web-worker"]

  included_files = ["assets/**", "pkg/**"]

The project runs perfectly using netlify dev, but not when deployed. The ‘simulate’ serverless function log tells me it cannot find the ‘webworker.cjs’ module, as shown in [3].

[3 - Function ‘Simulate’ Log]

Error: Cannot find module '/var/task/pkg/webworker.cjs'
Require stack:
- /var/task/node_modules/web-worker/cjs/node.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at workerThread (/var/task/node_modules/web-worker/cjs/node.js:214:9)
at Object.<anonymous> (/var/task/node_modules/web-worker/cjs/node.js:79:56)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12) {
requireStack: [ '/var/task/node_modules/web-worker/cjs/node.js' ]

I am sure this is a simple dependency linking issue (I am a novice!), any help would be greatly appreciated.

What happens if you try to use:

  included_files = ["./assets/**", "./pkg/**"]

Your configuration should work too, but curious to see if this makes any difference.

If not, would you be able to share a repo?

1 Like

Thank you for suggesting a solution. Unfortunately that did not solve the issue. Netlify still throws the error:

Cannot find module ‘/var/task/pkg/webworker.cjs’

I have linked to the repository and website below. The naming of files in the repository is slightly different to what I used to describe the problem, but hopefully that’s not the issue.

I don’t see a folder named pkg in the repo you shared, and there’s no file named webworker.cjs either (at least not the one I can find). What am I missing?

1 Like

You spotted my mistake! A sneaky gitignore file in the pkg folder was preventing it uploading. You live and you learn! Thank you for your help.