How to append text to a file using gulp?

I want to create a task in gulpfile.js that will append some text on a file. I do not want to use replace function as there is no particular string on the file that needs to be replaced.

1 Answers

If you want to append text on file at the top, you can use gulp-header package for that. You can install and use this as below

npm install --save-dev gulp-header

Now you can use it in your gulpfile.js

var header = require('gulp-header');


gulp.src('./app/filename.html')
  .pipe(header('Text to append'))
  .pipe(gulp.dest('./dist/'))

If you want to append text at the bottom of the file you can use gulp-footer. Below is the installation and usage information.

To Install gulp-footer in your project use npm command

npm install --save-dev gulp-footer

Now you can use gulp-footer in your gulpfile.js

var footer = require('gulp-footer');
 
gulp.src('./app/filename.html')
  .pipe(footer('Text to append'))
  .pipe(gulp.dest('./dist/'))

You can also use .replace() function of javascript to replace some text from a file and add you text in place of it

Ask question now
Never leave your website again in search of code snippets by installing our chrome extension.