F:\node_modules\ini\ini.js:67
const lines = str.split(/[\r\n]+/g)
^
TypeError: str.split is not a function
at Object.decode (F:\node_modules\?[4mini?[24m\ini.js:67:21)
at F:\trash.js:79:22
at Array.forEach (<anonymous>)
at Object.<anonymous> (F:\\trash.js:78:11)
?[90m at Module._compile (node:internal/modules/cjs/loader:1097:14)?[39m
?[90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)?[39m
?[90m at Module.load (node:internal/modules/cjs/loader:975:32)?[39m
?[90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)?[39m
?[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)?[39m
?[90m at node:internal/main/run_main_module:17:47?[39m
Node.js v17.1.0
var fs=require('fs');
var ini=require('ini');
var path='G:\test.ini'
var info = ini.parse(fs.readFileSync(path));
fs.readFileSync()
返回的不是一个字符串,而是一个Buffer对象。打印结果如下:
<Buffer 5b 54 72 61 73 68 20 49 6e 66 6f 5d 0a 50 61 74 68 3d 46 3a 5c 50 72 65 5f 43 6f 64 69 6e 67 5c 50 72 65 5f 57 65 62 53 74 6f 72 6d 5f 50 72 6f 6a 65 ... 58 more bytes>
而ini.parse()
方法应该传入一个字符串参数。
利用toString()
方法将Buffer结果转换成字符串。
var fs=require('fs');
var ini=require('ini');
var path='G:\test.ini'
var info = ini.parse(fs.readFileSync(path).toString());