Stf_F
Stf_F

Reputation: 1164

dist folder disappears during npm publish in GCP Cloud Build

I have a small Cloud Build pipeline with a build process to publish a private npm package to Artifact Registry.

My problem is that upon npm publish, the dist folder is systematically removed from the package.

I have ensured that:

Things to note:

My yaml file:

steps:
  - name: node:16
    entrypoint: npm
    args: ["run", "artifactregistry-login"]
  - name: node:16
    entrypoint: npm
    args: ["install"]
  - name: node:16
    entrypoint: npm
    args:
      ["-w", "@myapp/config", "version", "0.0.1${_PRE_ID_STRING}.$COMMIT_SHA"]
  - name: node:16
    dir: "packages/config"
    entrypoint: npm
    args: ["run", "build"]
  - name: "gcr.io/cloud-builders/gcloud"
    dir: "packages/config"
    entrypoint: bash
    args:
      - "-c"
      - |
        find . -type f ! -name "package.json" -delete
        rm -rf src
        rm -rf tsconfig
# Quick check to see if dist is there at this point and it is. 
  - name: "gcr.io/cloud-builders/gcloud"
    dir: "packages/config"
    entrypoint: bash
    args:
      - "-c"
      - |
        echo "---before publish---"
        ls -lia
        cd ./dist
  - name: node:16
    entrypoint: npm
    args: ["-w", "@myapp/config", "publish", "--no-git-tag-version"]
substitutions:
  _PRE_ID_STRING: ""
options:
  substitution_option: "ALLOW_LOOSE"
timeout: "1000s"

And the package.json (with a desperate attempt to capture my dist contents with an awful glob pattern):

{
  "name": "@myapp/config",
  "version": "xxxx",
  "license": "MIT",
  "scripts": {
    "build:cjs": "tsc -p tsconfig.cjs.json",
    "build:esm": "tsc -p tsconfig.esm.json",
    "build": "npm run build:cjs && npm run build:esm"
  },
  "exports": {
    ".": {
      "import": "./dist/esm/src/index.js",
      "require": "./dist/cjs/src/index.js"
    },
  },
  "types": "./dist/cjs/src/index.d.ts",
  "publishConfig": {
    "access": "restricted",
    "registry": "https://europe-west2-npm.pkg.dev"
  },
  "files": [
    "dist/**/*",
    "./dist/**/*",
    "./dist/**/**/*",
    "./dist/**/**/**/*",
    "./dist/**/**/*",
    "./dist/**/**/**/*",
    "dist/**/*",
    "dist/**/**/*",
    "dist/**/**/**/*",
    "dist/**/**/*",
    "dist/**/**/**/*"
  ]
}

Any idea would be massively appreciated.

Thanks,

Upvotes: 0

Views: 235

Answers (1)

Marco
Marco

Reputation: 1620

I encountered a similar issue previously when I overlooked running the build command.

Considering you're encountering the same problem, it's possible that the absence of a 'dist' folder might be due to not building the package beforehand. Perhaps you've mistakenly committed an outdated 'dist' folder?

To address this, you have a couple of options:

  1. Run npm run build prior to publishing.
  2. Alternatively, you can add the following script to your package.json:
"scripts": {
  "prepack": "npm run build"
}

This script will automatically execute the build command before the package will be published.

Upvotes: 0

Related Questions