diff --git a/README.md b/README.md
index 3b9241053..239506156 100644
--- a/README.md
+++ b/README.md
@@ -243,6 +243,11 @@ Default: 600 (10 minutes)
WebSocket port to listen on.
Default: 3001
+#### preLoadModules
+
+Pre-load specified modules in the main thread to avoid crashes when importing in worker threads. Provide module names as a comma-separated list (e.g., "sharp,canvas").
+Default: ''
+
Any of the CLI options can be added to your `serverless.yml`. For example:
```yml
diff --git a/src/ServerlessOffline.js b/src/ServerlessOffline.js
index eeeb98413..57cf6d69a 100644
--- a/src/ServerlessOffline.js
+++ b/src/ServerlessOffline.js
@@ -65,6 +65,8 @@ export default class ServerlessOffline {
async start() {
this.#mergeOptions()
+ this.#preLoadModules()
+
if (this.#cliOptions.noSponsor) {
log.notice()
} else {
@@ -423,6 +425,18 @@ export default class ServerlessOffline {
}
}
+ #preLoadModules() {
+ const modules = this.#options.preLoadModules.split(",")
+
+ modules.forEach((module) => {
+ try {
+ import(module)
+ } catch (error) {
+ log.error(`Error importing module ${module}: ${error}`)
+ }
+ })
+ }
+
// TODO FIXME
// TEMP quick fix to expose for testing, look for better solution
internals() {
@@ -446,6 +460,10 @@ export default class ServerlessOffline {
mergeOptions: () => {
this.#mergeOptions()
},
+
+ preLoadModules: () => {
+ this.#preLoadModules()
+ },
}
}
}
diff --git a/src/config/commandOptions.js b/src/config/commandOptions.js
index 5c996b9ef..8dd917575 100644
--- a/src/config/commandOptions.js
+++ b/src/config/commandOptions.js
@@ -139,4 +139,8 @@ export default {
type: "string",
usage: "Websocket port to listen on. Default: 3001.",
},
+ preLoadModules: {
+ type: "string",
+ usage: "A comma separated list of modules to preload on the main thread",
+ },
}
diff --git a/src/config/defaultOptions.js b/src/config/defaultOptions.js
index 9159204c6..5d6c31c6f 100644
--- a/src/config/defaultOptions.js
+++ b/src/config/defaultOptions.js
@@ -28,4 +28,5 @@ export default {
webSocketHardTimeout: 7200,
webSocketIdleTimeout: 600,
websocketPort: 3001,
+ preLoadModules: '',
}