如何将 typescript Nodejs 项目转换为单个可执行文件 exe

问题描述 投票:0回答:1

我首先将我的 typescript 项目编译为 javascript,以便将其转换为 exe。

我尝试使用https://github.com/nexe/nexehttps://github.com/vercel/pkghttps://nwjs.io/

我正在使用 Puppeteer,它具有 Puppeteer Chromium 依赖项。它引起了问题

这是我使用pkg创建exe时的日志

PS C:\Users\mohammed mehdi\OneDrive\Desktop\myna\Automatic-Scraper\nodepuppeteer\dist> pkg .\main.js -t node18
> [email protected]
> Fetching base Node.js binaries to PKG_CACHE_PATH
  fetched-v18.5.0-win-x64             [====================] 100%

> Warning Cannot include directory %1 into executable.
  The directory must be distributed with executable as %2.
  %1: node_modules\puppeteer\.local-chromium
  %2: path-to-executable/puppeteer


PS C:\Users\mohammed mehdi\OneDrive\Desktop\myna\Automatic-Scraper\nodepuppeteer\dist> .\main-win.exe
pkg/prelude/bootstrap.js:1872
      throw error;
      ^

Error: Cannot find module 'C:\snapshot\dist\node_modules\openai\_shims\auto\runtime-node.js'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
    at createEsmNotFoundErr (node:internal/modules/cjs/loader:960:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:953:15)
    at resolveExports (node:internal/modules/cjs/loader:482:14)
    at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
    at Function._resolveFilename (pkg/prelude/bootstrap.js:1951:46)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at Module.require (pkg/prelude/bootstrap.js:1851:31)
    at require (node:internal/modules/cjs/helpers:102:18) {
  code: 'MODULE_NOT_FOUND',
  path: 'C:\\snapshot\\dist\\node_modules\\openai\\package.json',
  pkg: true
}

这是给nexe的

nexe main.js --build
i nexe 4.0.0-rc.4
√ Node source extracted to: C:\Users\mohammed mehdi\.nexe\16.15.0
√ Compiling Node with arguments: nosign,release,x64
√ Finished in 78.68s

Error: vcbuild.bat nosign release x64 exited with code: 1

See nexe -h for usage..
node.js typescript webpack exe vercel
1个回答
0
投票

我通过一些尝试和错误弄清楚了。

我正在做的是

  1. 将 TypeScript 编译为 JavaScript
  2. 使用 pkg 或 nexe 将其转换为 exe。

我缺少的是将其捆绑到一个 javascript 文件中

所以现在我的步骤是

  1. 将整个 TypeScript 项目捆绑到一个 javascript Bundle.js 中。 (我不需要单独将 typescript 编译为 javascript。webpack 为我做这件事。)
  2. 使用pkg将bundle.js转换为exe(这很简单。)

默认情况下pkg使用node16。您可以通过这样做来设置目标节点。

pkg .\main.js -t node18

执行此操作后,可以轻松创建 exe。

要进行 webpack 捆绑,您需要在 TS 项目的根目录中创建一个

webpack.config.js

在我写的那个文件中。

// webpack.config.js
const path = require('path');

module.exports = {
  mode: 'production',
  entry: './src/main.ts', // Entry point of your application
  target: 'node',
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  }
};

© www.soinside.com 2019 - 2024. All rights reserved.