Serverless + Cloudflare Workers + NPM Modules

Here is how I got NPM modules working with Cloudflare workers + Serverless.

  1. Install webpack
npm install --save-dev webpack
  1. Add a webpack config. webpack.config.js
module.exports = {
  target: 'webworker'
};
  1. Add build to your package.json
"build": "webpack yourFunctionFile.js",
  1. Update your serverless.yml.
functions:
  helloWorld:
    worker: hello
    script: dist/main  # This file is generated by webpack

Then you can use require inside of your function. :tada:

More info

  • Cloudflare workers - using NPM modules: https://developers.cloudflare.com/workers/writing-workers/using-npm-modules/