egoist / egoist/tsup

how to bundle a cli?

Open
#800 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
275
PR merge metrics
No merged PRs in 30d

Description

I'm trying to write cli for my project. But it doesn't work,I need help.

```bash

D:\github\packages\rigger\dist\bin.js:44
var import_ora2 = __toESM(require("ora"));
^

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\github\node_modules\.pnpm\ora@6.1.2\node_modules\ora\index.js from D:\github\packages\rigger\dist\bin.js not supported.
Instead change the require of index.js in D:\github\packages\rigger\dist\bin.js to a dynamic import() which is available in all CommonJS modules.
at Object. (D:\github\packages\rigger\dist\bin.js:44:27) {
code: 'ERR_REQUIRE_ESM'
}

Node.js v18.12.1
```

```json
//tsup.config.json
{
"entry": [
"src/index.ts",
"src/bin.ts"
],
"minify": false,
"splitting": false,
"clean": true,
"dts": true,
"skipNodeModulesBundle": true
}
```

```json
//tsconfig.json
{
"compilerOptions": {
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"noEmit": false,
"target": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports":true
}
}
```

```json
//package.json
{

"main": "dist/index",
"types": "dist/index",
"files": [
"dist/*.{js,d.ts}",
"schema/*.json"
],
"bin" : "dist/bin.js",
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"prepublish": "tsup"
},
"dependencies": {
"chalk": "^2.4.1",
"child_process": "^1.0.2",
"chokidar": "^3.5.3",
"commander": "^9.4.0",
"fs-extra": "^10.1.0",
"node-xlsx": "^0.21.0",
"ora": "^6.1.2",
"steam-game-path": "2.0.1"
},
"devDependencies": {
"@types/fs-extra": "^9.0.13",
"@types/node": "^17.0.45",
"@types/node-xlsx": "^0.15.3"
}
}

```

```typescript
//dits/bin.ts
#!/usr/bin/env node

import { program } from 'commander';
import { version } from '../package.json';
import { tools } from '.';
import { spawn } from 'child_process';
import { existsSync, writeFile } from 'fs-extra';
import ora from 'ora';
import { join, relative } from 'path';

program
.version(version, "-v, --version")
.on('--help', () => {
let text = ora('hello world')
text.start()
})
.option("aaa", "aaa", tools )
.parse(process.argv)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.