Search code examples
amazon-web-servicesaws-codepipelineaws-codebuildsveltekitnpm-build

SvelteKit: 'npm run build' error in AWS CodeBuild


I have a SvelteKit app that I'm deploying using AWS CodePipeline.

My pipeline gets triggered on pushes to CodeCommit. But CodeBuild fails with the following error messages:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `svelte-kit build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
[Container] 2021/11/30 14:45:25 Command did not exit successfully npm run build exit status 1

I tried cloning the repository and building it in a clean environment on my PC and it builds without any errors. I cannot reproduce the problem outside of CodeBuild.


buildspec.yml

    version: 0.2
    
    phases: 
        install:
            commands:
                - echo "Entered the install phase..."
                - npm install
        pre_build:
            commands: 
                - echo "Entered the pre_build phase..."
        build:
            commands:
                - echo "Entered the build phase..."
                - echo "Build started on `date`"
                - npm run build
        post_build:
            commands:
                - echo "Entered the post_build phase..."
                - echo "Build completed on `date`"
    
    artifacts:
      files:
        - '**/*'
      name: project-title

package.json

{
  "name": "project-title",
  "version": "0.0.1",
  "scripts": {
    "dev": "svelte-kit dev",
    "build": "svelte-kit build",
    "preview": "svelte-kit preview",
    "lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
    "format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ."
  },
  "devDependencies": {
    "@sveltejs/adapter-static": "^1.0.0-next.21",
    "@sveltejs/kit": "next",
    "autoprefixer": "^10.4.0",
    "eslint": "^7.32.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-svelte3": "^3.2.1",
    "prettier": "^2.4.1",
    "prettier-plugin-svelte": "^2.4.0",
    "svelte": "^3.42.6",
    "tailwindcss": "^2.2.19"
  },
  "type": "module"
}

svelte.config.js

import adapter from '@sveltejs/adapter-static';

export default {
    kit: {
        adapter: adapter({
            // default options are shown
            pages: 'build',
            assets: 'build',
            fallback: null
        })
    }
};

CodeBuild project environment:

environment:


Solution

  • I ended up doing the following workaround: I build the project on my own machine and then have a codecommit repository for the build files.

    Then I created a CodeBuild project that syncs the CodeCommit repository with the S3 bucket I want. The command CodeBuild uses is:

    aws s3 sync . s3://BUCKET_NAME --delete
    

    The "--delete" option basically removes all files from S3, that are not in the repository and the sync command copies over the new files and updates the files that are already in there.

    OPTIONAL: I didn't set up a codepipeline, as I don't always need to run CodeBuild when pushing to the repository. So I just start the CodeBuild project manually, on a as-needed basis.

    OPTIONAL 2: My static website is accessed through a CloudFront distribution, so I create a CloudFront invalidation every time I use CodeBuild with the following command (after the sync command):

     aws cloudfront create-invalidation --distribution-id ID_OF_DISTRIBUTION --paths '/*'