#How do I run a typescript script inside my electron app (electron-vite)?

5 messages · Page 1 of 1 (latest)

soft pollen
#

Hello,

I have an electron app scaffolded from the great electron-vite plugin: https://electron-vite.org. Now I want to run a script that is not part of the my app at all, what the script does it it downloads some files (trpc router types) from a different repo using Octokit.

This is a part of my script:

const trpcApiImport = (): void => {
  const octokit = new Octokit({
    auth: `xx`
  })

  void TRPC_API_DIST.filePaths.map(async (trpcApiFilePath) => {
    const octokitResponse = await octokit.rest.repos.getContent({
      owner: TRPC_API_DIST.owner,
      repo: TRPC_API_DIST.repository,
      path: trpcApiFilePath.sourcePath
    })

    if (!('content' in octokitResponse.data)) {
      throw Error(`Error: No content available to download - File ${trpcApiFilePath.sourcePath}.`)
    }

    const decodedFileContent = Buffer.from(octokitResponse.data.content, 'base64')

    fs.writeFileSync(trpcApiFilePath.targetPath, decodedFileContent)
  })
}

When I tried to run using: npx tsx trc-import-api/index.ts I get this error:

return new ERR_PACKAGE_PATH_NOT_EXPORTED(
         ^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in C:\Users\lahdi\dev\zahir\node_modules\@octokit\app\package.json

This is my tsconfig.ts

{
  "compilerOptions": {
    "module": "ESNext",
    "target": "ESNext",
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
  },
  "files": [],
  "references": [{ "path": "./tsconfig.node.json" }, { "path": "./tsconfig.web.json" }]
}

This is my tsconfig.node.json:

{
  "extends": "@electron-toolkit/tsconfig/tsconfig.node.json",
  "include": ["electron.vite.config.*", "src/main/**/*", "src/preload/**/*", "src/common/**/*"],
  "compilerOptions": {
    "module": "ESNext",
    "target": "ESNext",
    "esModuleInterop": true,
    "composite": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "types": ["electron-vite/node"]
  }
}

Thanks.

soft pollen
#

I added "type": "module" to my package.json and the scrit ran but this solution unfortunately breaks my electron app from running.

wet rover
#

sounds like it wants something like this in your package.json

exports: {
  ".": ./index.js
}
#

possible the "main" field is acceptable, not sure.