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?
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.
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?
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.