colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
34.26k stars 1.2k forks source link

Module '"zod"' has no exported member 'z'.ts #3739

Open JacobFV opened 2 months ago

JacobFV commented 2 months ago

I get this error when my linter runs over the bunx create-t3-app starting repo. I also get this linting error when i install zod separately into a empty repo. The code runs fine tho. I don't know if I'm missing type deps or maybe the package has changed and all the tutorials telling me to import {z} from 'zod' are outdated now. How can i fix this error?

zod version: ^3.23.3 bun version: 1.1.17 node version: v20.12.2

package.json:

{
  "name": "subhub",
  "version": "0.1.0",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "lint": "next lint",
    "start": "next start",
    "dev:soft-reset": "bun db:wipe && bun db:seed && bun dev",
    "dev:hard-reset": "bun i && bun db:rm && bun db:start && bun db:seed && bun dev",
    "db:generate": "drizzle-kit generate",
    "db:migrate": "drizzle-kit migrate",
    "db:push": "drizzle-kit push",
    "db:studio": "drizzle-kit studio",
    "db:seed": "bun run src/server/db/seed.ts",
    "db:wipe": "bun run src/server/db/wipe.ts",
    "db:start": "./start-database.sh && sleep 10 && bun db:generate && bun db:migrate && bun db:push",
    "db:stop": "docker stop $(docker ps -aq) || true",
    "db:rm": "rm -rf drizzle || true && bun db:stop || true && docker rm $(docker ps -aq) || true",
    "db:restart": "bun db:stop || true && bun db:rm || true && bun db:start"
  },
  "dependencies": {
    "@auth/drizzle-adapter": "^1.1.0",
    "@hookform/resolvers": "^3.9.0",
    "@radix-ui/react-icons": "^1.3.0",
    "@t3-oss/env-nextjs": "^0.10.1",
    "@tanstack/react-query": "^5.50.0",
    "@trpc/client": "^11.0.0-rc.446",
    "@trpc/next": "^11.0.0-rc.446",
    "@trpc/react-query": "^11.0.0-rc.446",
    "@trpc/server": "^11.0.0-rc.446",
    "@types/uuid": "^10.0.0",
    "bcrypt": "^5.1.1",
    "class-variance-authority": "^0.7.0",
    "clsx": "^2.1.1",
    "drizzle-orm": "^0.33.0",
    "geist": "^1.3.0",
    "lucide-react": "^0.438.0",
    "next": "^14.2.4",
    "next-auth": "^4.24.7",
    "next-themes": "^0.3.0",
    "postgres": "^3.4.4",
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "recharts": "^2.12.7",
    "superjson": "^2.2.1",
    "tailwind-merge": "^2.5.2",
    "tailwindcss-animate": "^1.0.7",
    "uuid": "^10.0.0",
    "zod": "^3.23.3"
  },
  "devDependencies": {
    "@types/bcrypt": "^5.0.2",
    "@types/eslint": "^8.56.10",
    "@types/node": "^20.14.10",
    "@types/react": "^18.3.3",
    "@types/react-dom": "^18.3.0",
    "@typescript-eslint/eslint-plugin": "^8.1.0",
    "@typescript-eslint/parser": "^8.1.0",
    "drizzle-kit": "^0.24.0",
    "eslint": "^8.57.0",
    "eslint-config-next": "^14.2.4",
    "eslint-plugin-drizzle": "^0.2.3",
    "postcss": "^8.4.39",
    "prettier": "^3.3.2",
    "prettier-plugin-tailwindcss": "^0.6.5",
    "tailwindcss": "^3.4.3",
    "typescript": "^5.5.3"
  },
  "ct3aMetadata": {
    "initVersion": "7.37.0"
  }
}
Char99s commented 2 months ago

Can you provide tsconfig.json

JacobFV commented 2 months ago
{
  "compilerOptions": {
    /* Base Options: */
    "esModuleInterop": true,
    "skipLibCheck": true,
    "target": "ESNext",
    "allowJs": true,
    "resolveJsonModule": true,
    "moduleDetection": "force",
    "isolatedModules": true,

    /* Strictness */
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "checkJs": true,

    /* Bundled projects */
    "lib": ["dom", "dom.iterable", "ESNext"],
    "noEmit": true,
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "jsx": "preserve",
    "plugins": [{ "name": "next" }],
    "incremental": true,

    /* Path Aliases */
    "baseUrl": ".",
    "paths": {
      "~/*": ["./src/*"]
    },
    "types": ["cypress"]
  },
  "include": [
    ".eslintrc.cjs",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "**/*.cjs",
    "**/*.js",
    ".next/types/**/*.ts"
  ],
  "exclude": ["node_modules"]
}
yycheng-hkuspace commented 2 months ago

I got the same error

Char99s commented 2 months ago
{
  "compilerOptions": {
    /* Base Options: */
    "esModuleInterop": true,
    "skipLibCheck": true,
    "target": "ESNext",
    "allowJs": true,
    "resolveJsonModule": true,
    "moduleDetection": "force",
    "isolatedModules": true,

    /* Strictness */
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "checkJs": true,

    /* Bundled projects */
    "lib": ["dom", "dom.iterable", "ESNext"],
    "noEmit": true,
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "jsx": "preserve",
    "plugins": [{ "name": "next" }],
    "incremental": true,

    /* Path Aliases */
    "baseUrl": ".",
    "paths": {
      "~/*": ["./src/*"]
    },
    "types": ["cypress"]
  },
  "include": [
    ".eslintrc.cjs",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "**/*.cjs",
    "**/*.js",
    ".next/types/**/*.ts"
  ],
  "exclude": ["node_modules"]
}

Can you try these settings?

{ "compilerOptions": { "target": "ESNext", "module": "ESNext", "allowImportingTsExtensions": true, "moduleResolution": "Bundler", "noEmit": true, "esModuleInterop": true, "strict": true } }

takshch commented 2 months ago

I am getting the same error from many days in the vs code.

JacobFV commented 2 months ago

when did you start getting it?- JacobOn Sep 12, 2024, at 09:03, Taksh Chanana @.***> wrote: I am getting the same error from many days in the vs code.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

takshch commented 2 months ago

Screenshot from 2024-09-12 19-35-11

JacobFV commented 2 months ago

allowImportingTsExtensions

same

JacobFV commented 2 months ago

This fixes it for me:

in eslint.config.mjs, add the zod plugin:

import typescriptEslint from "@typescript-eslint/eslint-plugin";
import drizzle from "eslint-plugin-drizzle";
import zod from "eslint-plugin-zod";
import tsParser from "@typescript-eslint/parser";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
  baseDirectory: __dirname,
  recommendedConfig: js.configs.recommended,
  allConfig: js.configs.all
});

export default [...compat.extends(
  "next/core-web-vitals",
  "plugin:@typescript-eslint/recommended-type-checked",
  "plugin:@typescript-eslint/stylistic-type-checked",
), {
  plugins: {
    "@typescript-eslint": typescriptEslint,
    drizzle,
    zod,
  },

  languageOptions: {
    parser: tsParser,
    ecmaVersion: 5,
    sourceType: "script",

    parserOptions: {
      project: true,
    },
  },

  rules: {
    "zod/prefer-enum": 2,
    "zod/require-strict": 2,

    "@typescript-eslint/array-type": "off",
    "@typescript-eslint/consistent-type-definitions": "off",

    "@next/next/no-duplicate-head": "off",
    "import/no-anonymous-default-export": "off",

    "@typescript-eslint/consistent-type-imports": ["warn", {
      prefer: "type-imports",
      fixStyle: "inline-type-imports",
    }],

    "@typescript-eslint/no-unused-vars": ["warn", {
      argsIgnorePattern: "^_",
    }],

    "@typescript-eslint/require-await": "off",

    "@typescript-eslint/no-misused-promises": ["error", {
      checksVoidReturn: {
        attributes: false,
      },
    }],

    "@typescript-eslint/no-empty-object-type": "off",

    "drizzle/enforce-delete-with-where": ["error", {
      drizzleObjectName: ["db", "ctx.db"],
    }],

    "drizzle/enforce-update-with-where": ["error", {
      drizzleObjectName: ["db", "ctx.db"],
    }],

    "@typescript-eslint/no-unsafe-member-access": "off",
    "@typescript-eslint/no-unsafe-call": "off",
    "@typescript-eslint/no-unsafe-assignment": "off",
    "@typescript-eslint/no-unsafe-argument": "off",
  },
}];

Then install the dependencies:

bun add @eslint/js @eslint/eslintrc @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint eslint-plugin-react eslint-plugin-jsx-a11y eslint-plugin-import eslint-plugin-drizzle eslint-plugin-zod 

For reference, here's my package.json:

{
  "name": "aii",
  "version": "0.1.0",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "next build",
    "db:generate": "drizzle-kit generate",
    "db:migrate": "drizzle-kit migrate",
    "db:push": "drizzle-kit push",
    "db:studio": "drizzle-kit studio",
    "db:seed": "bun run src/server/db/seed.ts",
    "dev": "next dev",
    "lint": "next lint",
    "start": "next start"
  },
  "dependencies": {
    "@auth/drizzle-adapter": "^1.1.0",
    "@fullcalendar/daygrid": "^6.1.15",
    "@fullcalendar/interaction": "^6.1.15",
    "@fullcalendar/react": "^6.1.15",
    "@hookform/resolvers": "^3.9.0",
    "@radix-ui/react-alert-dialog": "^1.1.1",
    "@radix-ui/react-collapsible": "^1.1.0",
    "@radix-ui/react-context-menu": "^2.2.1",
    "@radix-ui/react-dialog": "^1.1.1",
    "@radix-ui/react-hover-card": "^1.1.1",
    "@radix-ui/react-icons": "^1.3.0",
    "@radix-ui/react-label": "^2.1.0",
    "@radix-ui/react-menubar": "^1.1.1",
    "@radix-ui/react-progress": "^1.1.0",
    "@radix-ui/react-select": "^2.1.1",
    "@radix-ui/react-separator": "^1.1.0",
    "@radix-ui/react-slider": "^1.2.0",
    "@radix-ui/react-slot": "^1.1.0",
    "@radix-ui/react-switch": "^1.1.0",
    "@radix-ui/react-tabs": "^1.1.0",
    "@radix-ui/react-toggle": "^1.1.0",
    "@radix-ui/react-toggle-group": "^1.1.0",
    "@radix-ui/react-tooltip": "^1.1.2",
    "@sendgrid/mail": "^8.1.3",
    "@t3-oss/env-nextjs": "^0.10.1",
    "@tanstack/react-query": "^5.50.0",
    "@trpc/client": "^11.0.0-rc.446",
    "@trpc/next": "^11.0.0-rc.446",
    "@trpc/react-query": "^11.0.0-rc.446",
    "@trpc/server": "^11.0.0-rc.446",
    "bcrypt": "^5.1.1",
    "class-variance-authority": "^0.7.0",
    "clsx": "^2.1.1",
    "drizzle-orm": "^0.33.0",
    "eslint-plugin-import": "^2.30.0",
    "eslint-plugin-jsx-a11y": "^6.10.0",
    "eslint-plugin-react": "^7.36.1",
    "geist": "^1.3.0",
    "lucide-react": "^0.441.0",
    "next": "^14.2.4",
    "next-auth": "^4.24.7",
    "node-cron": "^3.0.3",
    "nodemailer": "^6.9.15",
    "postgres": "^3.4.4",
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "react-hook-form": "^7.53.0",
    "superjson": "^2.2.1",
    "tailwind-merge": "^2.5.2",
    "tailwindcss-animate": "^1.0.7",
    "zod": "^3.23.8"
  },
  "devDependencies": {
    "@eslint/eslintrc": "^3.1.0",
    "@eslint/js": "^9.10.0",
    "@types/bcrypt": "^5.0.2",
    "@types/eslint": "^8.56.10",
    "@types/node": "^20.14.10",
    "@types/react": "^18.3.3",
    "@types/react-dom": "^18.3.0",
    "@typescript-eslint/eslint-plugin": "^8.6.0",
    "@typescript-eslint/parser": "^8.6.0",
    "drizzle-kit": "^0.24.0",
    "eslint": "^9.10.0",
    "eslint-config-next": "^14.2.4",
    "eslint-plugin-drizzle": "^0.2.3",
    "eslint-plugin-zod": "^1.4.0",
    "postcss": "^8.4.39",
    "prettier": "^3.3.2",
    "prettier-plugin-tailwindcss": "^0.6.5",
    "tailwindcss": "^3.4.3",
    "typescript": "^5.5.3"
  },
  "ct3aMetadata": {
    "initVersion": "7.37.0"
  }
}
ZachHandley commented 2 months ago

Okay so for those of us who don't have any linting rules and have this stupid error, what the heck is it

JacobFV commented 2 months ago

yeah linting configuration didn’t actually fix it for me either- JacobOn Sep 24, 2024, at 23:13, ZachHandley @.***> wrote: Okay so for those of us who don't have any linting rules and have this stupid error, what the heck is it

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

ZachHandley commented 2 months ago

@colinhacks can you please fix this or look at this, something got effed

❯ bun install
bun install v1.1.30-canary.34 (17d719fa)

+ vite-tsconfig-paths@5.0.1
+ @astrojs/check@0.9.3
+ @astrojs/cloudflare@11.1.0
+ @astrojs/partytown@2.1.2
+ @astrojs/sitemap@3.1.6
+ @astrojs/tailwind@5.1.1
+ @astrojs/vue@4.5.1
+ @directus/sdk@17.0.1
+ @iconify/vue@4.1.2
+ @inox-tools/astro-when@0.2.3
+ @nanostores/persistent@0.10.2
+ @nanostores/vue@0.10.0
+ @vueuse/core@11.1.0
+ astro@4.15.9
+ nanostores@0.11.3
+ sweetalert2@11.14.1
+ tailwindcss@3.4.13
+ typescript@5.6.2
+ vue@3.5.8
+ zastro-service-worker@0.0.3
+ zod@3.23.8

865 packages installed [164.71s]

okay great, so it's installed right? and I can use it right? image

okay............................................. idk what the hell to do here. No package manager properly resolves it and it's broken now on multiple projects.

JacobFV commented 2 months ago

i should clarify that this is not a eslint error. because even if i get eslint to shut up next build still fails

ZachHandley commented 2 months ago

^ correct. Something got effed.

g-pascal commented 1 month ago

I have the same issue with Typescript 5.6, but it works fine with Typescript 5.5.

I suspect the problem is related to this change (related Typescript pull request). This change was already in the typescript@5.5.1-rc prerelease, and I'm also encountering the issue when using this version.

navneetsingh-cpu commented 1 month ago

I fixed above issue by putting the following inside the tsconfig.json for your front end project NOT Amplify file:

{
  "compilerOptions": {
    ...
    "esModuleInterop": true
    ...
  }
}
takshch commented 1 month ago
  1. Add this to your package.json dependency (^ is the problem)

    "zod": "3.22.4"
  2. Remove node_modules(sudo rm -rf node_modules)

  3. Re-install npm modules (npm i)

JacobFV commented 1 month ago

careful with the sudo rm- JacobOn Oct 9, 2024, at 15:52, Taksh Chanana @.***> wrote:

Add this to your package.json dependency (^ is the problem)

"zod": "3.22.4"

Remove node_modules(sudo rm -rf node_modules) Re-install npm modules (npm i)

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

igoro00 commented 1 month ago
  1. Add this to your package.json dependency (^ is the problem)

^ is not the problem. ^ merely allows to get the newest 3.x.x version out there. The real problem is the specific version ^3 gets you and since I just installed zod in my new project my version looks like this "zod":"^3.23.8. Removing the ^ does literally nothing in my case.

But sure, I installed v3.22.4 and it worked, so then I kept upgrading zod until it broke. v3.23.7 works and v3.23.8 doesnt.

Also, v3.23.8 works with commonjs. It's just the ESM part that's broken.

catinrage commented 2 weeks ago

any update on the issue ? using pnpm instead of bun seems to resolve the issue.

JacobFV commented 2 weeks ago

hmm thank u ill stick with pnpm then- JacobOn Nov 12, 2024, at 03:22, catinrage @.***> wrote: any update on the issue ? using pnpm instead of bun seems to resolve the issue.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

fcamblor commented 1 day ago

I have the same issue with Typescript 5.6, but it works fine with Typescript 5.5.

I suspect the problem is related to this change (https://github.com/microsoft/TypeScript/pull/58825). This change was already in the typescript@5.5.1-rc prerelease, and I'm also encountering the issue when using this version.

Rolling back from typescript@5.6 to typescript@5.5 fixed the compilation error on my side

npm install --save-dev typescript@5.5