【问题标题】:how to compile nodejs to a single, fully static binary file如何将nodejs编译成一个完全静态的二进制文件
【发布时间】:2013-07-30 09:45:02
【问题描述】:

有谁知道如何将 nodejs 静态编译为单个可执行二进制文件? 我的意思是不需要共享库。

有一些旧版本nodejs的指南,但不适用于最后一个。

谢谢!

【问题讨论】:

  • 谢谢,狂怒。但我只是希望节点本身是静态 exe,而不是想将所有 js 打包在一起。通过这种方式,我可以在 android 环境中运行这个单一的二进制文件。
  • 我想要的是$ ldd /path/to/bin/node 并得到not a dynamic executable
  • 两年后...你找到解决方案了吗(有同样的问题)?

标签: node.js static compilation


【解决方案1】:

正如 Daniel Milde 所指出的,https://hub.docker.com/r/dundee/nodejs-static/ 是一种替代方案,但它已被弃用,并且没有暴露 Dockerfile,因此不会激发信任。

我已经从头开始在容器中测试了节点二进制文件并且它工作正常。

您可以自己构建静态链接的节点。

~/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

【讨论】:

【解决方案2】:

您可以使用 nodejs-static docker image 来获取与 musl libc 链接的静态编译的 Node.JS 二进制文件。

https://hub.docker.com/r/dundee/nodejs-static/

【讨论】:

    【解决方案3】:

    这是另一个可以将您的应用程序打包到单个可执行文件中的工具https://www.npmjs.com/package/pkg

    【讨论】:

    • 这似乎没有静态选项。
    【解决方案4】:

    同时有http://enclosejs.com/ 似乎能够生成单个二进制文件。

    【讨论】:

    • 这似乎不再维护。另一个答案中的 pkg 项目似乎是另一种选择。
    【解决方案5】:

    我今天正在寻找这个,我找到了这个选项:

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

    https://github.com/vercel/ncc

    两者都在 2021 年开发

    还有 NEXE,最新版本是 2017 年

    【讨论】:

      【解决方案6】:

      如果您正在谈论编译 Javascript 代码,您可能会使用 pkg 接近

      它以某种方式从 Javascript 创建一个独立的二进制可执行文件,包括模块依赖项和资产文件,并生成一个独立的可执行文件。

      安装使用简单:

      $ npm install -g pkg
      $ pkg index.js -o my-program
      $ ./my-program
      

      我的理解是这个二进制文件包含 nodejs 字节码。看来你也可以交叉编译。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-17
        • 2021-11-23
        • 1970-01-01
        • 1970-01-01
        • 2013-02-16
        • 2010-12-05
        相关资源
        最近更新 更多