ryu*_*yuu 5 javascript node.js angular
我想创建一个脚本来向angular webpack app添加一个新规则,如下所示.有时代码执行部分,有时它会给出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)
Run Code Online (Sandbox Code Playgroud)
小智 12
fs.writeFile(...)需要第三个(或第四个)参数,这是一个在操作完成时调用的回调函数.您应该提供回调函数或使用fs.writeFileSync(...)
有关详细信息,请参阅node fs docs.