You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术问询:如何将Node.js项目转换为可执行.exe文件

Convert Your Node.js Project to a Standalone .exe File

Hey there! Converting your Node.js project into a standalone Windows executable (.exe) so it can run without a Node.js installation is totally achievable with the right tools. Below are the two most popular and reliable methods to get this done:

Method 1: Use pkg (Most Common & User-Friendly)

pkg packages your Node.js code along with the full Node.js runtime into a single, self-contained executable. Here's how to use it step by step:

  1. Install pkg (globally for easy access, or locally if you prefer project-specific setup):

    npm install -g pkg
    
  2. Configure your package.json:

    • Add a bin field pointing to your project's entry file (e.g., index.js or app.js):
      {
        "name": "your-project",
        "version": "1.0.0",
        "bin": "./index.js"
      }
      
    • If your project uses external assets (like images, config files, or static resources), add a pkg section to ensure they're included in the build:
      {
        "pkg": {
          "assets": ["src/assets/**/*", "config/*.json"]
        }
      }
      
  3. Build the executable:

    • To build for your current Windows platform:
      pkg .
      
    • To explicitly target 64-bit Windows (avoid compatibility issues):
      pkg --targets win32-x64 .
      

    You’ll find the generated .exe file in your project root, named after your package.

Method 2: Use nexe (Customizable Node Version Support)

nexe is great if you need to package your project with a specific Node.js version (useful if your code relies on runtime-specific features).

  1. Install nexe globally:

    npm install -g nexe
    
  2. Build your executable:

    • Basic build (uses your current Node.js version):
      nexe index.js
      
    • Build with a specific Node.js version (e.g., Node 18.x 64-bit Windows):
      nexe -t windows-x64-18.12.0 index.js
      

    The resulting .exe will appear in your project directory.

Key Tips & Troubleshooting

  • Test first: Always confirm your project runs perfectly in a regular Node.js environment before packaging—fix any bugs upfront to avoid headaches later.
  • Native modules: If your project uses C++ native modules, you’ll need to compile them for Windows first, or use pre-compiled versions compatible with your target architecture.
  • File size: The .exe will be larger than your source code (50-100MB typically) because it includes the entire Node.js runtime—this is normal.
  • Path handling: Avoid hardcoded file paths. Use process.cwd() or adjusted __dirname references (pkg/nexe provide environment variables to handle packaged paths) to access files dynamically.

内容的提问来源于stack exchange,提问作者ATUL SHARMA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:32:54