Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Buffer #886

Closed
lokrajahuja opened this issue Apr 10, 2020 · 4 comments

Comments

@lokrajahuja
Copy link

lokrajahuja commented Apr 10, 2020

Calling fs.stat function with buffer as a path parameter. getting the following error after packaging the app using pkg.

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Buffer
at validateString (internal/validators.js:117:11)
at Object.dirname (path.js:1128:5)
at isRootPath (pkg/prelude/bootstrap.js:168:26)
at Object.fs.readdir (pkg/prelude/bootstrap.js:850:18)
at onStat (/snapshot/node-dx/node_modules/node-dir/lib/paths.js:160:12)
at FSReqCallback.oncomplete (fs.js:167:5) {
code: 'ERR_INVALID_ARG_TYPE'
}

fs.stat can accept string | Buffer | URL

After debugging found issue in isRootPath function of the bootstrap.js.

function isRootPath (p) {
if (p === '.') p = require('path').resolve(p);
return require('path').dirname(p) === p;
}

dirname function of path module only accepts string not buffer. After making changes to convert buffer into the string. everything was working.

But this hack will not work in the CI environment. Please help us to resolve this issue asap.

@lokrajahuja
Copy link
Author

@leo @igorklopov Can you please help us to resolve this issue.

@lokrajahuja
Copy link
Author

@igorklopov I am still facing the same issue. fs.readdir method of bootstrap.js is getting called.

fs.readdir = function (path, options_) {
var isRoot = isRootPath(path);

in this method, the path is coming as a buffer and in isRootPath is requiring path module and calling the dirname method with buffer.

@lokrajahuja
Copy link
Author

lokrajahuja commented Apr 12, 2020

@igorklopov Here is the sample dummy code.

pkgFs.js file code

`const fs = require('fs');

const onDirRead = function (err, list) {
console.log(err);
console.log(list);
};
const readDir = () => {
const bufdir = Buffer.from('/Users/lokraj/practice/');
fs.readdir(bufdir, {
encoding: 'buffer'
}, onDirRead);
}

readDir();

module.exports = readDir;`

package creation code.

const { exec } = require('pkg'); exec(['pkgFs.js', '--target', 'node12-macos-x64', '--output', 'package']);

Error is following

internal/validators.js:117
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Buffer
at validateString (internal/validators.js:117:11)
at Object.dirname (path.js:1128:5)
at isRootPath (pkg/prelude/bootstrap.js:168:26)
at Object.fs.readdir (pkg/prelude/bootstrap.js:850:18)
at readDir (/snapshot/practice/pkgFs.js)
at Object. (/snapshot/practice/pkgFs.js)
at Module._compile (pkg/prelude/bootstrap.js:1324:22)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14) {
code: 'ERR_INVALID_ARG_TYPE'
}

@igorklopov
Copy link
Contributor

@lokrajahuja Please try again, against master.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants