aws / aws/aws-cdk

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

Open
#36,567 1 comment 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-lambda-nodejs bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

When using `nodeModules` with pnpm, CDK creates a `pnpm-workspace.yaml` in the bundling temp directory (added in [#21911](https://github.com/aws/aws-cdk/pull/21911) to fix [#21910](https://github.com/aws/aws-cdk/issues/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=
```

2. 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'],
},
});
```

3. Run `cdk synth`

4. 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](https://github.com/aws/aws-cdk/issues/21910), [#21911](https://github.com/aws/aws-cdk/pull/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_

Contributor guide

Open the contributing guide

Research direction

Start by tracing the lambda-nodejs nodeModules bundling flow and where pnpm-workspace.yaml is created. Reproduce the issue with a pnpm monorepo, a private registry, and cdk synth; the work is done when pnpm install can authenticate using the project .npmrc, or the limitation and workaround are documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.