Create an empty serverless project for typescript: (after installing typescript, run serverless create --template aws-nodejs-typescript --path aws-serverless-typescript-api
Install the package
In the hello-world function created by the template, add the next code (with the required import): validate('US', '12345')
Run the function (serverless offline)
It will throw with the error: Cannot find module '/Users/e10117069/Code/MS/address-management-purchase-api/.esbuild/.build/src/functions/helloWorld/formats/US.json'\nRequire stack:\n- /Users/e10117069/Code/MS/address-management-purchase-api/.esbuild/.build/src/functions/v1/helloWorld/handler.js\n- /Users/e10117069/Code/MS/address-management-purchase-api/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js
Problem
As serverless builds the required dependencies in just on output file and it tries to obtain the json files from the root folder of the output.
Workaround 1
Add and configure webpack to add the json files to the root folder of the function.
Steps to reproduce
serverless create --template aws-nodejs-typescript --path aws-serverless-typescript-api
validate('US', '12345')
Cannot find module '/Users/e10117069/Code/MS/address-management-purchase-api/.esbuild/.build/src/functions/helloWorld/formats/US.json'\nRequire stack:\n- /Users/e10117069/Code/MS/address-management-purchase-api/.esbuild/.build/src/functions/v1/helloWorld/handler.js\n- /Users/e10117069/Code/MS/address-management-purchase-api/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js
Problem
As serverless builds the required dependencies in just on output file and it tries to obtain the json files from the root folder of the output.
Workaround 1
Possible solution
Allowing to force using the format web (that adds the files with require, so they are included in the bundle). Added pull request: https://github.com/Cimpress-MCP/postal-codes-js/pull/78