question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read properties of undefined (reading '_parse')

See original GitHub issue

Hey, I’ve come across this issue in my project while composing schemas from imported zod objects/enums.Here is the code:

CountryCode.ts

import { z } from 'zod';

const countryCode = z.enum(['IE', 'GB']);

type CountryCode = z.infer<typeof countryCode>;

const countrySchema = z.object({
  alpha2: z.string(),
  alpha3: z.string(),
  code: countryCode,
  name: z.string(),
  region: z.string(),
  regionCode: z.number(),
  subRegion: z.string(),
  subRegionCode: z.number(),
});

type Country = z.infer<typeof countrySchema>;

export { countryCode, countrySchema };
export type { Country, CountryCode };

CurrencyCode.ts

import { z } from 'zod';

const currencyCode = z.enum(['EUR', 'GBP']);

type CurrencyCode = z.infer<typeof currencyCode>;

const currencySchema = z.object({
    code: currencyCode,
    name: z.string(),
    minorUnit: z.number(),
  });


  type Currency = z.infer<typeof currencySchema>;

export { currencyCode, currencySchema };    
export type { CurrencyCode, Currency };   

If i import the above like so: invitePerson.ts

import { z } from 'zod';
import { countryCode, currencyCode } from '../../api';

const invitePersonSchema = z.object({
  startDate: z.date().optional(),
  firstName: z.string().min(1, 'First name is required'),
  lastName: z.string().min(1, 'Last name is required'),
  role: z.string().optional(),
  dateOfBirth: z.date().optional(),
  email: z.string().email('Email is required'),
  residentialAddress: z.object({
    addressLine1: z.string().optional(),
    addressLine2: z.string().optional(),
    addressLine3: z.string().optional(),
    city: z.string().optional(),
    countryCode: countryCode,
    postalCode: z.string().optional(),
  }),
  annualSalary: z
    .string()
    .min(1, 'Annual salary is required')
    .transform(value => parseInt(value, 10)),
  salaryCurrency: currencyCode,
  taxId: z.string().optional(),
});

type InvitePersonData = z.infer<typeof invitePersonSchema>;

export { invitePersonSchema };
export type { InvitePersonData };

I get the error in the title. However, if I define the enums directly in the invitePersonSchema and don’t import, there is no error. I would have expected this to be allowed, so please forgive me if I am misusing the library in some way.

Note: this was already discussed here, but the last comment provides a reproducible example which I have copied below, just in case it helps: https://github.com/colinhacks/zod/issues/643

I ran into the same error message and found this issue. Now while my problem is likely a different another one, I thought sharing this might still help others ending up here.

Essentially what I was doing is exporting a schema from one file, using it in another one to compose another schema, and then importing that composed schema back into the first file. While there isn’t a cyclic dependency per se, it causes the mentioned error at runtime.

See this CodeSandbox for an example: https://codesandbox.io/s/patient-butterfly-hsg3q?file=/src/index.ts The message isn’t as descriptive as in my local (larger) project, but removing this exact kind of import solved it for me. (Maybe this is generally “wrong” in terms of my tsconfig or simply unsupported by zod and I don’t know it, but I would intuitively expect it to work 🙈.)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
rchampourliercommented, Aug 8, 2022

The Enum is in a different file (where it belongs to) and I get this cryptic error message (“TypeError: Cannot read properties of undefined (reading ‘_parseSync’)”). If I temporarly put the enums inline in the same file where the zod object is created, everything works.

I had a Zod schema defined in a file using schemas defined in another. I was having the about the same issue (Cannot read properties of undefined (reading '_asyncParse').

Moving the schemas in the same file fixed it.

I then got the issue again in another context. I looked at this issue which was mentioned earlier and avoiding the “circular import” mentioned here fixed it, definitely I hope.

1reaction
cromaniuccommented, Oct 5, 2022

Facing the same issue and this is a clear reproducible example without any circular dependency: https://codesandbox.io/s/patient-butterfly-hsg3q?file=/src/index.ts

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'parse' of undefined when requiring a ...
You get this error because in the file docuvieware-min.js , at the line 166 , the eval function isn't able to read the...
Read more >
[BUG] Cannot read property 'parse' of undefined (intl ... - GitHub
This error seems to be coming from "into" library. I just can't find the exact reason of this error. Here's a screenshot of...
Read more >
How to Avoid the Infamous "Cannot read properties of ... - Bitovi
That error message is telling you the function is returning undefined implicitly, but its return type does not include undefined in it. Awesome!...
Read more >
Getting 141 code with error "Cannot read property 'success' of ...
Him I'm new to Parse library. Please help me i got a error of 141 with message of “Cannot read property 'success' of...
Read more >
My result cannot read properties of undefined (reading 'length ...
Your result is not an array; it's an object which happens to have numeric property names. If you want to iterate over the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found