diff --git a/API.md b/API.md
index 59b727ab..235fab0a 100644
--- a/API.md
+++ b/API.md
@@ -2634,6 +2634,7 @@ const imageOptimizationProps: ImageOptimizationProps = { ... }
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| nextjsPath
| string
| Relative path to the directory where the NextJS project is located. |
+| buildPath
| string
| The directory to execute `npm run build` from. |
| compressionLevel
| number
| 0 - no compression, fatest 9 - maximum compression, slowest. |
| environment
| {[ key: string ]: string}
| Custom environment variables to pass to the NextJS build and runtime. |
| isPlaceholder
| boolean
| Skip building app and deploy a placeholder. |
@@ -2661,6 +2662,22 @@ Can be the root of your project (`.`) or a subdirectory (`packages/web`).
---
+##### `buildPath`Optional
+
+```typescript
+public readonly buildPath: string;
+```
+
+- *Type:* string
+
+The directory to execute `npm run build` from.
+
+By default, it uses `nextjsPath`.
+Can be overridden, particularly useful for monorepos where `build` is expected to run
+at the root of the project.
+
+---
+
##### `compressionLevel`Optional
```typescript
@@ -2848,6 +2865,7 @@ const nextjsAssetsDeploymentProps: NextjsAssetsDeploymentProps = { ... }
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| nextjsPath
| string
| Relative path to the directory where the NextJS project is located. |
+| buildPath
| string
| The directory to execute `npm run build` from. |
| compressionLevel
| number
| 0 - no compression, fatest 9 - maximum compression, slowest. |
| environment
| {[ key: string ]: string}
| Custom environment variables to pass to the NextJS build and runtime. |
| isPlaceholder
| boolean
| Skip building app and deploy a placeholder. |
@@ -2876,6 +2894,22 @@ Can be the root of your project (`.`) or a subdirectory (`packages/web`).
---
+##### `buildPath`Optional
+
+```typescript
+public readonly buildPath: string;
+```
+
+- *Type:* string
+
+The directory to execute `npm run build` from.
+
+By default, it uses `nextjsPath`.
+Can be overridden, particularly useful for monorepos where `build` is expected to run
+at the root of the project.
+
+---
+
##### `compressionLevel`Optional
```typescript
@@ -3032,6 +3066,7 @@ const nextjsBaseProps: NextjsBaseProps = { ... }
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| nextjsPath
| string
| Relative path to the directory where the NextJS project is located. |
+| buildPath
| string
| The directory to execute `npm run build` from. |
| compressionLevel
| number
| 0 - no compression, fatest 9 - maximum compression, slowest. |
| environment
| {[ key: string ]: string}
| Custom environment variables to pass to the NextJS build and runtime. |
| isPlaceholder
| boolean
| Skip building app and deploy a placeholder. |
@@ -3055,6 +3090,22 @@ Can be the root of your project (`.`) or a subdirectory (`packages/web`).
---
+##### `buildPath`Optional
+
+```typescript
+public readonly buildPath: string;
+```
+
+- *Type:* string
+
+The directory to execute `npm run build` from.
+
+By default, it uses `nextjsPath`.
+Can be overridden, particularly useful for monorepos where `build` is expected to run
+at the root of the project.
+
+---
+
##### `compressionLevel`Optional
```typescript
@@ -3147,6 +3198,7 @@ const nextjsBuildProps: NextjsBuildProps = { ... }
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| nextjsPath
| string
| Relative path to the directory where the NextJS project is located. |
+| buildPath
| string
| The directory to execute `npm run build` from. |
| compressionLevel
| number
| 0 - no compression, fatest 9 - maximum compression, slowest. |
| environment
| {[ key: string ]: string}
| Custom environment variables to pass to the NextJS build and runtime. |
| isPlaceholder
| boolean
| Skip building app and deploy a placeholder. |
@@ -3170,6 +3222,22 @@ Can be the root of your project (`.`) or a subdirectory (`packages/web`).
---
+##### `buildPath`Optional
+
+```typescript
+public readonly buildPath: string;
+```
+
+- *Type:* string
+
+The directory to execute `npm run build` from.
+
+By default, it uses `nextjsPath`.
+Can be overridden, particularly useful for monorepos where `build` is expected to run
+at the root of the project.
+
+---
+
##### `compressionLevel`Optional
```typescript
@@ -3419,6 +3487,7 @@ const nextjsDistributionProps: NextjsDistributionProps = { ... }
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| nextjsPath
| string
| Relative path to the directory where the NextJS project is located. |
+| buildPath
| string
| The directory to execute `npm run build` from. |
| compressionLevel
| number
| 0 - no compression, fatest 9 - maximum compression, slowest. |
| environment
| {[ key: string ]: string}
| Custom environment variables to pass to the NextJS build and runtime. |
| isPlaceholder
| boolean
| Skip building app and deploy a placeholder. |
@@ -3451,6 +3520,22 @@ Can be the root of your project (`.`) or a subdirectory (`packages/web`).
---
+##### `buildPath`Optional
+
+```typescript
+public readonly buildPath: string;
+```
+
+- *Type:* string
+
+The directory to execute `npm run build` from.
+
+By default, it uses `nextjsPath`.
+Can be overridden, particularly useful for monorepos where `build` is expected to run
+at the root of the project.
+
+---
+
##### `compressionLevel`Optional
```typescript
@@ -3787,6 +3872,7 @@ const nextjsLambdaProps: NextjsLambdaProps = { ... }
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| nextjsPath
| string
| Relative path to the directory where the NextJS project is located. |
+| buildPath
| string
| The directory to execute `npm run build` from. |
| compressionLevel
| number
| 0 - no compression, fatest 9 - maximum compression, slowest. |
| environment
| {[ key: string ]: string}
| Custom environment variables to pass to the NextJS build and runtime. |
| isPlaceholder
| boolean
| Skip building app and deploy a placeholder. |
@@ -3812,6 +3898,22 @@ Can be the root of your project (`.`) or a subdirectory (`packages/web`).
---
+##### `buildPath`Optional
+
+```typescript
+public readonly buildPath: string;
+```
+
+- *Type:* string
+
+The directory to execute `npm run build` from.
+
+By default, it uses `nextjsPath`.
+Can be overridden, particularly useful for monorepos where `build` is expected to run
+at the root of the project.
+
+---
+
##### `compressionLevel`Optional
```typescript
@@ -3939,6 +4041,7 @@ const nextjsProps: NextjsProps = { ... }
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| nextjsPath
| string
| Relative path to the directory where the NextJS project is located. |
+| buildPath
| string
| The directory to execute `npm run build` from. |
| compressionLevel
| number
| 0 - no compression, fatest 9 - maximum compression, slowest. |
| environment
| {[ key: string ]: string}
| Custom environment variables to pass to the NextJS build and runtime. |
| isPlaceholder
| boolean
| Skip building app and deploy a placeholder. |
@@ -3964,6 +4067,22 @@ Can be the root of your project (`.`) or a subdirectory (`packages/web`).
---
+##### `buildPath`Optional
+
+```typescript
+public readonly buildPath: string;
+```
+
+- *Type:* string
+
+The directory to execute `npm run build` from.
+
+By default, it uses `nextjsPath`.
+Can be overridden, particularly useful for monorepos where `build` is expected to run
+at the root of the project.
+
+---
+
##### `compressionLevel`Optional
```typescript
diff --git a/src/NextjsBase.ts b/src/NextjsBase.ts
index 588de831..1e557337 100644
--- a/src/NextjsBase.ts
+++ b/src/NextjsBase.ts
@@ -14,6 +14,13 @@ export interface NextjsBaseProps {
*/
readonly nextjsPath: string;
+ /**
+ * The directory to execute `npm run build` from. By default, it uses `nextjsPath`.
+ * Can be overridden, particularly useful for monorepos where `build` is expected to run
+ * at the root of the project.
+ */
+ readonly buildPath?: string;
+
/**
* Custom environment variables to pass to the NextJS build and runtime.
*/
diff --git a/src/NextjsBuild.ts b/src/NextjsBuild.ts
index 05f9c8e0..4545d0e6 100644
--- a/src/NextjsBuild.ts
+++ b/src/NextjsBuild.ts
@@ -115,10 +115,11 @@ export class NextjsBuild extends Construct {
...(this.props.nodeEnv ? { NODE_ENV: this.props.nodeEnv } : {}),
};
+ const buildPath = this.props.buildPath ?? nextjsPath;
// run build
- console.debug('├ Running "npm build" in', nextjsPath);
+ console.debug('├ Running "npm build" in', buildPath);
const buildResult = spawn.sync('npm', ['run', 'build'], {
- cwd: nextjsPath,
+ cwd: buildPath,
stdio: this.props.quiet ? 'ignore' : 'inherit',
env: buildEnv,
});