Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'trim' of undefined #22

Closed
ljubic opened this issue Oct 15, 2018 · 18 comments
Closed

TypeError: Cannot read property 'trim' of undefined #22

ljubic opened this issue Oct 15, 2018 · 18 comments

Comments

@ljubic
Copy link

ljubic commented Oct 15, 2018

I wanted to check this library out, but after using the example code you provided I am getting the following error:
TypeError: Cannot read property 'trim' of undefined

I have node v10 installed and using it on MacOS Mojave.

@sindresorhus
Copy link
Owner

Are you running the latest v10 version? If not I would try upgrading to the latest first.

@ljubic
Copy link
Author

ljubic commented Oct 15, 2018

Yes, I have v10.12.0 installed.

@sindresorhus
Copy link
Owner

Hmm, it must be failing at

const val = line.slice(pid.length + 1).trim();

Can you add console.log(stdout); on line 32 and share the output?

@ljubic
Copy link
Author

ljubic commented Oct 15, 2018

I already tried that, and stdout is undefined.

@ljubic
Copy link
Author

ljubic commented Oct 15, 2018

However, when I log the definition of stdout on line before it I get printed processes list:

console.log(await execFile('ps', [flags, `pid,${cmd}`], {maxBuffer: TEN_MEGABYTES}));

but for some reason is the:

{stdout} = await execFile('ps', [flags, `pid,${cmd}`], {maxBuffer: TEN_MEGABYTES});

undefined.

@ljubic
Copy link
Author

ljubic commented Oct 15, 2018

I tested with removing brackets around stdout and now it works:

const stdout = await execFile('ps', [flags, `pid,${cmd}`], {maxBuffer: TEN_MEGABYTES});

@ljubic
Copy link
Author

ljubic commented Oct 15, 2018

@sindresorhus Can I make the pull request for this? Want to make my first open source contribution. Can you just test it on Windows so I can know whether the corresponding windows function should be changed as well? Looking at previous commits, I would assume yes :)

@sindresorhus
Copy link
Owner

Are you sure you're actually running the correct version? Try console logging process.versions.

From Node.js docs:

If this method is invoked as its util.promisify()ed version, it returns a Promise for an Object with stdout and stderr properties. In case of an error (including any error resulting in an exit code other than 0), a rejected promise is returned, with the same error object given in the callback, but with an additional two properties stdout and stderr. - https://nodejs.org/dist/latest-v10.x/docs/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

@ljubic
Copy link
Author

ljubic commented Oct 15, 2018

Executing

  const { execFile } = require('child_process');
      const child = execFile('node', ['--version'], (error, stdout, stderr) => {
        if (error) {
          throw error;
        }
        console.log("version", stdout);
      });

gives the same node version v10.12.0 .

@ljubic
Copy link
Author

ljubic commented Oct 15, 2018

const util = require('util');
const execFile = util.promisify(require('child_process').execFile);
async function getVersion() {
  const { stdout } = await execFile('node', ['--version']);
  console.log(stdout);
}
getVersion();

stdout in this one is undefined.

@sindresorhus
Copy link
Owner

Definitely something weird with your Node.js version as the above example runs fine for me on v10.12.0.

@ljubic
Copy link
Author

ljubic commented Oct 15, 2018

I am using this boilerplate for electron app:
https://github.com/electron-react-boilerplate/electron-react-boilerplate

Could there in project settings be some defined option to use other node version? I see in travis.yml file that they specified node_js version 9, but I am not sure whether that have influence on it.

@sindresorhus
Copy link
Owner

So when you run the snippet, you run it in Electron and not pure Node.js? If so, it could be an Electron bug.

@ljubic
Copy link
Author

ljubic commented Oct 15, 2018

Yes, in Electron. I put that piece of code in main function.

@ljubic
Copy link
Author

ljubic commented Oct 16, 2018

@sindresorhus could the problem be that this boilerplate is using Electron 2.0.6? Electron 2.0.0 uses Node 8.6 and in Version they switch to v10.2.0.

@sindresorhus
Copy link
Owner

Could be, yes.

@ljubic
Copy link
Author

ljubic commented Oct 16, 2018

@sindresorhus is there any way to check whether the process has opened (not active) window? I am also using your active-win package, but need to get list of opened windows :)

@sindresorhus
Copy link
Owner

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

No branches or pull requests

2 participants