How to fix npm ERR! code ENOENT npm ERR! prepublish-only No files to publish in npm

npmINTERMEDIATEHIGHUpdated Dec 5, 2025· 165 views

This error occurs when npm publish cannot find any files to include in your package tarball. npm uses the files field in package.json as an allowlist, .npmignore as a denylist, or .gitignore as a fallback. When all mechanisms filter out your code, the package ends up empty.

Quick fix

Fixes npm ERR! code ENOENT npm ERR! prepublish-only No files to publish

npm pack
tar -tzf your-package-1.0.0.tgz
  1. Test what will be published with npm pack
  2. Check and fix the files field in package.json
  3. Handle .npmignore and .gitignore correctly
  4. Ensure build script runs before publishing
npm — error
npm pack
tar -tzf your-package-1.0.0.tgz
npm ERR! code ENOENT
npm ERR! prepublish-only No files to publish

What this error means

This error occurs when npm publish (or npm version with prepublishOnly script) cannot find any files to include in your package tarball. npm uses three mechanisms to determine what gets published: 1. **files field** (allowlist) - only listed files are published 2. **.npmignore** (denylist) - excludes files from publication 3. **.gitignore** (fallback denylist) - used if no .npmignore exists When all mechanisms filter out your actual code, the package ends up empty except for metadata files like package.json and README. The "ENOENT" (Error: NO ENTry) code indicates that required file(s) do not exist in the paths npm is looking for.

Typical symptoms

  • npm publish or npm version fails with 'ENOENT: no such file or directory'
  • Published package on npm registry contains only README, LICENSE, and package.json
  • Build script output (like dist/ or lib/) doesn't appear in the published package
  • npm pack produces a tarball with minimal files
  • Installing your package provides only metadata, not actual code

Common causes

  1. files field in package.json doesn't include your compiled output directory
  2. .gitignore excludes build artifacts (dist/, lib/) and no .npmignore exists to override
  3. .npmignore file exists but doesn't properly include necessary directories
  4. prepublishOnly or build script generates files but package.json files field points to non-existent paths
  5. Build step isn't run before npm publish in CI pipeline
  6. Main entry point in package.json references a file that doesn't exist

How to fix "npm ERR! code ENOENT npm ERR! prepublish-only No files to publish"

1Test what will be published with npm pack

Before publishing, run npm pack to generate a test tarball and inspect its contents:

bash
npm pack
tar -tzf your-package-1.0.0.tgz

This shows exactly which files will be in your published package. If your code files are missing, the ignore/files configuration is wrong.

2Check and fix the files field in package.json

If you have a files array in package.json, verify it includes your compiled output:

json
{
  "name": "my-package",
  "main": "dist/index.js",
  "files": [
    "dist",
    "README.md",
    "LICENSE"
  ]
}

The files field is an allowlist—only files you explicitly list will be published. Common mistake: listing 'src' when you meant 'dist'.

3Handle .npmignore and .gitignore correctly

The ignore files work as a denylist:

- If .npmignore exists, npm uses ONLY .npmignore (not .gitignore)
- If .npmignore doesn't exist, npm falls back to .gitignore
- They are NOT merged

If your .gitignore excludes build artifacts (dist/, lib/), add an .npmignore that doesn't:

bash
node_modules
.git
src
test
*.log
.env

Or use the files field in package.json instead.

4Ensure build script runs before publishing

Add or verify a prepublishOnly script in package.json:

json
{
  "scripts": {
    "build": "tsc",
    "prepublishOnly": "npm run build"
  }
}

The prepublishOnly lifecycle hook runs before npm publish but NOT when someone installs your package. This ensures dist/ is populated before npm tries to pack it.

5Verify main and exports fields reference built files

Check that package.json's main field points to a file that will exist after build:

json
{
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.js"
    }
  }
}

If main points to src/index.js but you only ship dist/, users won't find the entry point.

Advanced notes

Files Always Included: Regardless of files, .npmignore, or .gitignore, npm always includes: package.json, README, CHANGES/CHANGELOG/HISTORY, LICENSE/LICENCE, NOTICE, and the file specified in the main field.

files Field Priority: The files field is an allowlist that overrides both .npmignore and .gitignore. Files listed in files cannot be excluded by ignore rules.

CI/CD Pitfall: When publishing from CI systems with semantic-release, the build step must complete before publish. If your build step fails silently, the publish step executes with no built artifacts.

Testing Locally: Always run npm pack locally before npm publish. Use tar -tzf to verify contents.

Frequently asked questions

What does "npm ERR! code ENOENT npm ERR! prepublish-only No files to publish" mean?
This error occurs when npm publish (or npm version with prepublishOnly script) cannot find any files to include in your package tarball. npm uses three mechanisms to determine what gets published: 1. files field (allowlist) - only listed files are published 2.
How do I fix "npm ERR! code ENOENT npm ERR! prepublish-only No files to publish"?
This error occurs when npm publish cannot find any files to include in your package tarball. npm uses the files field in package.json as an allowlist, .npmignore as a denylist, or .gitignore as a fallback. When all mechanisms filter out your code, the package ends up empty.
What causes "npm ERR! code ENOENT npm ERR! prepublish-only No files to publish"?
files field in package.json doesn't include your compiled output directory .gitignore excludes build artifacts (dist/, lib/) and no .npmignore exists to override .npmignore file exists but doesn't properly include necessary directories
Is the "npm ERR! code ENOENT npm ERR! prepublish-only No files to publish" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading