How to fix npm ERR! code ENOFILES npm ERR! No files to pack in npm
The ENOFILES error occurs when npm cannot find any files to include in the package tarball. This happens when the files field in package.json is misconfigured or .gitignore rules exclude all publishable content.
Quick fix
Fixes npm ERR! code ENOFILES npm ERR! No files to pack
# Run the build
npm run build
# Check output directory exists
ls -la dist/
# or
ls -la lib/- Check if build output exists
- Create an empty .npmignore file
- Configure the files field properly
- Add prepublishOnly script
# Run the buildnpm run build# Check output directory existsls -la dist/# orls -la lib/npm ERR! code ENOFILESnpm ERR! No files to pack
What this error means
This error means npm's file inclusion logic resulted in zero files being available to package. npm uses a combination of the "files" field in package.json, .npmignore, and .gitignore to determine what to include. When you run npm pack or npm publish, npm creates a tarball containing your package. If no files pass the inclusion filters, you get this error.
Typical symptoms
- npm publish fails with "No files to pack" before uploading
- npm pack creates an empty or near-empty tarball
- Package installs but contains no actual code files
- Build step runs successfully but publish fails
- Works locally but fails in CI
Common causes
- .gitignore blocking the dist/build output directory
- Empty or misconfigured files array in package.json
- Build step not executed before publishing
- Nested .gitignore files overriding root settings
- Using relative paths like ./dist instead of dist
How to fix "npm ERR! code ENOFILES npm ERR! No files to pack"
Verify your built files actually exist:
# Run the build
npm run build
# Check output directory exists
ls -la dist/
# or
ls -la lib/If dist/ is empty or missing, your build step failed or wasn't run.
The simplest fix—create an empty .npmignore to override .gitignore:
touch .npmignoreWhen .npmignore exists (even empty), npm ignores .gitignore rules entirely. This allows dist/ to be included even if it's in .gitignore.
Use the files field to explicitly whitelist what to publish:
{
"name": "my-package",
"version": "1.0.0",
"files": [
"dist",
"lib",
"README.md"
]
}Important:
- No ./ prefix (use dist, not ./dist)
- Directories include all subdirectories automatically
- package.json and README are auto-included
Ensure build runs automatically before publishing:
{
"scripts": {
"build": "tsc --outDir dist",
"prepublishOnly": "npm run build"
},
"files": ["dist"]
}npm automatically runs prepublishOnly before npm publish, so the build output will exist.
Always verify before publishing:
# Build first
npm run build
# Create tarball
npm pack
# Inspect contents
tar -tzf my-package-1.0.0.tgz
# Extract to verify
tar -xzf my-package-1.0.0.tgz
ls -la package/If the tarball only contains package.json, your files field or ignore patterns are wrong.
npm v9+ respects nested .gitignore files which can block your files:
# Find all .gitignore files
find . -name ".gitignore" -type f
# Check subdirectory gitignore contents
cat dist/.gitignoreIf a nested .gitignore blocks your files, remove it or use .npmignore at root level to override.
Advanced notes
The relationship between .gitignore, .npmignore, and files field:
1. If "files" exists in package.json, only listed items are included (plus auto-includes)
2. If .npmignore exists, it overrides .gitignore completely
3. If neither .npmignore nor "files" exist, npm uses .gitignore rules
4. Nested .gitignore files can override root settings in npm v9+
A complete working setup for TypeScript:
{
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": ["dist"],
"scripts": {
"build": "tsc",
"prepublishOnly": "npm run build"
}
}Frequently asked questions
- What does "npm ERR! code ENOFILES npm ERR! No files to pack" mean?
- This error means npm's file inclusion logic resulted in zero files being available to package. gitignore to determine what to include. When you run npm pack or npm publish, npm creates a tarball containing your package.
- How do I fix "npm ERR! code ENOFILES npm ERR! No files to pack"?
- The ENOFILES error occurs when npm cannot find any files to include in the package tarball. This happens when the files field in package.json is misconfigured or .gitignore rules exclude all publishable content.
- What causes "npm ERR! code ENOFILES npm ERR! No files to pack"?
- .gitignore blocking the dist/build output directory Empty or misconfigured files array in package.json Build step not executed before publishing
- Is the "npm ERR! code ENOFILES npm ERR! No files to pack" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚npm Official Documentation
- 📖npm pack Documentation(Official Docs)
- 💬Stack Overflow: npm(Stack Overflow)
- 🐙GitHub: npm(GitHub)