Eliseo Soto
Eliseo Soto

Reputation: 1302

Get processor architecture from Node

I know that I can get the OS details using process.platform or with the os Module, but I'd also like to know if the OS is a 32 or 64 bit flavor.

Is that possible?

Upvotes: 15

Views: 18377

Answers (4)

Ângelo Polotto
Ângelo Polotto

Reputation: 9531

You simply run on your terminal this code:

node -e "console.log(process.arch)"

This will return the architecture of your Node build. It's very useful to check if you are running inside Rosetta 2 on MacOS.

Upvotes: 14

Luis Cuende
Luis Cuende

Reputation: 371

It's far easier than that, just use process.arch. It'll return x64 or x32 depending on the architecture.

Upvotes: 24

errordeveloper
errordeveloper

Reputation: 6912

I would use process.env['MACHTYPE'] or process.env['HOSTTYPE']. If either are undefined, then check with uname -m (that is supposed to work on all POSIX systems, though the output can be any string in fact, see uname(1P)).

var exec = require('child_process').exec, arch;

exec('uname -m', function (error, stdout, stderr) { 
  if (error) throw error;
  arch = stdout;
});

Well, may be it's not quite as brief as you wish, however you would probably find a plenty of shell scripts which do just that for whatever system you are running on and you can just run such script instead. That way you will have just one thing to run and check the output of from your Node app.

Another solution would be to write a module for Node in C++ which would check for low-level system features, including the endianess and whatever else one might need to check. There plenty of example on how to check such things.

Upvotes: 4

Dobes Vandermeer
Dobes Vandermeer

Reputation: 8820

If you know you are running in a linux-like system you can use the process module to run 'uname -i'. If the output contains '64' there's a good chance you are on a 64-bit system.

This won't always work.

Any particular reason you want to detect this? Perhaps another approach would be better.

Upvotes: 0

Related Questions