Package.json preinstall not running on build

My site names are playbook-argon.jeffmcaleer.com and playbook-cobalt.jeffmcaleer.com

I need a preinstall step to be run before npm install gets run. I have added a preinstall script to my package.json. The preinstall script makes a modification to package.json itself as well as package-lock.json, to remove a reference to a private npm repo that Netlify doesn’t have access to. This reference is not needed when hosted on Netlify, but IS needed elsewhere so I can’t just remove it.

In the playbook-argon project, this script runs. It modifies both files, then everything works beautifully.

In the playbook-cobalt project, which is configured identically to playbook-argon, the script does not run. If I look at the build process, I can see that it goes straight to npm install and my console.log statements from the preinstall script are never run.

I’m kind of at a loss as to what I can do to make the preinstall script run. I’ve been through the docs and I can’t figure anything out.

package.json

{
    "name": "playbook-cobalt",
    "version": "1.0.0",
    "private": true,
    "scripts": {
        "preinstall": "node setupEmulation.js",

setupEmulation.js

var fs = require("fs");

console.log("Checking to see if any patching is needed for emulation mode");
var filename;

if (process.env.EMULATION) {
    filename = "package.json";
    try {
        // Read File
        console.log(`Patching ${filename}`);
        var data = fs.readFileSync(filename, "utf8");
        var package = JSON.parse(data);

        // Modify File
        package.dependencies["@avispl/hermes-endpoint"] =
            "file:./src/assets/emulation/hermes-endpoint";

        // Write File
        fs.writeFileSync(filename, JSON.stringify(package, null, 4));
        console.log(`${filename} patching complete`);
    } catch {
        console.log(`Error patching ${filename}`);
    }

    filename = "package-lock.json";
    try {
        // Read File
        console.log(`Patching ${filename}`);
        var data = fs.readFileSync(filename, "utf8");
        if (!data) {
            console.log(`${filename} not found, no patching necessary`);
        } else {
            var packageLock = JSON.parse(data);

            // Modify File
            packageLock.packages[""].dependencies["@avispl/hermes-endpoint"] =
                "file:./src/assets/emulation/hermes-endpoint";
            if (packageLock.packages) {
                Object.keys(packageLock.packages).forEach((package) => {
                    if (package.includes("hermes-endpoint")) {
                        delete packageLock.packages[package];
                    }
                });
            }

            if (packageLock.dependencies) {
                Object.keys(packageLock.dependencies).forEach((dependency) => {
                    if (dependency.includes("hermes-endpoint")) {
                        delete packageLock.dependencies[dependency];
                    }
                });
            }

            // Write file
            fs.writeFileSync(filename, JSON.stringify(packageLock, null, 4));
            console.log(`${filename} patching complete`);
        }
    } catch {
        console.log(`Error patching ${filename}`);
    }
} else {
    console.log("Environment is not emulated, no patching required");
}

The console.log statement at the top of setupEmulation.js shows in the build logs of playbook-argon, but not in the build logs of playbook-cobalt.

I’m open to other ways to modify package.json before anything gets installed. I just need to make it reference a local file instead of a private npm repo before the install happens.

Partially answered here: Modify package.json before npm install is run - #9 by hrishikesh