最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError [ERR_INVALID_CALLBACK]: Callback must be a function - Stack Overflow

programmeradmin4浏览0评论

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.

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) > -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)

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.

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) > -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)
Share Improve this question edited May 20, 2018 at 22:56 Kalamarico 5,66622 gold badges56 silver badges73 bronze badges asked May 20, 2018 at 19:22 ryuuryuu 1191 gold badge2 silver badges7 bronze badges 3
  • @Kalamarico It looks like the error is occurring when you try to close the file descriptor (fs.close(file)). Could this be happening because you are doing fs.writeFile(file, output) asynchronously in the previous line? Might be worth trying to use fs.writeFileSync or adding a callback on the fs.writeFile / fs.close and see if either of them are throwing an error in the callback. – jrasm91 Commented May 21, 2018 at 5:50
  • Tried Dosent Work – ryuu Commented May 21, 2018 at 14:21
  • @Jason I'm not the owner of the question, ryuu (he is the owner) said that your proposal did not work – Kalamarico Commented May 27, 2018 at 21:08
Add a comment  | 

2 Answers 2

Reset to default 13

fs.writeFile(...) requires a third (or fourth) parameter which is a callback function to be invoked when the operation completes. You should either provide a callback function or use fs.writeFileSync(...)

See node fs docs for more info.

Try this.

 fs.readFile('readMe.txt', 'utf8', function (err, data) {
  fs.writeFile('writeMe.txt', data, function(err, result) {
     if(err) console.log('error', err);
   });
 });
发布评论

评论列表(0)

  1. 暂无评论