Hello guys, I have a problem with JSDOM library. W...
# help
g
Hello guys, I have a problem with JSDOM library. When I run
npx sst start
I get this error:
Copy code
==========================
 Starting Live Lambda Dev
==========================

Transpiling Lambda code...
 > addons/foo/node_modules/jsdom/lib/jsdom/utils.js:160:25: error: Could not resolve "canvas" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
    160 │   const Canvas = require("canvas");
I’ve been marked
canvas
as external, in this way:
Copy code
const topic = new sst.Topic(stack, 'mail-receipt', {
      subscribers: ['addons/foo/index.foo'],
      snsTopic: {
        topicName: 'mail-receipt'
      },
      defaultFunctionProps: {
        bundle: {
          externalModules: ['canvas']
        }
      }
    });
After this change I don’t get any error when I run
sst deploy
but I still have the same problem “Could not resolve “canvas”… ” when run
sst start
Is there any difference on esbuild process between deploy/start? Any suggestion to solve it?
r
You may need to add it to nodeModules rather than externalModules if it's something your code relies on
g
thanks @Ross Coundon, I was reading now on JSDOM doc: Since jsdom v13, version 2.x of 
canvas
 is required; version 1.x is no longer supported. I tried to install many times canvas but I get this error:
Copy code
==========================
 Starting Live Lambda Dev
==========================

Transpiling Lambda code...
 > addons/foo/node_modules/canvas/lib/bindings.js:3:25: error: No loader is configured for ".node" files: addons/turo-sync/node_modules/canvas/build/Release/canvas.node
    3 │ module.exports = require('../build/Release/canvas.node');
Should I load canvas.node thorough bundle settings? I’m using monorepo with yarn.
f
As for the
No loader is configured
error, try configuring the loader inside bundle like this https://docs.serverless-stack.com/constructs/Function#configure-bundling
As for the original
Could not resolve "canvas"
error, do you have
canvas
npm installed inside ur nodeModules?
g
thanks @Frank I solve it as described in your link
Copy code
const topic = new sst.Topic(stack, 'mail-receipt', {
      subscribers: ['addons/foo/src/index.emailReceiptTrigger'],
      snsTopic: {
        topicName: 'mail-receipt'
      },
      defaultFunctionProps: {
        bundle: {
          externalModules: ['canvas'],
          loader: {
            ".node": "dataurl",
          },
        }
      }
    });
canvas wasn’t installed because I just update jsdom (after v13 canvas was required)
f
I see. I guess if you had
canvas
installed, that would’ve resolved the original error.