title: TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
body: <p>I wanted to make a script to add a new rule to angular webpack app, as below.Some times the code executes partially, some times it give erorr.</p>
  
  <pre><code>const fs = require('fs');
  const commonCliConfig = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js';
  const pug_rule = "\n{ test: /\\.pug$/, loader: ['raw-loader' , 'pug-html-loader' ]},";
  var configText = "";
  fs.readFile(commonCliConfig, function(err, data) {
      if (err) throw err;
      configText = data.toString();
      if (configText.indexOf(pug_rule) &gt; -1) { return; }
      const position = configText.indexOf('rules: [') + 8;
      const output = [configText.slice(0, position), pug_rule, configText.slice(position)].join('');
      const file = fs.openSync(commonCliConfig, 'r+');
      fs.writeFile(file, output);
      fs.close(file);
  });
  
  
  Terminal node pug-rule.js
  fs.js:148
      throw new ERR_INVALID_CALLBACK();
      ^
  
  TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
      at makeCallback (fs.js:148:11)
      at Object.fs.close (fs.js:520:20)
      at path/pug-rule.js:18:5
      at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:422:3)
  </code></pre>