aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

Type error: Re-exporting a type when 'isolatedModules' is enabled requires using 'export type'. #13136

Closed mehrdad-shokri closed 6 months ago

mehrdad-shokri commented 6 months ago

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

Authentication

Amplify Version

v6

Amplify Categories

auth

Backend

Amplify CLI

Environment information

``` System: OS: macOS 14.4 CPU: (8) arm64 Apple M1 Memory: 146.94 MB / 8.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node Yarn: 3.2.4 - ~/.nvm/versions/node/v20.10.0/bin/yarn npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm pnpm: 8.8.0 - ~/Library/pnpm/pnpm Browsers: Chrome: 122.0.6261.129 Safari: 17.4 npmPackages: @ampproject/toolbox-optimizer: undefined () @babel/core: ^7.23.0 => undefined (7.24.0, ) @babel/runtime: 7.22.5 @edge-runtime/cookies: 4.0.2 @edge-runtime/ponyfill: 2.4.1 @edge-runtime/primitives: 4.0.2 @formatjs/cli: ^6.2.1 => 6.2.7 @formatjs/intl-localematcher: ^0.4.2 => 0.4.2 (0.5.4, 0.2.25, 0.2.32) @hapi/accept: undefined () @hookform/resolvers: ^3.3.2 => 3.3.4 @hookform/resolvers/ajv: 1.0.0 @hookform/resolvers/arktype: 1.0.0 @hookform/resolvers/class-validator: 1.0.0 @hookform/resolvers/computed-types: 1.0.0 @hookform/resolvers/io-ts: 1.0.0 @hookform/resolvers/joi: 1.0.0 @hookform/resolvers/nope: 1.0.0 @hookform/resolvers/superstruct: 1.0.0 @hookform/resolvers/typanion: 1.0.0 @hookform/resolvers/typebox: 1.0.0 @hookform/resolvers/valibot: 1.0.0 @hookform/resolvers/vest: 1.0.0 @hookform/resolvers/yup: 1.0.0 @hookform/resolvers/zod: 1.0.0 @ianvs/prettier-plugin-sort-imports: ^4.1.0 => 4.2.1 @mantine/carousel: ^7.6.2 => 7.6.2 @mantine/code-highlight: ^7.6.2 => 7.6.2 @mantine/core: ^7.6.2 => 7.6.2 @mantine/dates: ^7.6.2 => 7.6.2 @mantine/dropzone: ^7.6.2 => 7.6.2 @mantine/hooks: ^7.6.2 => 7.6.2 @mantine/modals: ^7.6.2 => 7.6.2 @mantine/notifications: ^7.6.2 => 7.6.2 @mantine/nprogress: ^7.6.2 => 7.6.2 @mantine/spotlight: ^7.6.2 => 7.6.2 @mantine/tiptap: ^7.6.2 => 7.6.2 @mswjs/interceptors: undefined () @napi-rs/triples: undefined () @next/bundle-analyzer: ^13.5.4 => 13.5.6 @next/eslint-plugin-next: ^13.5.4 => 13.5.6 @next/font: undefined () @next/react-dev-overlay: undefined () @opentelemetry/api: undefined () @segment/ajv-human-errors: undefined () @sentry/nextjs: ^7.77.0 => 7.107.0 @storybook/addon-essentials: ^7.4.6 => 7.6.17 @storybook/addon-styling: ^1.3.7 => 1.3.7 @storybook/blocks: ^7.4.6 => 7.6.17 @storybook/nextjs: ^7.4.6 => 7.6.17 @storybook/react: ^7.4.6 => 7.6.17 @tabler/icons-react: ^2.40.0 => 2.47.0 @tanstack/query-codemods: 4.24.3 @tanstack/react-query: ^5.8.4 => 5.28.4 @testing-library/dom: ^9.3.3 => 9.3.4 @testing-library/jest-dom: ^6.1.3 => 6.4.2 @testing-library/react: ^14.0.0 => 14.2.1 @testing-library/user-event: ^14.5.1 => 14.5.2 @theme-toggles/react: ^4.1.0 => 4.1.0 @tiptap/extension-link: ^2.1.12 => 2.2.4 @tiptap/react: ^2.1.12 => 2.2.4 @tiptap/starter-kit: ^2.1.12 => 2.2.4 @types/jest: ^29.5.5 => 29.5.12 @types/negotiator: ^0.6.2 => 0.6.3 @types/node: ^20.8.3 => 20.11.28 (17.0.45, 18.19.24) @types/react: 18.2.25 => 18.2.25 @types/react-lottie: ^1.2.9 => 1.2.10 @typescript-eslint/eslint-plugin: ^7.2.0 => 7.2.0 @typescript-eslint/parser: ^7.2.0 => 7.2.0 @vercel/analytics: ^1.1.1 => 1.2.2 @vercel/nft: undefined () @vercel/og: 0.5.15 acorn: undefined () amphtml-validator: undefined () animate.css: ^4.1.1 => 4.1.1 anser: undefined () arg: undefined () assert: undefined () async-retry: undefined () async-sema: undefined () aws-amplify: ^6.0.20 => 6.0.20 aws-amplify/adapter-core: undefined () aws-amplify/analytics: undefined () aws-amplify/analytics/kinesis: undefined () aws-amplify/analytics/kinesis-firehose: undefined () aws-amplify/analytics/personalize: undefined () aws-amplify/analytics/pinpoint: undefined () aws-amplify/api: undefined () aws-amplify/api/server: undefined () aws-amplify/auth: undefined () aws-amplify/auth/cognito: undefined () aws-amplify/auth/cognito/server: undefined () aws-amplify/auth/enable-oauth-listener: undefined () aws-amplify/auth/server: undefined () aws-amplify/datastore: undefined () aws-amplify/in-app-messaging: undefined () aws-amplify/in-app-messaging/pinpoint: undefined () aws-amplify/push-notifications: undefined () aws-amplify/push-notifications/pinpoint: undefined () aws-amplify/storage: undefined () aws-amplify/storage/s3: undefined () aws-amplify/storage/s3/server: undefined () aws-amplify/storage/server: undefined () aws-amplify/utils: undefined () babel-loader: ^9.1.3 => 9.1.3 babel-packages: undefined () babel-plugin-formatjs: ^10.5.7 => 10.5.13 browserify-zlib: undefined () browserslist: undefined () buffer: undefined () bytes: undefined () ci-info: undefined () cli-select: undefined () client-only: 0.0.1 comment-json: undefined () compression: undefined () conf: undefined () constants-browserify: undefined () constructs: ^10.3.0 => 10.3.0 content-disposition: undefined () content-type: undefined () cookie: undefined () cross-spawn: undefined () crypto-browserify: undefined () css.escape: undefined () data-uri-to-buffer: undefined () dayjs: ^1.11.10 => 1.11.10 debug: undefined () devalue: undefined () domain-browser: undefined () edge-runtime: undefined () eslint: ^8.51.0 => 8.57.0 eslint-config-airbnb: 19.0.4 => 19.0.4 eslint-config-airbnb-typescript: ^18.0.0 => 18.0.0 eslint-config-mantine: 3.2.0 => 3.2.0 eslint-config-prettier: ^9.0.0 => 9.1.0 eslint-plugin-import: ^2.28.1 => 2.29.1 eslint-plugin-jest: ^27.4.2 => 27.9.0 eslint-plugin-jsx-a11y: ^6.7.1 => 6.8.0 eslint-plugin-prettier: ^5.0.0 => 5.1.3 eslint-plugin-react: ^7.33.2 => 7.34.1 eslint-plugin-react-hooks: ^4.6.0 => 4.6.0 eslint-plugin-testing-library: ^6.0.2 => 6.2.0 eslint-plugin-unused-imports: ^3.0.0 => 3.1.0 events: undefined () find-cache-dir: undefined () find-up: undefined () firebase: ^10.6.0 => 10.9.0 firebase/analytics: undefined () firebase/app: undefined () firebase/app-check: undefined () firebase/auth: undefined () firebase/auth/cordova: undefined () firebase/auth/web-extension: undefined () firebase/compat: undefined () firebase/compat/analytics: undefined () firebase/compat/app: undefined () firebase/compat/app-check: undefined () firebase/compat/auth: undefined () firebase/compat/database: undefined () firebase/compat/firestore: undefined () firebase/compat/functions: undefined () firebase/compat/installations: undefined () firebase/compat/messaging: undefined () firebase/compat/performance: undefined () firebase/compat/remote-config: undefined () firebase/compat/storage: undefined () firebase/database: undefined () firebase/firestore: undefined () firebase/firestore/lite: undefined () firebase/functions: undefined () firebase/installations: undefined () firebase/messaging: undefined () firebase/messaging/sw: undefined () firebase/performance: undefined () firebase/remote-config: undefined () firebase/storage: undefined () framer-motion: ^10.16.4 => 10.18.0 fresh: undefined () get-orientation: undefined () glob: undefined () gzip-size: undefined () http-proxy: undefined () http-proxy-agent: undefined () https-browserify: undefined () https-proxy-agent: undefined () icss-utils: undefined () ignore-loader: undefined () image-size: undefined () is-animated: undefined () is-docker: undefined () is-wsl: undefined () jest: ^29.7.0 => 29.7.0 jest-environment-jsdom: ^29.7.0 => 29.7.0 jest-worker: undefined () json5: undefined () jsonwebtoken: undefined () loader-runner: undefined () loader-utils: undefined () lodash.curry: undefined () lru-cache: undefined () micromatch: undefined () mini-css-extract-plugin: undefined () mongodb: ^6.2.0 => 6.5.0 mysql2: ^3.6.3 => 3.9.2 nanoid: undefined () native-url: undefined () negotiator: ^0.6.3 => 0.6.3 neo-async: undefined () next: 14.0.4 => 14.0.4 next-intl: 3.4.0 => 3.4.0 node-fetch: undefined () node-html-parser: undefined () openai: ^4.24.1 => 4.29.1 ora: undefined () os-browserify: undefined () p-limit: undefined () path-browserify: undefined () platform: undefined () postcss: ^8.4.31 => 8.4.35 (8.4.31) postcss-flexbugs-fixes: undefined () postcss-modules-extract-imports: undefined () postcss-modules-local-by-default: undefined () postcss-modules-scope: undefined () postcss-modules-values: undefined () postcss-preset-env: undefined () postcss-preset-mantine: 1.13.0 => 1.13.0 postcss-safe-parser: undefined () postcss-scss: undefined () postcss-simple-vars: ^7.0.1 => 7.0.1 postcss-value-parser: undefined () prettier: ^3.0.3 => 3.2.5 (2.8.8) process: undefined () punycode: undefined () querystring-es3: undefined () raw-body: undefined () react: 18.2.0 => 18.2.0 react-builtin: undefined () react-country-flag: ^3.1.0 => 3.1.0 react-dom: 18.2.0 => 18.2.0 react-dom-builtin: undefined () react-dom-experimental-builtin: undefined () react-experimental-builtin: undefined () react-hook-form: ^7.48.2 => 7.51.1 react-icons: ^4.11.0 => 4.12.0 react-is: 18.2.0 react-refresh: 0.12.0 react-server-dom-turbopack-builtin: undefined () react-server-dom-turbopack-experimental-builtin: undefined () react-server-dom-webpack-builtin: undefined () react-server-dom-webpack-experimental-builtin: undefined () regenerator-runtime: 0.13.4 sass-loader: undefined () scheduler-builtin: undefined () scheduler-experimental-builtin: undefined () schema-utils: undefined () semver: undefined () send: undefined () server-only: 0.0.1 setimmediate: undefined () shell-quote: undefined () source-map: undefined () stacktrace-parser: undefined () storybook: ^7.4.6 => 7.6.17 storybook-dark-mode: ^3.0.1 => 3.0.3 stream-browserify: undefined () stream-http: undefined () string-hash: undefined () string_decoder: undefined () strip-ansi: undefined () superstruct: undefined () tar: undefined () terser: undefined () text-table: undefined () timers-browserify: undefined () ts-jest: ^29.1.1 => 29.1.2 tty-browserify: undefined () typescript: ^5.3.3 => 5.4.2 typewriter-effect: ^2.21.0 => 2.21.0 ua-parser-js: undefined () unistore: undefined () util: undefined () vm-browserify: undefined () watchpack: undefined () web-vitals: undefined () webpack: undefined () webpack-sources: undefined () ws: undefined () zod: ^3.22.4 => 3.22.4 () npmGlobalPackages: @aws-amplify/cli: 12.10.1 aws-cdk: 2.113.0 corepack: 0.22.0 npm: 10.2.3 ```

Describe the bug

When trying to build next.js app router project, I get error

./node_modules/@aws-amplify/auth/src/providers/cognito/index.ts:31:2
Type error: Re-exporting a type when 'isolatedModules' is enabled requires using 'export type'.

I think it's because type exports are not exported as export type.(this is required to do if isolatedModules is set to true) I can't set isolateModules to false in my project because it's mandatory by next.js to be true.(when trying to set it to false it resets it to false when building the project.) I can submit a pr to fix this issue if you want. I saw this prior bug report but there was not proper handling of this issue.

Expected behavior

Build the project successfully without errors.

Reproduction steps

  1. Install next.js app router project
  2. Add amplify to the project by npm create amplify
  3. Import some auth utilities in the project
  4. Try to build the project with npm run build

Code Snippet

import {
  confirmSignUp,
  getCurrentUser,
  resendSignUpCode,
  signIn,
  signInWithRedirect,
  SignInWithRedirectInput,
  signOut,
  signUp,
} from 'aws-amplify/auth';

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

mehrdad-shokri commented 6 months ago

I have found out the root reason is importing import { SignInWithRedirectInput } from "@aws-amplify/auth/src/providers/cognito/types"; which is for declaring my function's parameter type. After changing the import from @aws-amplify/auth the problem was fixed.

mehrdad-shokri commented 6 months ago

I ended up using react-hook-form to handle form submission/validation. To pass the control exported from useForm call you can use something like this:

import {TextInput} from '@mantine/core';
import {Controller, FieldValues} from 'react-hook-form';

import {TextFieldControllerProps} from '@/types/props';

import styles from './TextFieldController.module.css';

const TextFieldController = <T extends FieldValues>({
  control,
  label,
  placeholder,
  name,
  ...textFieldProps
}: TextFieldControllerProps<T>) => {
  return (
    <Controller
      name={name}
      control={control}
      render={({field}) => (
        <TextInput
          label={label}
          required
          withAsterisk={false}
          placeholder={placeholder}
          mb='lg'
          size='md'
          classNames={{
            label: styles.label,
          }}
          {...textFieldProps}
          {...field}
        />
      )}
    />
  );
};

export default TextFieldController;

error states and everything gets handled automatically. I didn't use the @mantine/form solution because of form state not being provided.

nadetastic commented 6 months ago

HI @mehrdad-shokri thank you for opening this issue. Just to confirm your original issue was resolved by updating where your imports were coming from, correct?

Ex: import { SignInWithRedirectInput } from "@aws-amplify/auth/src/providers/cognito/types" vs import { SignInWithRedirectInput } from "aws-amplify/auth"

Let me know if this is not the case or if you are still having questions.

mehrdad-shokri commented 6 months ago

Hello, yes the issue is resolved now after changing the import path to the mentioned ones.