DEV Community

Cover image for `npm config get` Knows Which Layer Won. It Just Won't Tell You.
AheadKit
AheadKit

Posted on

`npm config get` Knows Which Layer Won. It Just Won't Tell You.

fluffydesk left a comment on my last piece about .npmrc layers, and it named the exact check I had been skipping. I had shown that a repo's own .npmrc beats your user config: my user file says ignore-scripts=true, the repo says false, and the repo wins: the install hook runs. The check I recommended was npm config list, run inside the repo before installing, because it names the winning file.

fluffydesk asked about the check people actually run instead. npm config get ignore-scripts answers with the merged value from wherever you are standing: false inside the repo, true from home. Same command, two answers, and no mention that a vote happened. Worse, --location=user does not rescue it. The flag is accepted, and the command still returns false.

So I ran the comparison. Same setup as last time: user config true, project config false, a test package with an install hook that leaves a marker file when it runs. Thirteen commands, three locations, every answer recorded. The flag does nothing. That is the claim, and I know it sounds like a small thing, but a flag that is accepted and ignored is worse than a flag that errors, because it lets you walk away believing you asked one layer a question and got its answer.

Problem 1: the same command gives two answers, and neither says why

Inside the repo, npm config get ignore-scripts prints false. From home, the same command prints true. From the folder above the repo, true again. All three answers are the effective value for that spot, and all three are correct in the narrow sense. The install proves it: the hook ran in the repo and did not run in the clean folder. But none of them says which layer won. If you run the check from home and read true, you walk into the install believing hooks are off. They are not. The command answered the question you typed and hid the question you meant: who decided this?

That is the trap in its plainest form. The value is right and the understanding is wrong, and the understanding is the part that costs you.

Problem 2: --location=user is accepted, and then ignored

This is the one fluffydesk called, and they called it exactly. Inside the repo, npm config get ignore-scripts --location=user returns false. Not true, the user layer's value. false, the project-overridden value. The flag does not error, does not warn, does not print anything extra. It just takes your request to hear from the user layer and hands you the merged answer anyway.

I ran it from all three spots to be sure. Home: true. Run root: true. Inside the repo: false, with the flag on. The flag changes nothing about the value anywhere. If you believed --location=user was a way to interrogate one layer at a time, you were never interrogating anything. You were asking the same merged question with extra words attached.

A flag that errors is honest. A flag that is silently ignored is a trap with good manners.

Problem 3: --location=project outside a repo answers instead of refusing

The other direction is worse, because it invents an answer. From home, where no project config exists at all, npm config get ignore-scripts --location=project returns true, the user value, with no warning that there is no project layer to ask. From the run root, same thing: true, silence, no complaint.

Read that as a person would. You asked the project layer a question, and it answered. Except it did not. There is no project layer there. The command reached past the layer you named, took the next value down the chain, and handed it to you dressed as a project answer. If you are writing a check that runs in CI, in a directory you did not verify first, that silent fallback is the difference between "the repo has no override" and "I never actually looked at the repo".

Problem 4: only one command names the winner, and it is not the one with JSON

Plain npm config list, run from inside the repo, is the only command in the whole matrix that tells you which layer won. It prints the user value true stamped ; overridden by project, then names the winning file by its full path, ; "project" config from .../repo/.npmrc, and then the effective false. That stamp is the whole story in one line: a vote happened, here is who lost, here is the file that beat them.

npm config list --json shows "ignore-scripts": false and names no source file at all. So the machine-readable version is the one that drops the one fact you need, and the human-readable version is the one that keeps it. If your tooling parses the JSON, it gets the value and loses the provenance in the same breath.

And config list only tells the truth from inside the repo. From the parent folder, the project layer is invisible: true, user file named, no project section. Same command, same honesty, but the answer depends on where you stand, and standing in the wrong place is the easiest mistake in the whole chain.

Command Inside repo/ From home From outside Names the winner?
npm config get ignore-scripts false true true No
... --location=user false true true No
... --location=project false true true No
npm config list false true true Yes
npm config list --json false — — No

Every command took about 0.2 seconds. The check costs nothing. The wrong check costs a hook running on a machine you thought was protected.

Problem 5: the value was never wrong, which is why this keeps biting

Here is the part that keeps me from calling the whole thing broken. npm config get gets the effective value right every time. The install confirms it: hook ran in the repo at 00:14:01 UTC, no marker in the clean folder. If all you need is "what will the install do", get answers correctly from the right directory.

But "what will the install do" is not the question people are asking when they type get. They are asking "am I safe", and safety is a question about layers, not values. A true from home is not a promise about the repo. A false inside the repo is not an explanation of who set it. The command gives you the outcome and withholds the cause, and the cause is the thing you need when the outcome surprises you at 2 a.m. in a CI log.

So the rule I am keeping is narrow, and it is the same shape as the last piece: cd into the repo, then plain npm config list, not --json. Not because get lies. It does not. But because it answers the question you typed while the question you meant goes unasked. And if you ever catch yourself adding --location to make the answer more specific, stop. It is accepted, it changes nothing, and the silence afterwards is the command agreeing with a premise it never checked.

What check have you run that reported the loser without mentioning a vote happened? I would like to hear which command it was, and whether you found out from the output or from the hook running anyway.


P.S. Everything I write stays free — that part doesn't change. But if this one was worth something to you, you can buy me a coffee ☕ (one-time or monthly, both work). It covers the API credits and hosting behind these experiments — and honestly, it's also the nudge that gets the next one started on the evenings I'd otherwise skip it. — Salam

Buy me a coffee

AheadKit on Gumroad — Beginner-Friendly Books & Packs

Top comments (0)