pipelines: Calling `npm ci` only works after updating npm
The CDK pipelines example contains the following synth step:
const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
synth: new pipelines.ShellStep('Synth', {
...
commands: [
'npm ci',
'npm run build',
'npx cdk synth',
],
}),
});
When I tried this, however, my pipelines failed at the npm ci command with a strange error message:
[Container] 2022/12/14 16:00:37 Running command npm ci
npm ERR! Cannot read property 'aws-cdk-lib' of undefined
The package aws-cdk-lib was the first entry in my package.json dependencies. So it seems like CodeBuild was not able to parse my dependencies when installing via npm ci.
The reason that npm ci does not work is that CodeBuild uses an old version of npm. When I checked, the newest release was 9.2.0 and CodeBuild was using 6.14.17.
Updating npm before running npm ci fixes the issue:
const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
synth: new pipelines.ShellStep('Synth', {
...
// Update npm before running commands
installCommands: ['npm i -g npm@latest'],
commands: [
'npm ci',
'npm run build',
'npx cdk synth',
],
}),
});
I would suggest adding the installCommands line in the documentation so other users can avoid this issue.
Stackoverflow issue: Link Minimum working example with issue and fix: Link
Links
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.pipelines-readme.html
Hi @estrehle , I tried to repro the issue and got an error at build stage(not at the "npm ci"). Could you please share some more information to help me in repro of the issue? As I got the build failure error, I implemented the fix you provided which did not work. From my understanding , it seems like the issue lies with "npm build" command as this package seems to be outdated. But even that would need more investigation. Could you please try to remove the "npm build" command and see if that works for you? (with initial code sample provided in documentation)
This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.
I can confirm the bug and the suggested fix.
From what I can tell, this is due to the package-lock.json being lock version 3, while the npm version on code build is only supporting up to lock version 2.
~~This has impacted me as well. I'm confused why this started happening recently. I don't have an exact time other than the past few months but I have multiple pipelines that were created using cdkv2 and now with an update of cdk version number (v2.58.0 -> v2.65.0 in at least one case) completely breaks the pipeline synth step specifically on the npm ci command.~~
~~Going to investigate @DASPRiD 's comment. Not sure what lock versions are w/re: to package-lock.json.~~
~~Perhaps I'm not understanding something but I don't believe you can modify the cdk code to adjust the behavior because the self-mutate step never has a chance to execute. One must navigate to the CodeBuild project for the pipeline and directly modify the npm commands in the buildspec.~~
~~Additionally, I took the opportunity to update the pipeline build environment from standard:5.0 to standard:6.0. Once that is accomplished npm ci works w/o the npm i -g npm@latest fix. At least that's what I'm seeing.~~
OK so this was due to me most recently changing my default node via nvm to v18.
I think this is fixed by https://github.com/aws/aws-cdk/pull/24544
Even after updating npm, it still fails. I currently cannot use CDK to deploy any apps. Is there anyone from AWS working on this?
We don't have a good mechanism for monitoring comments on closed isseus/prs. If this is still an issue, please create a new issue for us to track.
@a-shatou Works for me using "aws-cdk-lib": "2.81.0"