├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 22 14 | - 20 15 | - 18 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: actions/setup-node@v4 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type CpuArchitecture = 'arm64' | 'x64' | 'arm' | 'ia32' | 'mips' | 'mipsel' | 'ppc' | 'ppc64' | 'riscv64' | 's390' | 's390x'; 2 | 3 | /** 4 | Get the operating system CPU architecture. 5 | 6 | @example 7 | ``` 8 | import {systemArchitecture} from 'system-architecture'; 9 | 10 | // On ARM64 macOS 11 | console.log(await systemArchitecture()); 12 | //=> 'arm64' 13 | ``` 14 | */ 15 | export function systemArchitecture(): Promise; 16 | 17 | /** 18 | Get the operating system CPU architecture. 19 | 20 | @example 21 | ``` 22 | import {systemArchitectureSync} from 'system-architecture'; 23 | 24 | // On ARM64 macOS 25 | console.log(systemArchitectureSync()); 26 | //=> 'arm64' 27 | ``` 28 | */ 29 | export function systemArchitectureSync(): CpuArchitecture; 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import {promisify} from 'node:util'; 2 | import process from 'node:process'; 3 | import childProcess from 'node:child_process'; 4 | 5 | const execFilePromises = promisify(childProcess.execFile); 6 | 7 | export async function systemArchitecture() { 8 | const {arch, platform, env} = process; 9 | 10 | // Detect Node.js x64 binary running under Rosetta 2 on a ARM64 Mac. 11 | if (platform === 'darwin' && arch === 'x64') { 12 | const {stdout} = await execFilePromises('sysctl', ['-inq', 'sysctl.proc_translated']); 13 | return stdout.trim() === '1' ? 'arm64' : 'x64'; 14 | } 15 | 16 | if (arch === 'arm64' || arch === 'x64') { 17 | return arch; 18 | } 19 | 20 | if (platform === 'win32' && Object.hasOwn(env, 'PROCESSOR_ARCHITEW6432')) { 21 | return 'x64'; 22 | } 23 | 24 | if (platform === 'linux') { 25 | const {stdout} = await execFilePromises('getconf', ['LONG_BIT']); 26 | if (stdout.trim() === '64') { 27 | return 'x64'; 28 | } 29 | } 30 | 31 | return arch; 32 | } 33 | 34 | export function systemArchitectureSync() { 35 | const {arch, platform, env} = process; 36 | 37 | // Detect Node.js x64 binary running under Rosetta 2 on a ARM64 Mac. 38 | if (platform === 'darwin' && arch === 'x64') { 39 | const stdout = childProcess.execFileSync('sysctl', ['-inq', 'sysctl.proc_translated'], {encoding: 'utf8'}); 40 | return stdout.trim() === '1' ? 'arm64' : 'x64'; 41 | } 42 | 43 | if (arch === 'arm64' || arch === 'x64') { 44 | return arch; 45 | } 46 | 47 | if (platform === 'win32' && Object.hasOwn(env, 'PROCESSOR_ARCHITEW6432')) { 48 | return 'x64'; 49 | } 50 | 51 | if (platform === 'linux') { 52 | const stdout = childProcess.execFileSync('getconf', ['LONG_BIT'], {encoding: 'utf8'}); 53 | if (stdout.trim() === '64') { 54 | return 'x64'; 55 | } 56 | } 57 | 58 | return arch; 59 | } 60 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "system-architecture", 3 | "version": "1.0.0", 4 | "description": "Get the operating system CPU architecture", 5 | "license": "MIT", 6 | "repository": "sindresorhus/system-architecture", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "arch", 31 | "architecture", 32 | "cpu", 33 | "arm64", 34 | "arm", 35 | "x64", 36 | "x86", 37 | "64-bit", 38 | "32-bit", 39 | "bitness", 40 | "detect", 41 | "check" 42 | ], 43 | "devDependencies": { 44 | "ava": "^6.1.3", 45 | "xo": "^0.59.3" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # system-architecture 2 | 3 | > Get the operating system CPU architecture 4 | 5 | [`process.arch` / `os.arch()`](https://nodejs.org/api/process.html#processarch) is generally not useful as it returns the CPU architecture for which the Node.js binary was compiled, not the actual system architecture. 6 | 7 | For browser usage, you probably want [`is64bit`](https://github.com/sindresorhus/is64bit) instead. 8 | 9 | ## Install 10 | 11 | ```sh 12 | npm install system-architecture 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | import {systemArchitecture} from 'system-architecture'; 19 | 20 | // On ARM64 macOS 21 | console.log(await systemArchitecture()); 22 | //=> 'arm64' 23 | ``` 24 | 25 | ## API 26 | 27 | ### systemArchitecture() 28 | 29 | Returns a promise for a CPU architecture name. See [`process.arch`](https://nodejs.org/api/process.html#processarch) for possible values. 30 | 31 | ### systemArchitectureSync() 32 | 33 | Returns a CPU architecture name. See [`process.arch`](https://nodejs.org/api/process.html#processarch) for possible values. 34 | 35 | ## Note 36 | 37 | This should really be in Node.js core, but they are [not pragmatic](https://github.com/nodejs/node/issues/17036). 38 | 39 | ## Related 40 | 41 | - [is64bit](https://github.com/sindresorhus/is64bit) - Check whether operating system CPU architecture is 64-bit or 32-bit 42 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import {systemArchitecture, systemArchitectureSync} from './index.js'; 3 | 4 | test('async', async t => { 5 | const result = await systemArchitecture(); 6 | t.is(typeof result, 'string'); 7 | }); 8 | 9 | test('sync', t => { 10 | t.is(typeof systemArchitectureSync(), 'string'); 11 | }); 12 | --------------------------------------------------------------------------------