Serverless + Cloudflare Workers + NPM Modules
Here is how I got NPM modules working with Cloudflare workers + Serverless.
- Install webpack
npm install --save-dev webpack
- Add a webpack config.
webpack.config.js
module.exports = {
target: 'webworker'
};
- Add
build
to yourpackage.json
"build": "webpack yourFunctionFile.js",
- 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/