docs.sheetjs.com/docz/docs/03-demos/20-cli/05-pkg.md

4.2 KiB

title sidebar_label pagination_prev pagination_next sidebar_custom_props
Packing Sheets with pkg pkg demos/desktop/index demos/data/index
summary
Prebuilt NodeJS packages

import current from '/version.js'; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import CodeBlock from '@theme/CodeBlock';

export const r = {style: {color:"red"}}; export const B = {style: {fontWeight:"bold"}};

pkg1 is a tool for generating command-line tools that embed scripts.

SheetJS is a JavaScript library for reading and writing data from spreadsheets.

This demo uses pkg and SheetJS to create a standalone CLI tool for parsing spreadsheets and converting to other formats.

:::caution pass

With the official release of NodeJS SEA, Vercel opted to deprecate pkg. It is still useful for deploying apps embedding NodeJS v18 or earlier since those versions do not support NodeJS SEA.

:::

:::note Tested Deployments

This demo was tested in the following deployments:

Architecture Version NodeJS Date
darwin-x64 5.8.1 18.5.0 2024-05-28
darwin-arm 5.8.1 18.5.0 2024-05-25
win10-x64 5.8.1 18.5.0 2024-04-18
win11-arm 5.8.1 18.5.0 2024-05-28
linux-x64 5.8.1 18.5.0 2024-03-21
linux-arm 5.8.1 18.5.0 2024-05-26

:::

Integration Details

The SheetJS NodeJS module can be required from scripts. pkg will automatically handle packaging.

Script Requirements

Scripts that exclusively use SheetJS libraries and NodeJS built-in modules can be bundled using pkg.

The demo script xlsx-cli.js runs in NodeJS. It is a simple command-line tool for reading and writing spreadsheets.

Limitations

:::danger pass

When this demo was last tested, pkg failed with an error referencing node20:

> Targets not specified. Assuming:
  node20-linux-arm64, node20-macos-arm64, node20-win-arm64
> Error! No available node version satisfies 'node20'

pkg does not support NodeJS 20 or 22!

The local NodeJS version must be rolled back to version 18.

If nvm or nvm-windows was used to install NodeJS:

nvm install 18
nvm use 18

Otherwise, on macOS and Linux, n can manage the global installation:

sudo npm i -g n
sudo n 18

On Windows, it is recommended to use a prebuilt installer2

:::

Complete Example

  1. Downgrade NodeJS to major version 18 or earlier.

  2. Download the test file https://docs.sheetjs.com/pres.numbers:

curl -o pres.numbers https://docs.sheetjs.com/pres.numbers
  1. Download xlsx-cli.js
curl -o xlsx-cli.js https://docs.sheetjs.com/cli/xlsx-cli.js
  1. Install the dependencies:
{`\ npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`} {`\ pnpm install --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`} {`\ yarn add https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`}
  1. Create the standalone program:
npx pkg xlsx-cli.js

This generates xlsx-cli-linux, xlsx-cli-macos, and xlsx-cli-win.exe .

  1. Run the generated program, passing pres.numbers as the argument:
./xlsx-cli-linux pres.numbers
./xlsx-cli-macos pres.numbers
.\xlsx-cli-win.exe pres.numbers

  1. The project does not have a website. The source repository is publicly available. ↩︎

  2. The NodeJS website hosts prebuilt installers. ↩︎