Call typescript library from commonjs typescript application

64 views Asked by At

I've built a create-probot-app generator for NX. At the moment I'm testing whether I'd be able to share code from a typescript library. However, this doesn't seem to work.

The application MUST BE compiled to CommonJS or probot run will not work.

PS C:\repos\tttt\ccc> npm start -- --project issue-greeter    

> [email protected] start
> nx serve --project issue-greeter


 >  NX   Running target serve for project issue-greeter and 2 tasks it depends on:

 ————————————————————————————————————————————————————————————————————————————————————————————————————— 

> nx run issue-greeter:build  [existing outputs match the cache, left as is]


> nx run issue-greeter:serve

C:\repos\tttt\ccc\dist\apps\issue-greeter\src\index.js:1
import { __awaiter, __generator } from "tslib";
^^^^^^
SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1178:20)
    at Module._compile (node:internal/modules/cjs/loader:1220:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:121:18)
    at C:\repos\tttt\ccc\node_modules\probot\lib\helpers\resolve-app-function.js:35:65
    at async resolveAppFunction (C:\repos\tttt\ccc\node_modules\probot\lib\helpers\resolve-app-function.js:35:17)
Node.js v18.17.1
Warning: run-commands command "probot run ./dist/apps/issue-greeter/src/index.js" exited with non-zero status code

The repository is hosted here, and was created using the following commands:

npx create-nx-workspace
> probottest
> none
> package-based
> Yes

code probottest
npm i -D @mintplayer/nx-generators
nx generate @mintplayer/nx-generators:create-probot-app
> issue-greeter
> Greets when someone creates an issue
npm start -- --project issue-greeter

# Visit http://localhost:3000
# => https://github.com/settings/apps/new?url=http://localhost:3000
# Visit https://smee.io/ -> new channel
 => Webhook-url=https://smee.io/abcdefghijklmop
 => Webhook-secret=aaaaaaaaaaaaaaaaaaaaaaaa
 => cert-generated-from-the-github-app.private-key.pem
 => app-id = 123456

=> Put this in the .env file
# Visit https://github.com/settings/apps
=> Private keys: Generate
=> Move the pem-file to your project under the `.data` folder
# Visit http://localhost:3000
=> Use an existing Github app
=> Fill out + upload pem-file
npm run build -- --project issue-greeter --skip-nx-cache
# restart server
# Visit app home page https://github.com/apps/probottest
# Install on repo


nx g @nx/js:library greeter --buildable --publishable --import-path=@example/greeter

However, when I try to run the project (npm start -- --project issue-greeter or npx probot run ./dist/apps/issue-greeter/src/index.js), I get the following error:

Cannot find module '@example/greeter'

PS C:\repos\tttt\ccc> npx probot run ./dist/apps/issue-greeter/src/index.js
node:internal/modules/cjs/loader:1077
  const err = new Error(message);
              ^

Error: Cannot find module '@example/greeter'
Require stack:
- C:\repos\tttt\ccc\dist\apps\issue-greeter\src\index.js
- C:\repos\tttt\ccc\node_modules\probot\lib\helpers\resolve-app-function.js
- C:\repos\tttt\ccc\node_modules\probot\lib\run.js
- C:\repos\tttt\ccc\node_modules\probot\lib\index.js
- C:\repos\tttt\ccc\node_modules\probot\lib\bin\probot-run.js
- C:\repos\tttt\ccc\node_modules\probot\bin\probot-run.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:121:18)
    at Object.<anonymous> (C:\repos\tttt\ccc\dist\apps\issue-greeter\src\index.js:3:17)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\repos\\tttt\\ccc\\dist\\apps\\issue-greeter\\src\\index.js',
    'C:\\repos\\tttt\\ccc\\node_modules\\probot\\lib\\helpers\\resolve-app-function.js',
    'C:\\repos\\tttt\\ccc\\node_modules\\probot\\lib\\run.js',
    'C:\\repos\\tttt\\ccc\\node_modules\\probot\\lib\\index.js',
    'C:\\repos\\tttt\\ccc\\node_modules\\probot\\lib\\bin\\probot-run.js',
    'C:\\repos\\tttt\\ccc\\node_modules\\probot\\bin\\probot-run.js'
  ]
}

If I could get this to work, I'd be able to share code inside a monorepo that contains several github bots. But I don't seem to be able to call my library from the Probot app (which is commonjs).

0

There are 0 answers