咨询npm本地路径包变更时自动重建/重新安装的实现方案
咨询npm本地路径包变更时自动重建/重新安装的实现方案
Hey there! I’ve run into this exact issue before—here are a few practical, no-fuss ways to get your project to automatically pick up changes from your local package:
Option 1: Use npm link + Watch Script in the Local Package
This is my go-to method because it skips repeated reinstalls and uses symlinks for near-instant updates:
- First, head to your local package directory and run
npm link—this creates a global symlink for your package. - Then jump to your main project directory and run
npm link <your-local-package-name>—now your project uses the symlinked version of the local package instead of a copied static version. - In your local package’s
package.json, add a watch script that rebuilds your code whenever files change. For example, if you’re using Babel to transpile source code:"scripts": { "build": "babel src --out-dir dist", "watch": "babel src --out-dir dist --watch" } - Start the watch script in your local package (
npm run watch), and any changes you make will trigger an immediate rebuild. Since the package is symlinked, your main project will automatically use the updated files—no reinstall required!
Option 2: File Watcher in the Main Project
If symlinks aren’t your vibe, you can set up a script in your main project to watch the local package and reinstall it on changes:
- First, install a lightweight file-watching tool like
chokidar-clias a dev dependency in your main project:npm install chokidar-cli --save-dev - Add a custom script to your main project’s
package.jsonthat watches the local package’s files and runsnpm installwhen changes are detected:"scripts": { "watch-local-package": "chokidar '../path-to-your-local-package/src/**/*' -c 'npm install ../path-to-your-local-package'" } - Run
npm run watch-local-package—now every time you modify files in the local package’ssrcdirectory, your main project will reinstall the package automatically.
A Quick Heads-Up
- If your local package has its own dependencies, make sure they’re either compatible with your main project or installed properly (symlinking might require you to run
npm installin the local package first). - If you’re using a bundler like Webpack or Vite in your main project, you might need to enable its watch mode too, so it picks up the updated package files and rebuilds your project.
备注:内容来源于stack exchange,提问作者Jamshidkhuja Burikhujaev
相关产品推荐
相关产品推荐

