Travis Build failing: Cannot find module

on staging travis builds the library and docs then publishes to gemfury and aws. Looks like those are fine. I have Exit 1 for any build or publish fail, but after the package gets published there is an error.

I have added Exit 1 behind the all the publish, yarn, and curl commands.

Travis Error:

~~> Processing package upload
    se-fe-tooltip-2.0.1-staging.3480.tgz ... ok
internal/modules/cjs/loader.js:905
  throw err;
  ^
after_script
0.03s$ ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
Done. Your build exited with 0.

travis.yml file

dist: bionic
addons:
  chrome: stable
language: node_js
git:
  depth: false
before_install:
- pip install --user awscli
before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- "./cc-test-reporter before-build"
script:
- yarn test:ci
after_script:
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
before_deploy:
- chmod -x scripts/publish*
deploy:
- provider: script
  script: bash ./scripts/publish.sh main
  skip_cleanup: true
  on:
    branch: main
    type: push
- provider: script
  script: bash ./scripts/publish.sh staging
  skip_cleanup: true
  on:
    branch: staging*
    type: push
branches:
  only:
    - main
    - "/^staging.*$/"

publish script:

#!/bin/bash

find_changed_projects () {
  for project in $(ls projects); do
    git diff --name-status origin/HEAD | grep -q projects/$project/
    if [ $? == 0 ]; then
      echo $project
    fi
  done
}

prepare_version () {
  version=$(yarn --silent --cwd projects/$1/ current-version)
  if [ "$branch" = "staging" ]; then
    build_number=${TRAVIS_BUILD_NUMBER:=0}
    version="${version}-staging.${build_number}"
    yarn --cwd projects/$1/ version --new-version ${version} --no-git-tag-version
  fi
  ! yarn info $1 versions | grep -q \'${version}\' # returns successful if version does not exist
}

read_package_attr() {
  echo $(node -pe "require('./projects/$1/package.json').$2 || ''")
}

publish_to_gemfury () {
  package_name=$(npm pack dist/$1/)
  echo "$package_name is unpublished. Publishing to gemfury..."
  curl -F package=@${package_name} https://${GEMFURY_TOKEN}@push.fury.io/sportngin/ || exit 1
}

publish_to_aws () {
  project=$1
  version=read_package_attr $1 'version'
  echo "$project $version is unpublished. Publishing to aws..."

  if [ $branch == staging ]; then
    export AWS_ACCESS_KEY_ID=$STAGING_AWS_ACCESS_KEY_ID
    export AWS_SECRET_ACCESS_KEY=$STAGING_AWS_SECRET_ACCESS_KEY
    export NG_BUCKET=se-fe-angular-services.ui.stage.ngin-staging.com
    export NG_DISTRIBUTION=E1FUEBH5VVAABX
  elif [ $branch == main ]; then
    export AWS_ACCESS_KEY_ID=$PRODUCTION_AWS_ACCESS_KEY_ID
    export AWS_SECRET_ACCESS_KEY=$PRODUCTION_AWS_SECRET_ACCESS_KEY
    export NG_BUCKET=se-fe-angular-services.ui.sportngin.com
    export NG_DISTRIBUTION=E2NXPBAHB3K6RW
  else
    echo Please set a valid TARGET_ENV environment variable
    exit 1
  fi

  echo "Publish ./dist/$project -> /$project/*"

  aws s3 sync ./dist/${project} s3://$NG_BUCKET/${1} --delete --cache-control max-age=0 --acl public-read
  aws s3 sync ./dist/${project} s3://$NG_BUCKET/${1}/${version} --delete --cache-control max-age=0 --acl public-read
  aws s3 cp ./index.tpl.html s3://$NG_BUCKET/index.html --acl public-read

  aws cloudfront create-invalidation --distribution-id $NG_DISTRIBUTION --paths "/$project/*"
}


### MAIN ###

branch=$1

echo "Preparing to publish ${branch} packages"

if [ "$branch" != "main" ] && [ "$branch" != "staging" ]; then
  echo "Could not publish. First argument 'branch' must either be 'staging' or 'main'."
  exit 1
fi

yarn clean

if [ "$branch" = "main" ]; then
  ng_env="production"
  projects=$(ls projects)
else
  ng_env="staging"
  projects=$(find_changed_projects)
  if [ -z "$projects" ]; then
    echo "No changed projects detected."
  else
    printf "Changes detected in:\n$projects"
  fi
fi

for project in $projects; do
  prepare_version $project
  if [ $? -eq 0 ]; then
    s3_project=$(read_package_attr $project 'deployS3')
    gemfury_project=$(read_package_attr $project 'deployGemfury')
    if [ -n "$s3_project" ]; then
      yarn build $s3_project --configuration=$ng_env || exit 1
    fi
    if [ -n "$gemfury_project" ]; then
      yarn build $gemfury_project --configuration=$ng_env || exit 1
    fi
  else
    echo "$project version is up to date, skipping publishing."
  fi
done

dist_projects=$(ls dist)
for project in $dist_projects; do
  s3_project=$(read_package_attr $project 'deployS3')
  gemfury_project=$(read_package_attr $project 'deployGemfury')
  if [ -n "$s3_project" ]; then
    echo "Publishing to AWS: ${project}"
    publish_to_aws $s3_project
  fi
  if [ -n "$gemfury_project" ]; then
    echo "Publishing to Gemfury: ${project}"
    publish_to_gemfury $gemfury_project
  fi
done

Hi @Northskool,

I think initializer in this case is an npm package named create-<initializer>, which will be installed by npm-exec, and then have its main bin executed – presumably creating or updating package.json and running any other initialization-related operations, and this is causing the problem (most likely.)

1 Like

this was precisely the issue

Hello,

Yes I figured. Glad this help you.

1 Like