Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pkg loading wrong runtime.js #734

Closed
1 task done
PeterTran4 opened this issue Mar 21, 2024 · 6 comments
Closed
1 task done

Pkg loading wrong runtime.js #734

PeterTran4 opened this issue Mar 21, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@PeterTran4
Copy link

PeterTran4 commented Mar 21, 2024

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • This is an issue with the Node library

Describe the bug

Hi all,
We're currently building an application which uses Open AI Node which we go from MJS to JS files using babel. However whenever we try to load application locally ex: node .\app.js it runs fine. Once we package the files into an exe is where we run into issues. Mostly not finding the right modules. In this case, it's trying to find _shim/auto/runtime-node.js and cannot find it.

I viewed the debug for pkg and it seems like runtime.js is being added
> [debug] Bytecode of C:\Users\ptran\Desktop\nodeServiceTesting\node_modules\openai\_shims\auto\runtime.js is added to queue. It was required from C:\Users\ptran\Desktop\nodeServiceTesting\node_modules\openai\_shims\index.js

Where when we run the actual exe it looking for runtime-node.js

Here is the error from the executable:

1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a l iteral 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:967:15) at finalizeEsmResolution (node:internal/modules/cjs/loader:960:15) at resolveExports (node: internal/modules/cjs/loader:488:14) at Module._findPath (node:internal/modules/cjs/loader:528:31)
at Module._resolveFilename (node:internal/modules/cjs/loader:932:27) at Function._resolveFilename (pkg/prelude/bootstrap.js:1951:46)
at Module._load (node: internal/modules/cjs/loader: 787:27) at Module require (node:internal/modules/cjs/loader:1012: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|\nodeServiceTesting\\node_modules|\openail|package.json',
pkg: true```

My current fix is to add require the runtime-node.js within the openai file but this is temp fix. 


### To Reproduce

1. Install openAI on NPM
2. Reference openAI `import OpenAI from 'openai';`
3. Babel all the MJS into JS
4. Pkg the files into a single executable and run.


### OS

Windows 11

### Node version

v18.19.1

### Library version

openai 4.29.2
@PeterTran4 PeterTran4 added the bug Something isn't working label Mar 21, 2024
@rattrayalex
Copy link
Collaborator

rattrayalex commented Mar 22, 2024

Hmm. Does adding an explicit import 'openai/shims/node' at the top of your file help?

If not, this may essentially be a bug in the bundler you're using to package the exe.

@PeterTran4
Copy link
Author

Yep added import * as test from 'openai/shims/node.js'; to the top of the file and ran into this error.
> Warning Cannot find module 'formdata-node/file-from-path' from 'C:\Users\ptran\Desktop\nodeServiceTesting\node_modules\openai\_shims' in C:\Users\ptran\Desktop\nodeServiceTesting\node_modules\openai\_shims\node-runtime.js

After doing some digging it seems even adding import * as formdataTest from 'openai/node_modules/formdata-node/lib/cjs/index.js'; did not fix this error. I've also tried importing normally and still getting the same error.

@rattrayalex
Copy link
Collaborator

Hmm. We don't have official support for bundling with pkg, so I'm not terribly surprised this doesn't work. I'd encourage you to try raising an issue with pkg about this.

It's possible that this will get better in our next major version, once #402 lands.

Until then, I'm going to close this, but if pkg maintainers have follow-on questions or requests they're welcome to bring them here.

@rattrayalex rattrayalex closed this as not planned Won't fix, can't repro, duplicate, stale Mar 24, 2024
paulesser pushed a commit to R-A-I-S-E/paul-chat that referenced this issue Mar 24, 2024
@wlgns2223
Copy link

I have exactly same error
Cannot find module '/snapshot/Company/node_modules/.pnpm/openai@4.29.2/node_modules/openai/_shims/auto/runtime-node.js'

did you find any solution ? 🥹

@rattrayalex
Copy link
Collaborator

Please open an issue with pkg.

@josselinbuils
Copy link

josselinbuils commented Aug 2, 2024

Hello,

For those who have the issue, it can fixed with few pkg config.

Example for Node.js via package.json config:

"pkg": {
  "patches": {
    "./node_modules/openai/_shims/index.js": [
      "require('openai/_shims/auto/runtime')",
      "require('openai/_shims/node-runtime')"
    ]
  }
}

Openai lib loads runtime dependencies depending on the environment, so using dynamic import paths, and pkg does not like that.

The solution is to transform those dynamic imports into static ones depending on the your target environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants