4

I'm trying to use jsdom in an AWS Lambda function, bundled with serverless-esbuild. However, I'm encountering the following error:

[WARNING] "./xhr-sync-worker.js" should be marked as external for use with "require.resolve" [require-resolve-not-external]

node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:31:57:
  31 │ const syncWorkerFile = require.resolve ? require.resolve("./xhr-sync-worker.js") : null;
     ╵                                                          ~~~~~~~~~~~~~~~~~~~~~~

✖ Error: Cannot find module './xhr-sync-worker.js'
  Require stack:

Here is my yml file:

service: service-name

custom:
  esbuild:
    bundle: true
    minify: false
    sourcemap: true
    target: 'node20'
    external:
      - './xhr-sync-worker.js'
    platform: 'node'
    concurrency: 10

provider:
  name: aws
  runtime: nodejs20.x

plugins:
  - serverless-esbuild
  - serverless-offline

functions:
  getDailyPrices:
    handler: src/functions/getDailyPrices/handler.function

I've tried marking xhr-sync-worker.js as an external module, but the issue persists. How can I resolve this error and properly bundle jsdom for use in AWS Lambda with serverless-esbuild?

2 Answers 2

0

If someone is still facing this, add to the esbuild config:

  exclude:
      - 'jsdom'
      - 'canvas'
  external:
    - 'jsdom'
    - 'canvas'

It needs to be both to work! Workaround discussed here:

https://github.com/evanw/esbuild/issues/1311

Sign up to request clarification or add additional context in comments.

Comments

-1

I was facing the same issue.

I am using cdk and I needed to add this to NodeJSFunction

handler = new lambdaNode.NodejsFunction(stack, `${HANDLER_ID}${handlerId}`, {
  ...,
  bundling: {
      externalModules: ['canvas'],
      nodeModules: ['jsdom'],
    },
})

you would need to translate this into Serverless.

Maybe this, BUT UNTESTED!!

custom:
  esbuild:
    ...
    external:                # Exclude specific modules from being bundled
      - 'canvas'             # Exclude 'canvas' just like in your TypeScript config
    nodeModules:             # Include specific modules in the bundle
      - 'jsdom'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.