Ah, you've run into a classic—and very frustrating—problem with packages that have a build step (e.g., TypeScript, Babel).
The core issue is that npm install <local_path> and npm link install the package based on its source code, but the package.json is configured to point to files that only exist after the build is complete.
Here is a step-by-step guide to fixing this "terrible repo" so you can install it locally. You'll be editing its package.json.
The Problem Explained
bin points to a non-existent file: The package.json might say "bin": "dist/cli.js", but the dist folder isn't in the source control and is only created by a build script.
- Build folder (
dist, build, etc.) isn't included: The files array in package.json (which tells npm what to bundle when publishing) is missing the build output directory.
The Solution: Make the Package "Installation-Aware"
You need to tell the package to build itself before it gets packed or installed. You do this using a special npm lifecycle script.
Step 1: Add a prepare Script
The prepare script is a lifesaver. It runs automatically in several key situations:
- Before the package is packed (via
npm pack).
- Before the package is published (via
npm publish).
- When you
npm install a local directory.
- When you
npm install a git repository.
In the local package's package.json, find the scripts section. Add a prepare script that runs the existing build command.
// In the package.json of the project you are trying to install
"scripts": {
"build": "tsc", // Or "babel src -d dist", "vite build", etc.
"prepare": "npm run build"
},
Note: prepublishOnly is an older alternative that also works for this, but prepare is the modern standard.
Step 2: Include the Build Output in the Package
Now, make sure the build folder (e.g., dist, build, lib) is included when the package is packed. Add it to the files array in package.json. This tells npm, "When you bundle this up, make sure to include this folder."
// In the package.json of the project you are trying to install
"files": [
"dist", // <--- ADD THIS
"README.md"
],
Step 3: Verify main and bin Paths
Finally, ensure the main, bin, and other entry point fields point to the correct files inside the build folder.
// In the package.json of the project you are trying to install
"main": "dist/index.js", // The entry point for `require()`
"bin": {
"my-cli-tool": "dist/cli.js" // The executable script
},
Putting It All Together: Before & After
Here’s a complete example of a fixed package.json:
BEFORE (The broken version):
{
"name": "my-broken-package",
"version": "1.0.0",
"main": "dist/index.js",
"bin": "dist/cli.js",
"scripts": {
"build": "tsc"
}
// "files" array is missing or doesn't include "dist"
}
AFTER (The fixed version):
{
"name": "my-fixed-package",
"version": "1.0.0",
"main": "dist/index.js", // Correctly points to build output
"bin": "dist/cli.js", // Correctly points to build output
"files": [
"dist" // Crucially includes the build output
],
"scripts": {
"build": "tsc",
"prepare": "npm run build" // <-- THE MAGIC SCRIPT
}
}
Your New, Corrected Workflow
After making these changes to the local package, you can now install it correctly.
- Navigate to your target project (the one that needs this local package).
- Run the install command using the local path.
# This will now work!
# npm will first run the "prepare" script in the local package,
# which runs "npm run build", creating the `dist` folder.
# Then, it will proceed with the installation.
npm install ../path/to/the-fixed-local-package
Your bin script will now be correctly linked in the target project's node_modules/.bin, and require('my-fixed-package') will resolve correctly.
For npm link: The prepare script does not run on npm link. You will need to manually run the build in the source directory first.
# In the source package directory
npm run build
npm link
# In the target project directory
npm link my-fixed-package