如何将Node.js编译为单个、完全静态的二进制文件

25

有人知道如何将nodejs静态编译为单个可执行二进制文件吗?

我指的是不需要共享库的版本。

虽然有一些针对旧版本nodejs的指南,但对于最新的版本并不起作用。

谢谢!


1
这可能有所帮助:http://architects.dzone.com/articles/compiling-nodejs-projects - furydevoid
谢谢,furydevoid。但我只想让节点本身成为静态exe,而不是想要打包所有js在一起。通过这种方式,我可以在Android环境中运行这个单一的二进制文件。 - holly
1
我想要的是“$ ldd /path/to/bin/node”并获得“not a dynamic executable”。 - holly
2
两年后...你找到解决方案了吗(有同样的问题)? - Udo G
Deno提供了编译选项,非常类似于NodeJS。 - Marc
6个回答

16

正如Daniel Milde所指出的,https://hub.docker.com/r/dundee/nodejs-static/ 是一个备选方案,但它已经过时,而且没有公开的Dockerfile,这会使人失去信任感。

我在一个从零开始的容器中测试了node二进制文件,它可以正常工作。

你可以自己构建静态链接的node。

~/src$ git clone https://github.com/nodejs/node
cd node
~/src/node$ ./configure --help | grep static
    --fully-static        Generate an executable without external dynamic
    --partly-static       Generate an executable with libgcc and libstdc++
    --enable-static       build as static library
                        link to a shared http_parser DLL instead of static
    --shared-libuv      link to a shared libuv DLL instead of static linking
    --shared-nghttp2    link to a shared nghttp2 DLL instead of static linking
    --shared-openssl    link to a shared OpenSSl DLL instead of static linking
    --shared-zlib       link to a shared zlib DLL instead of static linking
    --shared-cares      link to a shared cares DLL instead of static linking
~/src/node$ ./configure --fully-static --enable-static
~/src/node$ make
...
~/src/node$ file out/Release/node 
out/Release/node: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 3.2.0, BuildID[sha1]=e5be29b02e283d0efdfc313e409b1a0802bd0603, with debug_info, not stripped
~/src/node$ /lib64/ld-linux-x86-64.so.2 --list out/Release/node 
        statically linked
~/src/node$ 

让我们从零开始测试一个Docker容器:

~/src/node$ cat > /tmp/Dockerfile.node-static <<EOF
FROM scratch
COPY out/Release/node /node
CMD /node
EOF
~/src/node$ docker build -t glaudiston/node-scratch -f /tmp/Dockerfile.node-static .
Sending build context to Docker daemon  1.239GB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : COPY out/Release/node /node
 ---> ec9d62bb7eb0
Step 3/3 : CMD /node
 ---> Running in 2e375fc580f7
Removing intermediate container 2e375fc580f7
 ---> 5d6a2b5f20b8
Successfully built 5d6a2b5f20b8
Successfully tagged glaudiston/node-scratch:latest
$ docker run -ti node-static:latest /node
Welcome to Node.js v14.0.0-pre.
Type ".help" for more information.
> var x = { 'test': 123 };
undefined
> x.test
123
> 

你也可以在 Dockerfile 中构建它:

Dockerfile.node 文件:

FROM alpine:3.11.3
RUN apk add git python gcc g++ linux-headers make
RUN git clone https://github.com/nodejs/node && \
        cd node && \
        ./configure --fully-static --enable-static && \
        make
FROM scratch
COPY --from=0 out/Release/node /node
CMD /node

并且去建造:

$ docker build -f Dockerfile.node -t node-static .
Sending build context to Docker daemon  121.9kB
Step 1/6 : FROM alpine:3.11.3
 ---> e7d92cdc71fe
Step 2/6 : RUN apk add git python gcc g++ linux-headers make
 ---> Using cache
 ---> c343d63a3094
Step 3/6 : RUN git clone https://github.com/nodejs/node &&      cd node &&      ./configure --fully-static --enable-static &&   make
 ---> Using cache
 ---> e7f9fc931827
Step 4/6 : FROM scratch
 ---> 
Step 5/6 : COPY --from=0 /node/out/Release/node /node
 ---> 045ad784eadc
Step 6/6 : CMD /node
 ---> Running in f48178348e7b
Removing intermediate container f48178348e7b
 ---> ff1d93da95c8
Successfully built ff1d93da95c8
Successfully tagged node-static:latest

或者,如果你信任我,你可以使用我的公共Docker镜像glaudiston/node-scratch

~/src/node$ docker push glaudiston/node-scratch
The push refers to repository [docker.io/glaudiston/node-scratch]
9251f7e82698: Pushed 
latest: digest: sha256:c6f2154daa0144abafc0a7070c48f0c4031a036901c75d432b8c826ae793a1d7 size: 529

1
你可以使用 https://github.com/nodejs/docker-node/tree/master/10/alpine3.10(或其他版本) 作为 Docker 构建的起点。 - Qiang Li

3

我今天在寻找这个,发现了以下选项:

https://github.com/vercel/pkg
PKG有一个编译为平台linuxstatic的选项。
请注意,完全静态的Node二进制文件无法加载本机绑定,因此您可能无法在linuxstatic中使用Node绑定。

https://github.com/vercel/ncc

它们都是2021年开发的。

还有NEXE,最新版本发布于2017年。

输入图像描述


2

这似乎没有静态选项。 - Drazisil

2

1
如果你想要编译JavaScript代码,你可以尝试使用pkg
它可以将JavaScript代码包括模块依赖和资源文件打包成一个自我包含的可执行二进制文件。
安装和使用都非常简单:
$ npm install -g pkg
$ pkg index.js -o my-program
$ ./my-program

我的理解是,这个二进制文件包含了Node.js的字节码。同时似乎也可以进行交叉编译。

0

1
这似乎不再得到维护。另一个答案中的 pkg 项目似乎是替代方案。 - Andrew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接