Skip to content

(lambda-nodejs): nodeModules with pnpm doesn't inherit project .npmrc for private registry authentication #36567

@sebastianplesciuc

Description

@sebastianplesciuc

Describe the bug

When using nodeModules with pnpm, CDK creates a pnpm-workspace.yaml in the bundling temp directory (added in #21911 to fix #21910). This causes pnpm to treat the bundling directory as a standalone workspace root, which prevents it from inheriting .npmrc configuration from parent directories. This breaks authentication for packages from private registries (e.g., AWS CodeArtifact).

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Library Version

No response

Expected Behavior

When nodeModules is specified, pnpm install in the bundling directory should be able to authenticate with private registries configured in the project's .npmrc.

Current Behavior

pnpm install fails with 401 Unauthorized errors when trying to fetch packages (or their dependencies) from private registries, because the bundling directory's pnpm-workspace.yaml prevents pnpm from finding the project's .npmrc with auth tokens.

Reproduction Steps

  1. Create a pnpm monorepo with a private registry configured in .npmrc:
@myorg:registry=https://my-private-registry.example.com/npm/
//my-private-registry.example.com/npm/:_authToken=<token>
  1. Add a NodejsFunction with nodeModules referencing a package from the private registry:
new NodejsFunction(this, 'MyFunction', {
  entry: 'src/handler.ts',
  bundling: {
    nodeModules: ['@myorg/my-private-package'],
  },
});
  1. Run cdk synth

  2. Observe 401 errors during bundling because pnpm can't find the auth token

Possible Solution

  • Copy the project's .npmrc to the bundling directory alongside pnpm-workspace.yaml
  • Or document this limitation and the workaround (use global ~/.npmrc or commandHooks.beforeInstall to copy .npmrc)

Additional Information/Context

Related: #21910, #21911

Workarounds:

  • Store auth tokens in global ~/.npmrc instead of project .npmrc
  • Use commandHooks.beforeInstall to copy .npmrc to the bundling directory

AWS CDK Library version (aws-cdk-lib)

2.194.0

AWS CDK CLI version

2.1015.0

Node.js Version

24

OS

MacOS 15.7.3 (24G419)

Language

TypeScript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions