“ path”参数必须为字符串类型。接收到的类型未定义(FS模块Node.js)

时间:2019-11-26 10:37:54

标签: javascript node.js macos

我正在尝试使用nodejs中的fs模块检查文件是否存在。请参考以下代码:

var path = require('path');
let customXmlPath = (process.platform == 'darwin' ? path.join(__dirname, '../../MacOS/service/custom.xml') : path.join(__dirname, '../../service/custom.xml'));
if (isDevelopment) {
    customXmlPath = (process.platform == 'darwin' ? path.join(__dirname, '../../../build/bin/darwin/release/custom.xml') : path.join(__dirname, '../../../build/bin/msvc/release/custom.xml'));
}
console.log(customXmlPath);

const fs = require('fs');
let productDisplayName = "xxxxxx";
try {
    fs.accessSync(customXmlPath, fs.F_OK);
    let file = fs.readFileSync(customXmlPath, "utf8");
    var xmlr = require('../../../shared/electronUIshared/xmlreader.js').XMLReader;
    var datastore = xmlr.parse('config', file);
    if(datastore.getTotalCount() > 0)
    {
        productDisplayName = datastore.getRecordAt(0).get('productname');
    }
}
catch(err) {
    console.log("custom.xml not found or is corrupted. Using display name as xxxxxx")
}

在Windows中,它工作正常,但在Mac中,它在fs.acessSync中引发错误:“ path”参数必须为字符串类型。收到的类型不确定。

path.join返回正确的路径

输出:

/Users/nikhell/Documents/Codelathe/Workspace/cl-fc-client/build/bin/darwin/release/custom.xml
App threw an error during load
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
    at validateString (internal/validators.js:105:11)
    at Object.join (path.js:1037:7)
    at eval (webpack:///./src/background.ts?:70:21)
    at Module../src/background.ts (/Users/nikhell/Documents/Codelathe/Workspace/cl-fc-client/clouddrive2service/ui/dist_electron/index.js:6094:1)
    at __webpack_require__ (/Users/nikhell/Documents/Codelathe/Workspace/cl-fc-client/clouddrive2service/ui/dist_electron/index.js:20:30)
    at eval (webpack:///multi_./src/background.ts?:1:18)
    at Object.0 (/Users/nikhell/Documents/Codelathe/Workspace/cl-fc-client/clouddrive2service/ui/dist_electron/index.js:6201:1)
    at __webpack_require__ (/Users/nikhell/Documents/Codelathe/Workspace/cl-fc-client/clouddrive2service/ui/dist_electron/index.js:20:30)
    at /Users/nikhell/Documents/Codelathe/Workspace/cl-fc-client/clouddrive2service/ui/dist_electron/index.js:84:18
    at Object.<anonymous> (/Users/nikhell/Documents/Codelathe/Workspace/cl-fc-client/clouddrive2service/ui/dist_electron/index.js:87:10)

如果我删除文件扩展名,它不会引发错误,但是找不到文件。为什么文件扩展名在Mac中造成问题?

0 个答案:

没有答案