Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
FREEDOM251

Freedom251 › Blog › JavaScript

Does npm Publish Ignore .gitignore? The .npmignore Override Rule

By Freedom251 Team · · 2 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Yes—when both files are present, npm uses .npmignore and ignores .gitignore for package exclusions. If .npmignore is absent, npm uses .gitignore instead. An empty .npmignore is still an override: Git-only ignore patterns no longer keep files out of the package.

What happens when both ignore files exist?

npm does not combine the two files. It uses .npmignore when that file exists; .gitignore supplies npm’s ignore rules only when .npmignore is absent. npm documents this precedence in its npm publish file-inclusion rules and its guide to keeping files out of a package.

As an Amazon Associate I earn from qualifying purchases.

Package setup Effect on npm’s package exclusions
.gitignore present; .npmignore absent npm uses the patterns in .gitignore.
Both files present npm uses .npmignore; it does not use .gitignore for package exclusions.
.npmignore present but empty No ignore patterns come from that file, so patterns found only in .gitignore no longer exclude files through the ignore-file fallback.
package.json contains a files field The field specifies paths to include. npm’s package.json documentation says files included through this field cannot be excluded by either ignore file.

Why an empty .npmignore can change the package

An empty .npmignore does not mean “inherit the Git ignore rules.” Its existence is enough to prevent npm from using .gitignore as the fallback. As a result, files excluded only by Git rules—such as local build output or configuration files—may become eligible for packaging.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

npm applies ignore-file rules in subdirectories as well as at the package root. The patterns use .gitignore-style syntax, including glob patterns and ! negation. Some paths are treated specially: npm’s documentation says certain paths are automatically excluded, while files such as package.json, README files, and license files are always included. Check the documentation for the npm CLI version you use for the exact default list.

How the files field affects inclusion

The files field in package.json is an inclusion allowlist: it names paths to include in the package. npm states that files included through this field cannot be excluded by .npmignore or .gitignore. Therefore, reviewing ignore files alone is not enough if the package also has a files entry. Check how the allowlist and ignore patterns interact by inspecting the package npm actually creates.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the archive before publishing

  1. Review the packaging rules. From the package directory, inspect .npmignore, .gitignore, and the files field in package.json.
  2. Create a local package archive. Run npm pack in that directory. npm describes this as the local packaging step for determining which files would be uploaded.
  3. Inspect the archive’s file list. Confirm that the files you intend to publish are present and private or otherwise unintended files are absent.
  4. Publish only after checking the contents. The archive preview, rather than the apparent intent of an ignore file, is the practical check on what will be packaged.

npm recommends checking package contents because a package directory can expose more files than its maintainers expect. See the npm developer guidance and npm publish documentation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JavaScriptNode.jsnpmnpm publishPackage Management

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.