Skip to content

Commit 730bcc9

Browse files
dreamorosisthulb
andauthored
fix(layers): add createRequire banner in esm (#2232)
Co-authored-by: Simon Thulbourn <[email protected]>
1 parent aa6e6e0 commit 730bcc9

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

Diff for: layers/src/layer-publisher-stack.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,24 @@ export class LayerPublisherStack extends Stack {
174174
.join(' ')}`
175175
);
176176

177-
// Phase 5: Copy files from tmp folder to cdk.out asset folder (the folder is created by CDK)
177+
// Phase 5: patch require keyword in ESM Tracer package due to AWS X-Ray SDK for Node.js not being ESM compatible
178+
const esmTracerPath = join(
179+
tmpBuildDir,
180+
'node_modules',
181+
'@aws-lambda-powertools/tracer',
182+
'lib',
183+
'esm',
184+
'provider',
185+
'ProviderService.js'
186+
);
187+
execSync(
188+
`echo "import { createRequire } from 'module'; const require = createRequire(import.meta.url);$(cat ${esmTracerPath})" > ${esmTracerPath}`
189+
);
190+
191+
// Phase 6: Copy files from tmp folder to cdk.out asset folder (the folder is created by CDK)
178192
execSync(`cp -R ${tmpBuildPath}${sep}* ${outputDir}`);
179193

180-
// Phase 6: (Optional) Restore changes to the project root made by the build
194+
// Phase 7: (Optional) Restore changes to the project root made by the build
181195
buildFromLocal &&
182196
execSync('git restore packages/*/package.json', {
183197
cwd: projectRoot,

0 commit comments

Comments
 (0)