如何在Node模块更新时用Gulp重启应用?含npm link场景配置
Got it, let's tackle your problem step by step. You're using npm link for a dependency and want your app to restart automatically when that linked package updates, right? Here's how to set this up with Gulp and nodemon, plus how to properly listen for changes in your linked Node module.
By default, nodemon ignores the node_modules directory entirely—which makes sense for most cases, but since your linked package is a symlink in node_modules, nodemon won't pick up changes there unless you explicitly tell it to.
You have two reliable options to make nodemon detect changes in your linked package:
Option A: Watch the Symlinked Path in node_modules
If your linked package is named my-shared-package, update your nodemon config to include it in the watch list and exclude other node_modules content:
gulp.task('serve', function () { return nodemon({ script: 'dist/index.js', // Your compiled app entry file ext: 'ts js', // Watch TypeScript and JavaScript files ignore: ['node_modules/**', '!node_modules/my-shared-package/**'], // Ignore all node_modules except your linked package watch: ['src/**/*.ts', 'node_modules/my-shared-package/**/*.js'], // Watch your source TS and the linked package's compiled JS tasks: ['build'] // Run your build task when changes are detected }).on('restart', function () { log('App restarted due to linked package update!'); }); });
Option B: Watch the Actual Source Directory (Better for TS Linked Packages)
If your linked package is a TypeScript project that compiles locally, it's more reliable to watch its source directory directly instead of the symlinked JS. Just add the absolute path to the linked package's source:
gulp.task('serve', function () { const linkedPackageSourcePath = '/path/to/your/linked-package/src'; // Replace with your actual path return nodemon({ script: 'dist/index.js', ext: 'ts js', ignore: ['node_modules/**'], // We'll watch the actual source path instead watch: ['src/**/*.ts', linkedPackageSourcePath + '/**/*.ts'], tasks: ['build'] }).on('restart', function () { log('App restarted due to linked package source update!'); }); });
Here's how to integrate this into your existing TypeScript-based Gulp setup:
var gulp = require("gulp"); var ts = require("gulp-typescript"); var uglify = require("gulp-uglifyes"); var gulpif = require("gulp-if"); var log = require("fancy-log"); var nodemon = require('gulp-nodemon'); var tsProject = ts.createProject("tsconfig.json"); // Build task: Compile TypeScript to JS gulp.task("build", () => { return tsProject.src() .pipe(tsProject()) .js.pipe(gulpif(process.env.NODE_ENV === 'production', uglify())) .pipe(gulp.dest("dist")); }); // Serve task with nodemon and linked package watching gulp.task("serve", gulp.series("build", function () { // Replace with your linked package name or source path const linkedPackageName = 'my-shared-package'; // Uncomment below and replace path if using Option B // const linkedPackageSourcePath = '/path/to/your/linked-package/src'; return nodemon({ script: 'dist/index.js', // Your app's entry point after build ext: 'ts js', // For Option A: Ignore all node_modules except your linked package ignore: ['node_modules/**', `!node_modules/${linkedPackageName}/**`], // For Option A: Watch your source TS and linked package JS watch: ['src/**/*.ts', `node_modules/${linkedPackageName}/**/*.js`], // For Option B: Replace ignore and watch with these lines // ignore: ['node_modules/**'], // watch: ['src/**/*.ts', linkedPackageSourcePath + '/**/*.ts'], tasks: ['build'] }).on('restart', function () { log('✅ App restarted because linked package or source code changed!'); }); })); // Set default task to serve gulp.task("default", gulp.series("serve"));
- Symlink Limitations: Some OS/file systems struggle with watching symlinked directories. If you run into issues, switch to Option B (watching the actual source directory of your linked package) instead.
- Linked Package Auto-Compile: If your linked package is TypeScript, make sure it has its own auto-compile setup (like
tsc --watch). Otherwise, your main app won't pick up changes until you manually build the linked package. - Nodemon Ignore Logic: The
!in the ignore array negates the rule—so!node_modules/my-shared-package/**tells nodemon not to ignore that specific directory, even though it's insidenode_modules.
内容的提问来源于stack exchange,提问作者hussain

