You're dealing with a particularly messy situation where the build artifacts are gitignored but also not included in the npm package. Here's how to fix it:
The Core Problem
.gitignore excludes build/ → prevents build artifacts from being committed
No .npmignore and no files in package.json → npm uses .gitignore by default
Result: build/ directory is excluded from npm package
"files": ["build"]
Explicitly tells npm to include the build/ directory, overriding .gitignore
"prepare": "npm run build"
Ensures the build runs automatically during installation:
Before npm pack (for local installs)
Before npm publish
When installing from Git
For Immediate Testing
Build manually first:
bash
# In package directory
npm run build
Install in dependent project:
bash
# In consumer project
npm install /path/to/your-package
If You Can't Modify package.json (Workaround)
Create a temporary .npmignore file with:
code
!.npmignore
!build/
Then install with:
bash
# In consumer project
npm install /path/to/your-package --ignore-scripts
Important Notes
prepare script behavior:
Runs before packing
Build artifacts must exist before packaging completes
Won't run with --ignore-scripts flag
Bin file fix:
Ensure bin points to built file:
json
"bin": "./build/cli.js"
Git hygiene:
Keep build/ in .gitignore to prevent committing build artifacts
Verification
After changes, test with:
bash
# In package directory
npm pack --dry-run
Check file listing includes:
code
package.json
build/index.js
build/cli.js
...
This approach forces npm to include the built files while keeping your git history clean. The prepare script ensures the build always runs at the right time.