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

"Cannot find module 'zod'" in production environment #1072

Closed
1 task done
oakgary opened this issue Sep 13, 2024 · 12 comments
Closed
1 task done

"Cannot find module 'zod'" in production environment #1072

oakgary opened this issue Sep 13, 2024 · 12 comments

Comments

@oakgary
Copy link

oakgary commented Sep 13, 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

Error: Cannot find module 'zod'
Require stack:
- /app/node_modules/openai/_vendor/zod-to-json-schema/parseDef.js
- /app/node_modules/openai/_vendor/zod-to-json-schema/index.js
- /app/node_modules/openai/helpers/zod.js
- /app/main.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1140:15)
at Module._load (node:internal/modules/cjs/loader:981:27)
at Module.require (node:internal/modules/cjs/loader:1231:19)
at Hook.Module.require (/app/node_modules/dd-trace/packages/dd-trace/src/ritm.js:64:27)
at require (node:internal/modules/helpers:177:18)
at Object.<anonymous> (/app/node_modules/openai/_vendor/zod-to-json-schema/parseDef.js:4:15)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Module._load (node:internal/modules/cjs/loader:1019:12) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [
        '/app/node_modules/openai/_vendor/zod-to-json-schema/parseDef.js',
        '/app/node_modules/openai/_vendor/zod-to-json-schema/index.js',
        '/app/node_modules/openai/helpers/zod.js',
        '/app/main.js'
    ]
}

package.json:

"dependencies": {
	...
	"openai": "^4.60.1",
	"zod": "^3.23.8",
	...
}

npm ls zod logs

@redacted/[email protected] /Users/redacted
├─┬ [email protected]
│ └── [email protected] deduped
└── [email protected]

To Reproduce

I am not sure to be honest. It works fine locally but seems to break somewhere during the deployment. Please let me know what insights could further help you debug this issue.

Code snippets

No response

OS

linux/arm64

Node version

v18.20.4

Library version

v4.60.1

@oakgary oakgary added the bug Something isn't working label Sep 13, 2024
@RobertCraigie
Copy link
Collaborator

@oakgary what happens if you import zod in your own code? does that work?

@oakgary
Copy link
Author

oakgary commented Sep 13, 2024

@oakgary what happens if you import zod in your own code? does that work?

@RobertCraigie the project, including openai and zod, works entirely fine locally, so I guess yes.

@RobertCraigie
Copy link
Collaborator

That's strange, could you share your package.json? And you're just using JS?

@oakgary
Copy link
Author

oakgary commented Sep 13, 2024

That's strange, could you share your package.json? And you're just using JS?

I can't really share the whole package.json. Is there any specific part you are interested in besides the two packages as referenced in the initial post?
Yes, It's just a NestJS application running on NodeJS.

@RobertCraigie
Copy link
Collaborator

No worries, would you mind redacting any parts you feel are sensitive and then sharing it? I'm not really sure what I'd need to be able to reproduce, so it would be helpful to get as similar of a setup as possible.

@oakgary
Copy link
Author

oakgary commented Sep 13, 2024

There isn't really much left after

{
	"name": "redacted",
	"version": "redacted",
	"license": "redacted",
	"scripts": {
		// redacted
	},
	"private": true,
	"dependencies": {
		// redacted
		"openai": "^4.60.1",
		"zod": "^3.23.8"
	},
	"devDependencies": {
		// redacted
	}
}

For context though, we only added zod for the openai implementation and are not using it for anything else.

During our deployment the relevant GitHub Actions steps should be

steps:
  - name: Checkout code
    uses: actions/checkout@v4

  - name: Set Node.js 20.x
    uses: actions/setup-node@v4
    with:
      node-version: 20.x

  - name: Cache node modules
    uses: actions/cache@v3
    with:
      path: node_modules
      key: node_modules-${{hashFiles('yarn.lock')}}
      restore-keys: node_modules-

  - name: Run yarn install
    uses: borales/actions-yarn@v4
    with:
      cmd: install

When it's deployed it's running inside a linux/arm64 node:18-alpine docker container.

@oakgary
Copy link
Author

oakgary commented Sep 14, 2024

While working on a minimal reproducible repository and debugging, the process involved two steps:

  1. Upgrading Yarn from version 1.22.19 to 1.22.22 resolved the issue in my minimal repository. I suspect this is related to the following fix:

    Fixes a hoisting issue when transitive dependencies themselves listed aliases as dependencies.

    Yarn v1.22.22 Release Notes

  2. However, the issue still occurred in the full project.

I then noticed that our build system, Nx, removed the zod package from the package.json in the dist directory. This was due to a combination of me not defining the dependency on the zod module correctly and it being marked as a devDependency in the openai module.

@oakgary
Copy link
Author

oakgary commented Sep 14, 2024

Is zod intended to be set as a devDependency https://github.com/openai/openai-node/blob/master/package.json#L56?

@oakgary
Copy link
Author

oakgary commented Sep 14, 2024

I've updated my response above, and the issue is now resolved on our end. Please feel free to close this if there are no further takeaways for the project.

@RobertCraigie RobertCraigie removed the bug Something isn't working label Sep 20, 2024
@RobertCraigie
Copy link
Collaborator

Thanks for the update!

Is zod intended to be set as a devDependency

Yes, we don't want to add it as a required dependency so that users that aren't using the structured output helpers don't need it.

This was due to a combination of me not defining the dependency on the zod module correctly

I'm curious how did you define it incorrectly? In case anyone else runs into this in the future.

@RobertCraigie RobertCraigie closed this as not planned Won't fix, can't repro, duplicate, stale Sep 20, 2024
@ZachHandley
Copy link

colinhacks/zod#3739 -- please include your solutions when you solve something so others like me don't have to wonder

@oakgary
Copy link
Author

oakgary commented Oct 7, 2024

This was due to a combination of me not defining the dependency on the zod module correctly

I'm curious how did you define it incorrectly? In case anyone else runs into this in the future.

colinhacks/zod#3739 -- please include your solutions when you solve something so others like me don't have to wonder

We are using a monorepo setup with nx. zod was defined as a dependency at the root level, but not within the custom library we have for LLMs that was actually using it. As a result, during the build step, zod was removed from the final package.json and node_modules since it was not properly considered as in use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants