WSL: nodejs not working, exec format error

Version

Microsoft Windows [Version 10.0.19044.1586]

WSL Version

  • WSL 2
  • WSL 1

Kernel Version

4.4.0-19041-Microsoft

Distro Version

Arch

Other Software

No response

Repro Steps

Install nodejs from Archlinux repo with pacman -S nodejs, and run node

Expected Behavior

nodejs starts

Actual Behavior

nodejs does not start. exec format error.

wangqr@ocalhost:~$ node
zsh: exec format error: node

Diagnostic Logs

nodejs can start if we manually invoke ld-linux, although it is already the interpreter in the ELF.

wangqr@ocalhost:~$ /usr/bin/node
zsh: exec format error: /usr/bin/node
wangqr@ocalhost:~$ strace /usr/bin/node
execve("/usr/bin/node", ["/usr/bin/node"], 0x7ffff42aefa0 /* 25 vars */) = -1 ENOEXEC (Exec format error)
strace: exec: Exec format error
+++ exited with 1 +++
wangqr@ocalhost:~$ /lib/ld-linux-x86-64.so.2 /usr/bin/node
Welcome to Node.js v17.7.1.
Type ".help" for more information.
>
wangqr@ocalhost:~$ /lib64/ld-linux-x86-64.so.2 /usr/bin/node
Welcome to Node.js v17.7.1.
Type ".help" for more information.
>
wangqr@ocalhost:~$ file /usr/bin/node
/usr/bin/node: ELF 64-bit LSB pie executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=10ec67f51a763d04ac6e1fe305ce214fb40eabda, for GNU/Linux 4.4.0, stripped
wangqr@ocalhost:~$ readelf -h /usr/bin/node
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - GNU
  ABI Version:                       0
  Type:                              DYN (Position-Independent Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x6aa570
  Start of program headers:          64 (bytes into file)
  Start of section headers:          34087608 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         16
  Size of section headers:           64 (bytes)
  Number of section headers:         30
  Section header string table index: 29

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 16
  • Comments: 37 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Had the same issues and upgrading to WSL 2 worked for me wsl --set-version <name> 2

Use <name> from wsl -l -v

I did follow https://docs.microsoft.com/en-us/windows/wsl/install-manual#step-3---enable-virtual-machine-feature (and step 4) but node is working for me now. This is with Ubuntu 22.04

Update to WSL v2 as @srogerf suggested - worked for me. Ubuntu 22.04.1 Node v12.22.9

I got the same problem. Win10 21H2 and Ubuntu 22.04 WSL1

after sudo apt install nodejs only get /usr/bin/node: cannot execute binary file: Exec format error

I fixed this problem by using nodejs package https://github.com/nodesource/distributions/blob/master/README.md#debinstall

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs

It is possible to make a workaround for node itself:

sudo mv /usr/bin/node /usr/bin/node-orig
printf '#!/bin/sh\nexec /lib64/ld-linux-x86-64.so.2 /usr/bin/node-orig "$@"' | sudo tee /usr/bin/node
sudo chmod a+x /usr/bin/node

My previous fix for npm won’t work with the updated node executable, needs to be reverted.

@qchencc 's solution worked for me on WSL1 + Ubuntu 18.04 + Windows 11 22H2, but required switching to Node v16:

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs

The suggestion “upgrade to WSL2” is both partly off topic and also potentially wrong. It’s potentially very wrong under certain contexts because WSL2 requires the Windows operating system to turn on the Hypervisor. This is completely unnecessary when there is no other requirement for it because the Hypervisor takes up resources to run everything from boot time (including the Operating System of Windows itself (because it’s a type 1 Hypervisor instead of a type 2)).

As in https://github.com/microsoft/WSL/issues/5875 following succeeds:

/lib64/ld-linux-x86-64.so.2 /usr/bin/node --version
v14.19.1

This bash script fixed it for me. I ran sudo ./wsl-fix-exec-format-error.sh /usr/bin/node wsl-fix-exec-format-error.sh.txt

I also encountered the same problem and solved it by:

  1. Download the Linux Binary file package from Nodejs official website (https://nodejs.org/en/download) and decompress it;
  2. Establish a soft connection for node/npm/npx;

Somehow the 23 version of node fails with the same error while 22 version does not:

All on WSLv1 Ubuntu 24.04:

Breaks with exec format error:

curl -fsSL https://deb.nodesource.com/setup_23.x | sudo -E bash -
sudo apt-get install -y nodejs
node --version

Works:

curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y nodejs
node --version

I’ve been trying to get a repro of this invalid elf format error but haven’t found one that works on new versions of Windows. If you can reproduce this on windows 22621 or later please let me know by opening a new issue.

Similar issue with WSL1 + Ubuntu 22.04:

$ npm
/usr/bin/node: 1: ELF: not found
/usr/bin/node: 2: �: not found
/usr/bin/node: 2: Syntax error: word unexpected (expecting ")")
$ /lib64/ld-linux-x86-64.so.2 /usr/bin/node --version
v12.22.9

# after I place a similar script as /usr/bin/node, node can start, but npm cannot start still
(node:9038) UnhandledPromiseRejectionWarning: Error: Cannot find module 'semver'
Require stack:
- /usr/share/nodejs/npm/lib/utils/unsupported.js
- /usr/share/nodejs/npm/lib/cli.js
- /usr/share/nodejs/npm/bin/npm-cli.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/usr/share/nodejs/npm/lib/utils/unsupported.js:2:16)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
(node:9038) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9038) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Because npm cannot start, I can’t even npm install semver

attaching /usr/bin/node in bug.zip

This is kind of surprising that such fundamental set-up is currently broken on WSLv1 😃

@benhillis This issue is easily reproducible. I’m not sure if you’ve tried it.

Image

@andreineculau I understand the solution, the initial analysis was done by https://github.com/microsoft/WSL/issues/8219#issuecomment-1094123281 stating that it is a bug in lxcore.sys.

ls.so loads image like the exec call but avoids problems in Microsoft’s lxcore.sys. It is better for sysadmins who are afraid binary patching with elftools.elf.elffile import ELFFile ))

@gavenkoa my general fix is to run

wsl-fix-exec-format-error $(realpath $(command -v node))

The script is here: https://github.com/ysoftwareab/yplatform/blob/master/bin/wsl-fix-exec-format-error

Based on https://gist.github.com/the-moog/179753aacc52b262d1068878d520e9c9 by @the-moog @wangqr 🙌

Running realpath is needed because sometimes (eg homebrew) node in your $PATH is a symlink

PS: I have found that I need to patch gzip as well, not just node, so a generic patch script is really good to have

As a workaround I updated npm tool (actual file is /usr/share/nodejs/npm/bin/npm-cli.js):

#!/lib64/ld-linux-x86-64.so.2 /usr/bin/node
//#!/usr/bin/env node
require('../lib/cli.js')(process)

The same problem on Debian:

bash# apt-cache show nodejs
Package: nodejs
Version: 16.13.2+really14.19.1~dfsg-6
Installed-Size: 949
Maintainer: Debian Javascript Maintainers <pkg-javascript-devel@alioth-lists.debian.net>
Architecture: amd64
Replaces: libnode72 (<< 12.22.10~dfsg~), libnode83 (<< 16.13.2+really14.19.1~dfsg~), nodejs-legacy
Provides: node-types-node (= 14.18.12~16.13.2+really14.19.1~dfsg-6)
Depends: libc6 (>= 2.2.5), libnode83 (= 16.13.2+really14.19.1~dfsg-6)

When run:

/usr/bin/node --version
-bash: /usr/bin/node: cannot execute binary file: Exec format error

libc6 is v2.33-7.

bash# strace -f node --version
execve("/usr/bin/node", ["node", "--version"], 0x7fffdc8ab7e0 /* 30 vars */) = -1 ENOEXEC (Exec format error)
strace: exec: Exec format error
+++ exited with 1 +++

WSL 1:

uname -a
Linux msi 4.4.0-19041-Microsoft #1237-Microsoft Sat Sep 11 14:32:00 PST 2021 x86_64 GNU/Linux

It’s zsh 5.8.1. Running under bash or strace gives save result though.

wangqr@ocalhost:~$ zsh --version
zsh 5.8.1 (x86_64-pc-linux-gnu)
wangqr@ocalhost:~$ bash -c /usr/bin/node
bash: line 1: /usr/bin/node: cannot execute binary file: Exec format error