Did something recently change with npm prepare scripts?

In our NodeJS app (call it our_app), we have one dependency (call it our_dependency, one we maintain in a private repo on GitHub) that has a prepare script in its package.json. Here’s a snippet from our_dependency's package.json:

  "scripts": {
    "build": "tsc",
    "prepare": "npm run build",
    ...
  },

Recently, builds for our_app started failing, with an error of:

WORKER STDERR: Unhandled rejection Error: Cannot find module '/root/*********/our_app/node_modules/our_dependency/build/index.js'. Please verify that the package.json has a valid "main" entry

The lack of a file in node_modules/our_dependency/build/ makes me think CircleCI has changed something with how it’s storing data created from a prepare script. We changed nothing related to this dependency, and I re-ran a build that passed perfectly fine a week ago, and it now fails with the same error.

Once I modified the package.json in our_app , which I believe forces a clean npm install from scratch, then the build passes.

The config.yml for our_app does indeed use restore_cache. Here’s a relevant snippet from that file:

version: 2.1

package-cache: &package-cache
  key: npm-cache-{{ checksum "package.json" }}-{{ checksum "package-lock.json" }}
...
steps:
    - checkout
    - restore_cache:
        <<: *package-cache
    - run:
        name: Install npm dependencies
        command: |
          # npm set used to set a variable or two
          npm install
    - save_cache:
        <<: *package-cache
        paths:
        - ./node_modules
...

Anyone else seen this issue, or anyone from CircleCI who can help explain if something changed in the system with regards to this? Seems like if a prepare script ran, anything within node_modules, including the contents of node_modules/our_dependency/build/, should have been maintained in CircleCI’s cache, correct?

One last note: the build/ directory is set to be ignored in the our_dependency repo. Think that could that be causing any weirdness with CircleCI’s caching?

Thanks!

Hi @karlbecker! Our support team is definitely going to have to dig into this one. It looks like you have a ticket open, and I’d love to hear any solutions you come up with!

Hopefully this is helpful. Found this article: npm5 prepare scripts not executing properly · Issue #17346 · npm/npm · GitHub and it mentions to use npm config set unsafe-perm true && npm install. I have a very similar issue from what I can tell, but it’s not using CircleCI (and unfortunately not yet resolved). For what it’s worth you may want to compare your NPM and Node versions as well.

1 Like