[Functions] ESBuild warning when using mongoose module

I use serverless functions with Typescript. Since I’ve updated netlify cli to the latest, my serverless function is outputing the following warnings during build:

◈ Function builder zip-it-and-ship-it detected.
◈ Building functions from directory my_functions
 > node_modules/mongoose/lib/index.js:11:28: warning: This call to "require" will not be bundled because the argument is not a string literal (surround with a try/catch to silence this warning)
    11 │     require('./driver').set(require(global.MONGOOSE_DRIVER_PATH));
       ╵                             ~~~~~~~

 > node_modules/mongodb/lib/core/index.js:4:53: warning: Indirect calls to "require" will not be bundled (surround with a try/catch to silence this warning)
    4 │ const require_optional = require('optional-require')(require);

And when I call the function I get:

(node:31480) [MONGODB DRIVER] Warning: Warning: no saslprep library specified. Passwords will not be sanitized
TypeError: Cannot read property 'connection' of undefined

So everything is not working anymore.

It seems to me it is a problem with the new esbuild

That doesn’t allow this type of imports.

My questions are:

  • Is it possible to build typescript serverless functions without esbuild?
  • Are there any known workarounds for this situation? I believe mongoose it is a pretty common library, so maybe someone know how to fix this?

Thank you

Unfortunately, no. Typescript functions are bundled with esbuild.

You could use a different bundler like Webpack which supports this and generate the required JavaScript. The resultant file should have the same name and location as the .ts file. Get started with functions | Netlify Docs

Thank you @hrishikesh Even though when I use ESBuild outside Netlify it actually compile.

I have the Warning

[MONGODB DRIVER] Warning: Warning: no saslprep...

when I call my function but I don’t have

warning: This call to "require" will not be bundled ...

when I build with ESBuild.

Is it possible to know which version of ESBuild Netlify is using and with what configuration? I would like to reproduce the error/warning outside Netlify.

Hi @ndr4,

We’re using a fork of esbuild, the repo is here: esbuild/NETLIFY.md at netlify · netlify/esbuild · GitHub. You’re getting the warning because the warning existed in older versions of esbuild.

Netlify CLI will use a more recent version of esbuild, so you could see the new warning in that.

However, I would not recommend taking this further this as it’s not solving the real problem at hand, it’s just showing you the location of warning.

But if you have any more questions, let us know.

Thank you @hrishikesh. Actually now that I compiled the Typescript funcitons with the typescript compiler and pointed Netlify function folder to this new files, I still get the warnings.

Can it be the library zip-it-and-ship-it that uses esbuild? In the README.md they are saying the default bundle is zisi not esbuild, though.

When you were saying to use a different bundler you were saying to use something like serverless-webpack? That will generate a package for aws lambda? Or the typescript compiler should be enough?

Thank you again.

Yes, but I’m not 100% sure if that’s valid for Typescript. According to the PR here: feat: make esbuild the default bundler for TypeScript functions by eduardoboucas · Pull Request #370 · netlify/zip-it-and-ship-it · GitHub, it should be esbuild.

I haven’t tried serverless webpack myself and not sure how exactly it differs from the normal webpack, but basically yes. I was saying about using a different bundler like webpack which can compile and bundle your JS in a single file. This would then be used instead of the Typescript file.