question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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'

See original GitHub issue

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

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:31 (3 by maintainers)

github_iconTop GitHub Comments

18reactions
stan-sackcommented, Oct 25, 2020

I was able to fix this by manually creating the directory before running serverless with: 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.

3reactions
damo78commented, Nov 2, 2020

Perhaps another workaround is to cache that directory in Template.json somewhere and then load it before calling serverless remove.

I’ve tried many combinations of caching (and s3 syncing) with GitLab CI:

  • .serverless (from deploy stage to remove, with and without s3 state syncing)
  • global and local npm packages, with and without using npx

All result in the same error when trying remove. Are you suggesting another caching option to try?

@danielcondemarin I’m not sure if there’s a quick fix as it seems the problem is inherent to Serverless. We are planning to explore other proper IaC options, so we may not want to invest too much into fixing the state management issues.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error cannot find module after deploying Cron app
After I deploy, I get the message “Error: Cannot find module 'axios'” . This will run locally, and I have run npm install...
Read more >
Serverless Framework with AWS Lambda error "Cannot find ...
I have managed to run the sample, (https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb) successfully, now hacking ...
Read more >
Error: Cannot find module '@serverless/test/setup/log' #11248
I had attempted to test previous to this release and had no issue. Steps to reproduce: Clone serverless/serverless; npm install; npm test ...
Read more >
Top 10 Serverless Deployment Errors (and How to Fix Them)
1. Invalid CloudFormation state · 2. Unable to fetch parameters · 3. API Gateway: No integration defined for method · 4. Codebuild deploy...
Read more >
serverless-optimizer-plugin - npm
Uncaught {"errorMessage":"Cannot find module '/usr/lib/node_modules/aws-sdk/apis/metadata.json'"... To fix this, the aws- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found