serverless-next.js: serverless remove Error: Cannot find module '/root/.serverless/components/registry/npm/@sls-next/serverless-component@1.18.0-alpha.18/node_modules/@sls-next/serverless-component'
Describe the bug
serverless remove in my CI/CD pipeline results in this error:
Error: Cannot find module '/root/.serverless/components/registry/npm/@sls-next/serverless-component@1.18.0-alpha.18/node_modules/@sls-next/serverless-component'
Actual behavior
The remove job results in:
459$ serverless remove
460 0s › Components › Running .
461aNA
462 0s › Template › Removing .
463aNA
464 error:
465 Error: Cannot find module '/root/.serverless/components/registry/npm/@sls-next/serverless-component@1.18.0-alpha.18/node_modules/@sls-next/serverless-component'
466Require stack:
467- /usr/local/lib/node_modules/serverless/node_modules/@serverless/core/src/Component.js
468- /usr/local/lib/node_modules/serverless/node_modules/@serverless/core/src/index.js
469- /usr/local/lib/node_modules/serverless/node_modules/@serverless/cli/src/index.js
470- /usr/local/lib/node_modules/serverless/bin/serverless.js
471 at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
472 at Function.Module._load (internal/modules/cjs/loader.js:725:27)
473 at Module.require (internal/modules/cjs/loader.js:952:19)
474 at require (internal/modules/cjs/helpers.js:88:18)
475 at Template.load (/usr/local/lib/node_modules/serverless/node_modules/@serverless/core/src/Component.js:108:24)
476 at async fn (/usr/local/lib/node_modules/serverless/node_modules/@serverless/template/utils.js:309:27)
477 at async Promise.all (index 0)
478 at async syncState (/usr/local/lib/node_modules/serverless/node_modules/@serverless/template/utils.js:333:3)
479 at async Template.remove (/usr/local/lib/node_modules/serverless/node_modules/@serverless/template/serverless.js:78:5)
480 at async Object.runComponents (/usr/local/lib/node_modules/serverless/node_modules/@serverless/cli/src/index.js:218:17) {
481 code: 'MODULE_NOT_FOUND',
482 requireStack: [
483 '/usr/local/lib/node_modules/serverless/node_modules/@serverless/core/src/Component.js',
484 '/usr/local/lib/node_modules/serverless/node_modules/@serverless/core/src/index.js',
485 '/usr/local/lib/node_modules/serverless/node_modules/@serverless/cli/src/index.js',
486 '/usr/local/lib/node_modules/serverless/bin/serverless.js'
487 ]
488}
489 0s › Template › Error: Cannot find module '/root/.serverless/components/registry/npm/@sls-next/serverless-component@1.18.0-alpha.18/node_modules/@sls-next/serverless-component'
490Require stack:
491- /usr/local/lib/node_modules/serverless/node_modules/@serverless/core/src/Component.js
492- /usr/local/lib/node_modules/serverless/node_modules/@serverless/core/src/index.js
493- /usr/local/lib/node_modules/serverless/node_modules/@serverless/cli/src/index.js
494- /usr/local/lib/node_modules/serverless/bin/serverless.js
495
Cleaning up file based variables
00:01
496ERROR: Job failed: exit code 1
Expected behavior
I can run the exact code locally with sls and sls remove and it provisions and removes as expected.
The sls command correctly provisions in the initial job, but the subsequent remove job fails as per the logs above.
Steps to reproduce
.gitlab-ci-yml
image: node:latest
before_script:
- npm config set prefix /usr/local
- npm install -g serverless
- npm install
- apt-get update && apt-get install -y python3 python3-pip python3-setuptools groff less && pip3 install --upgrade pip && apt-get clean
- pip3 --no-cache-dir install --upgrade awscli
stages:
- Deploy
- Remove
Deploy Staging:
stage: Deploy
environment: staging
variables:
SLS_NEXT_S3_STATE_BUCKET: my-state-bucket-staging
script:
- echo "Deploy Staging"
- aws s3 sync s3://$SLS_NEXT_S3_STATE_BUCKET/.serverless .serverless --delete
- serverless
- aws s3 sync .serverless s3://$SLS_NEXT_S3_STATE_BUCKET/.serverless --delete
rules:
- if: $CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE != "schedule"
Remove Staging:
stage: Remove
environment: staging
variables:
SLS_NEXT_S3_STATE_BUCKET: my-state-bucket-staging
script:
- echo "Remove Staging"
- aws s3 sync s3://$SLS_NEXT_S3_STATE_BUCKET/.serverless .serverless --delete
- serverless remove
- aws s3 rm s3://$SLS_NEXT_S3_STATE_BUCKET --recursive
rules:
- if: $CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE != "schedule"
when: manual
serverless.yml (Note: I’ve removed the env variables below from the gitlab-ci.yml above for simplification
org: ${env.SLS_NEXT_ORG}
app: ${env.SLS_NEXT_APP}
name: ${env.SLS_NEXT_NAME}
stage: ${env.SLS_NEXT_STAGE}
domain: ${env.SLS_NEXT_DOMAIN}
fullDomain: '${subdomainStage.${stage}}${domain}'
subdomainStage:
prod: ''
staging: staging.
dev: dev.
distribution: ${env.SLS_NEXT_DISTRIBUTION_MAIN}
bucketName: ${env.SLS_NEXT_BUCKET_NAME}
next-sls-app:
component: '@sls-next/serverless-component@1.18.0-alpha.18'
inputs:
cloudfront:
distributionId: ${distribution}
priceClass: 'PriceClass_100'
aliases: ['${fullDomain}']
bucketName: ${bucketName}
bucketRegion: 'eu-west-1'
description: 'Lambda@Edge for ${org}-${app}-${name}-${stage}'
name:
defaultLambda: ${org}-${app}-${name}-${stage}-default
apiLambda: ${org}-${app}-${name}-${stage}-api
runtime:
defaultLambda: 'nodejs12.x'
apiLambda: 'nodejs12.x'
package.json (Note: I’ve tried all combinations of serverless being installed as dependency, and globally as per gitlab-ci.yml above, so here is an example of a local dependency)
{
"name": "app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"mysql": "^2.18.1",
"next": "9.5.4",
"next-auth": "^3.1.0",
"react": "16.13.1",
"react-dom": "16.13.1"
},
"devDependencies": {
"serverless": "^2.8.0"
}
}
Screenshots/Code/Logs
As above
Versions
- OS/Environment: node:alpine Gitlab-CI runner
- @sls-next/serverless-component version: 1.18.0-alpha.18
- Next.js version: n/a
Checklist
I have pinned the version in my serverless.yml, as per the README and I’ve reviewed all of the issues similar that I could find - most of which had issues with the deploy sls and not pinning versions. This one is similar, but definitely different in that I’m attempting to use sls remove and this is where the failure occurs. This issue existed in version 1.18.0-alpha.12 also; I upgraded and tested locally and through GitLab-CI before posting this, and the same results.
Also, I’ve reviewed the .serverless templates generated for the local environment and the s3 bucket state environment and they look the same (apart from stage and aws resources). The Template.json is as follows:
{
"components": {
"next-sls-app": "/root/.serverless/components/registry/npm/@sls-next/serverless-component@1.18.0-alpha.18/node_modules/@sls-next/serverless-component"
}
}
It seems this is what is being looked at by the GitLab CI runner and cannot find it in the environment
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 5
- Comments: 31 (3 by maintainers)
I was able to fix this by manually creating the directory before running
serverlesswith:mkdir -p /root/.serverless/components/registry/npm/@sls-next/serverless-component@1.17.0. This obviously isn’t a great solution but it should unblock everyone.The issue seems to be that serverless doesnt create the directory before it tries to install the component and
npm install <package> --prefix <dir>fails if<dir>doesnt exist. I think this is a serverless bug.I’ve tried many combinations of caching (and s3 syncing) with GitLab CI:
All result in the same error when trying remove. Are you suggesting another caching option to try?
I haven’t wanted to write this, but I tend to agree with how much investment should be given to IaC and state-management in general should be given. I keep reading, and have followed much of the advice, to use terraform (or CDK) to provision and maintain infrastructure. Whilst almighty efforts have been given to try and work around serverless, serverless components general and this serverless next componentry, it feels like there’s a combination of reinventing the wheel whilst also trying to merge multiple moving parts.
Before this I was looking to try this; next-aws-lambda-webpack-plugin, to transform the referenced CloudFormation/SAM spec into terraform. But I thought I’d gain the benefits of the serverless framework by using this serverless next component. Having researched further, and especially when I understood that serverless components weren’t really the serverless framework as such anymore, I’m now not sure what benefits there are for infrastructure provisioning and maintenance left that isn’t better suited elsewhere.
Personally I’d rather this bug went stale if I knew that in the near future I’d be able to utilise terraform (or CDK) end-to-end for IaC provisioning into AWS for Next JS. The semi-manual removal stage is no big deal for me at the moment. Workflow for using terraform with serverless infrastructure is already straight-forward enough to keep them separated and sharing, e.g. using SSM Param Store, or CI/CD pipeline outputs. Trying to wedge something else in-between has taken weeks’ worth of effort that I feel could be much simpler architecturally.
Please, please do not take this as anything other than me agreeing with your conclusion on time investment for this bug @dphang. You are all doing outstanding work and I’ll be continuing with this componentry for this current project to see how it pans out.
Thank you for this thread. I was hit by a (probably) related issue. Since yesterday, I could not deploy anymore through CircleCI. Deploy from local machine worked just fine, and I was lost why.
For those running into simular problems: I was able to fix by
Copying the directory from the CircleCI build log where it failed. In my case
/home/circleci/.serverless/components/registry/npm/@sls-next/serverless-component@1.17.0.Adding a step in my CircleCI script just before
npx serverlessdeploy step, that doesmkdir - p [directory here].This fixed the deployment through CI/CD for me.
That would be awesome @dphang. I’d not heard that terraform had a CDK until now, so I’ve spent all night looking into this, as well as Pulumi. It seems the TF CDK is quite some way away from primetime, whereas Pulumi is very mature with a migration path from TF. However, given the size of the TF community and business adoption of TF so far, I suspect the TF CDK is probably going to come out on top. Good luck and thanks again!
Seeing this issue as well. This seems to be related https://github.com/serverless-nextjs/serverless-next.js/issues/504? cc @dphang @danielcondemarin
@damo78 I do not use GitLab (CircleCI instead), but the error surely seems like the one I am getting. Also the behavior is same as mine: deploy from Local works fine, deploy through CI/CD fails.
What you could try: Add a step in your
.gitlab-ci-yml. My guess woulld be in thebefore_script, just after- npm installThat says:
- mkdir -p /root/.serverless/components/registry/npm/@sls-next/serverless-component@1.18.0-alpha.18Which is the (missing) directory that cause the fail.
For me this solved it. Hope you have similar luck!
Just to clarify my earlier comment. I’m seeing this when i run
serverless, notserverless remove