Skip to content

fix: replace dynamic import() with require() to fix pkg binary vm.Script error - #14966

Open
shivennn wants to merge 2 commits into
aws-amplify:devfrom
shivennn:fix/dynamic-import-callback-codegen-add
Open

fix: replace dynamic import() with require() to fix pkg binary vm.Script error#14966
shivennn wants to merge 2 commits into
aws-amplify:devfrom
shivennn:fix/dynamic-import-callback-codegen-add

Conversation

@shivennn

@shivennn shivennn commented Jul 30, 2026

Copy link
Copy Markdown

Root Cause:

The Amplify CLI is distributed as a pkg-bundled native binary (~/.amplify/bin/amplify). Inside pkg's runtime, module execution happens via Node's vm.Script. When vm.Script encounters a dynamic import() call with a runtime-computed path, Node throws ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING because pkg does not set the required importModuleDynamically callback.

Two files were affected:

packages/amplify-cli/src/extensions/amplify-helpers/execute-provider-utils.ts
packages/amplify-cli/src/extensions/amplify-helpers/get-provider-plugins.ts

Fix:

Replace dynamic import() calls with require(). Since all provider plugins (e.g. amplify-provider-awscloudformation) are CommonJS modules, require() is fully compatible and works correctly inside pkg's bundled runtime.

Description of changes

When amplify codegen add --apiId --region is run from a project without amplify init, the CLI fails at the "Getting API details" step with:

✖ Getting API details
A dynamic import callback was not specified.

Issue #, if available

Fixes #14942

Description of how you validated changes

Built locally using yarn setup-dev and tested against a fresh Create React App project without amplify init:

Before fix:

✖ Getting API details
A dynamic import callback was not specified.

After fix:

✓ Getting API details
  • Verified on Node.js v18.20.8 and v24.17.0.

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)
  • New AWS SDK calls or CloudFormation actions have been added to relevant test and service IAM policies
  • Pull request labels are added

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Shiven added 2 commits July 31, 2026 01:57
…ipt error

When the Amplify CLI is run as a pkg-bundled binary, Node's vm.Script
does not set the importModuleDynamically callback. Any dynamic import()
call with a runtime-computed path throws ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
causing 'amplify codegen add --apiId' to fail at the 'Getting API details' step.

Replace dynamic import() with require() in execute-provider-utils.ts and
get-provider-plugins.ts since all provider plugins are CommonJS modules.

Fixes aws-amplify#14942
… pkg binary fix

Adds tests to execute-provider-utils and get-provider-plugins to verify
that provider plugins are loaded via require() which is compatible with
pkg-bundled binaries. Relates to aws-amplify#14942
@shivennn
shivennn requested a review from a team as a code owner July 30, 2026 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant