Skip to content

Commit 716f844

Browse files
author
Kamil Sobol
authored
Handle ENOENT error from npm (#2267)
1 parent 754d0f7 commit 716f844

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

.changeset/nasty-jars-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-deployer': patch
3+
---
4+
5+
Handle ENOENT error from npm

packages/backend-deployer/src/cdk_error_mapper.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,52 @@ for your current platform.
513513
errorName: 'ESBuildError',
514514
expectedDownstreamErrorMessage: undefined,
515515
},
516+
{
517+
errorMessage:
518+
// eslint-disable-next-line spellcheck/spell-checker
519+
`Error: npm ERR! code ENOENT
520+
npm ERR! syscall lstat
521+
npm ERR! path /opt/homebrew/Cellar/node/22.2.0/lib
522+
npm ERR! errno -2
523+
npm ERR! enoent ENOENT: no such file or directory, lstat '/opt/homebrew/Cellar/node/22.2.0/lib'
524+
npm ERR! enoent This is related to npm not being able to find a file.
525+
npm ERR! enoent
526+
`,
527+
expectedTopLevelErrorMessage:
528+
// eslint-disable-next-line spellcheck/spell-checker
529+
`NPM error occurred: npm ERR! code ENOENT
530+
npm ERR! syscall lstat
531+
npm ERR! path /opt/homebrew/Cellar/node/22.2.0/lib
532+
npm ERR! errno -2
533+
npm ERR! enoent ENOENT: no such file or directory, lstat '/opt/homebrew/Cellar/node/22.2.0/lib'
534+
npm ERR! enoent This is related to npm not being able to find a file.
535+
npm ERR! enoent`,
536+
errorName: 'CommonNPMError',
537+
expectedDownstreamErrorMessage: undefined,
538+
},
539+
{
540+
errorMessage:
541+
// eslint-disable-next-line spellcheck/spell-checker
542+
`Error: npm error code ENOENT
543+
npm error syscall lstat
544+
npm error path C:\\Users\testUser\\AppData\\Roaming\\npm
545+
npm error errno -4058
546+
npm error enoent ENOENT: no such file or directory, lstat 'C:\\Users\\testUser\\AppData\\Roaming\\npm'
547+
npm error enoent This is related to npm not being able to find a file.
548+
npm error enoent
549+
`,
550+
expectedTopLevelErrorMessage:
551+
// eslint-disable-next-line spellcheck/spell-checker
552+
`NPM error occurred: npm error code ENOENT
553+
npm error syscall lstat
554+
npm error path C:\\Users\testUser\\AppData\\Roaming\\npm
555+
npm error errno -4058
556+
npm error enoent ENOENT: no such file or directory, lstat 'C:\\Users\\testUser\\AppData\\Roaming\\npm'
557+
npm error enoent This is related to npm not being able to find a file.
558+
npm error enoent`,
559+
errorName: 'CommonNPMError',
560+
expectedDownstreamErrorMessage: undefined,
561+
},
516562
];
517563

518564
void describe('invokeCDKCommand', { concurrency: 1 }, () => {

packages/backend-deployer/src/cdk_error_mapper.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ export class CdkErrorMapper {
324324
errorName: 'InvalidPackageJsonError',
325325
classification: 'ERROR',
326326
},
327+
{
328+
errorRegex: new RegExp(
329+
`(?<npmError>(npm error|npm ERR!) code ENOENT${this.multiLineEolRegex}((npm error|npm ERR!) (.*)${this.multiLineEolRegex})*)`
330+
),
331+
humanReadableErrorMessage: 'NPM error occurred: {npmError}',
332+
resolutionMessage: `See https://docs.npmjs.com/common-errors for resolution.`,
333+
errorName: 'CommonNPMError',
334+
classification: 'ERROR',
335+
},
327336
{
328337
// Error: .* is printed to stderr during cdk synth
329338
// Also extracts the first line in the stack where the error happened
@@ -407,6 +416,7 @@ export type CDKDeploymentError =
407416
| 'CDKVersionMismatchError'
408417
| 'CFNUpdateNotSupportedError'
409418
| 'CloudFormationDeploymentError'
419+
| 'CommonNPMError'
410420
| 'FilePermissionsError'
411421
| 'MissingDefineBackendError'
412422
| 'MultipleSandboxInstancesError'

0 commit comments

Comments
 (0)