Workers + Webpack Issue

Hi,

i’m starting to explore the Workers service to check if its feasible for some of my future projects.
So far i’ve followed the “basic” examples and seems to be going ok.

I’m exploring a possible worker to connect with Sendgrid, using the “@sendgrid/mail” node package.
I’m trying with webpack to add this to the worker (and also using wrangler) but i’ve always get some errors,

Below is my webpack.config.js

const webpack = require("webpack")
module.exports = {
  target: "webworker",
  entry: "./index.js",
  mode: "development",
  plugins: [
    new webpack.ProvidePlugin({
      _sgMail: "@sendgrid/mail",
    }),
  ]
}

Below is my redacted index.js

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})
async function handleRequest(req) {
  const sgMail = _sgMail;
  let response
  if (req.method === "POST") {    
    sgMail.setApiKey(".....");
    const msg = {
        ....
      }
    }
    sgMail
      .send(msg).then(() => { response = new Response("OK", { status: 200 }) })
      .catch((error) => { response = new Response("NOK", { status: 200 }) })
  } else {
    response = new Response("Expected POST", { status: 500 })
  }  
}

I’m using wrangler 1.19.5 and webpack 4.42 (also tried with webpack5 but got errors)

Can someone point me into the right direction ?

Regards

What errors are you getting? Could you provide a screenshot of the error (with sensitive information redacted)

Hi,

below is the message i get after wrangler dev cli command

 ./node_modules/@sendgrid/helpers/classes/attachment.js
Module not found: Error: Can't resolve 'fs' in 'C:\www\workers.demo\node_modules\@sendgrid\helpers\classes'
resolve 'fs' in 'C:\www\workers.demo\node_modules\@sendgrid\helpers\classes'
  Parsed request is a module
  using description file: C:\www\workers.demo\node_modules\@sendgrid\helpers\package.json (relative path: ./classes)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      C:\www\workers.demo\node_modules\@sendgrid\helpers\classes\node_modules doesn't exist or is not a directory
      C:\www\workers.demo\node_modules\@sendgrid\helpers\node_modules doesn't exist or is not a directory
      C:\www\workers.demo\node_modules\@sendgrid\node_modules doesn't exist or is not a directory
      C:\www\workers.demo\node_modules\node_modules doesn't exist or is not a directory
      C:\www\node_modules doesn't exist or is not a directory
      C:\node_modules doesn't exist or is not a directory
      C:\Program Files\node_modules doesn't exist or is not a directory
      C:\node_modules doesn't exist or is not a directory
      looking for modules in C:\www\workers.demo\node_modules
        using description file: C:\www\workers.demo\package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: C:\www\workers.demo\package.json (relative path: ./node_modules/fs)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              C:\www\workers.demo\node_modules\fs doesn't exist
            .wasm
              Field 'browser' doesn't contain a valid alias configuration
              C:\www\workers.demo\node_modules\fs.wasm doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              C:\www\workers.demo\node_modules\fs.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              C:\www\workers.demo\node_modules\fs.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              C:\www\workers.demo\node_modules\fs.json doesn't exist
            as directory
              C:\www\workers.demo\node_modules\fs doesn't exist
[C:\www\workers.demo\node_modules\@sendgrid\helpers\classes\node_modules]
[C:\www\workers.demo\node_modules\@sendgrid\helpers\node_modules]
[C:\www\workers.demo\node_modules\@sendgrid\node_modules]
[C:\www\workers.demo\node_modules\node_modules]
[C:\www\node_modules]
[C:\node_modules]
[C:\Program Files\node_modules]
[C:\node_modules]
[C:\www\workers.demo\node_modules\fs]
[C:\www\workers.demo\node_modules\fs.wasm]
[C:\www\workers.demo\node_modules\fs.mjs]
[C:\www\workers.demo\node_modules\fs.js]
[C:\www\workers.demo\node_modules\fs.json]
 @ ./node_modules/@sendgrid/helpers/classes/attachment.js 9:11-24
 @ ./node_modules/@sendgrid/helpers/classes/index.js
 @ ./node_modules/@sendgrid/helpers/index.js
 @ ./node_modules/@sendgrid/mail/src/classes/mail-service.js
 @ ./node_modules/@sendgrid/mail/index.js
 @ ./index.js
Error: webpack returned an error. Try configuring `entry` in your webpack config relative to the current working directory, or setting `context = __dirname` in your webpack config.

Regards

Hi,

can anyone help me on this matter?

Regards

The SendGrid package is trying to use the Node API fs. Workers don’t run NodeJS therefore that does not exist. I’d recommend just doing a fetch call to the API yourself.

1 Like

Hi,

thanks…but cant i use the nodejs sendgrid package ?

Regards

You cannot no since it relies on NodeJS APIs. Doing the requests yourself is what I’d recommend but there may be another package out there too.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.