Serverless 4 esbuild is not packaging. I switches to the serverless 4 integrated esbuilt as webpack is not ESM compatible.
The common package can be recognized locally but is not packages when deploying (Github Actions), so that imports will fail.
"errorType": "Error",
"errorMessage": "Cannot find package '@cr/common' imported from /var/task/src/migrationService/migrationService.js",
"trace": [
"Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@cr/common' imported from /var/task/src/migrationService/migrationService.js",
" at packageResolve (node:internal/modules/esm/resolve:858:9)",
" at moduleResolve (node:internal/modules/esm/resolve:931:18)",
" at moduleResolveWithNodePath (node:internal/modules/esm/resolve:1173:14)",
" at defaultResolve (node:internal/modules/esm/resolve:1216:79)",
" at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)",
" at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)",
" at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:227:38)",
" at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39)",
" at link (node:internal/modules/esm/module_job:86:36)"
I am using NodeJs 20, removed webpack and the packaging should work with the integrated esbuild and its configuration in the monolith/serverless.yml (using serverless v4):
In the migrationService.js those imports cannot be resolved:
import config from '@cr/common/src/config.js';
import logger from '@cr/common/src/logger.js';
import { loadConfig } from '@cr/common/src/config.js';
Relevant part of the monolith/package.json:
"name": "@cr/monolith",
"type": "module",
"migrate": "babel-node --root-mode upward --ignore='node_modules' ./src/scripts/migration.js",
"dependencies": {
"@cr/common": "*",
"esbuild-plugin-alias": "0.2.1",
Relevant part of the common/package.json:
"name": "@cr/common",
"type": "module",
Relevant part of the monolith/serverless.yml:
plugins:
- serverless-offline-sqs
- serverless-offline
package:
individually: true
excludeDevDependencies: true
include:
- node_modules/@cr/common/**
- packages/common/**
custom:
esbuild:
bundle: true
minify: false
sourcemap: true
cache: false
format: 'esm'
target: 'node20'
external: []
define:
'require.resolve': undefined
plugins:
- path: '../../node_modules/esbuild-plugin-alias'
alias:
'@cr/common': '../../node_modules/@cr/common'
include:
- node_modules/@cr/common/**
- packages/common/**
Webpack was removed in favor of the ES built in esbuilt, therefore I cannot integrate the serverless-esbuilt plugin inthe monolith/serverless.yml.
Relevant part of the staging.yml:
name: staging stack
on:
push:
branches:
- staging
jobs:
deployingMonolith:
needs: [
linting,
testing,
deployingNetwork
]
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '20'
cache: 'yarn'
- run: yarn --prefer-offline
- uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ SOME_KEY }}
aws-secret-access-key: ${{ ANOTHER_KEY }}
aws-region: ${{ env.AWS_REGION }}
- run: |
yarn serverless deploy
yarn serverless invoke --function migrationService
working-directory: ./packages/monolith
When starting the local environment yarn serverless offline it works.
Is there a way to bundle the packages for the lambda using the integrated esbuilt for ES6 modules? AWS Lambda might still be having trouble with module resolution for ES6 modules.
My setup fails. Thank you, much apperciated!