amplify-js icon indicating copy to clipboard operation
amplify-js copied to clipboard

Getting `ReferenceError: process is not defined` when attempting to import `Schema` into Astro React component

Open nadetastic opened this issue 1 year ago • 6 comments

Environment information

System:
  OS: macOS 14.4.1
  CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Memory: 75.01 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 18.18.2 - ~/.nvm/versions/node/v18.18.2/bin/node
  Yarn: undefined - undefined
  npm: 10.2.1 - ~/.nvm/versions/node/v18.18.2/bin/npm
  pnpm: 8.12.1 - /usr/local/bin/pnpm
NPM Packages:
  @aws-amplify/auth-construct: 1.2.0
  @aws-amplify/backend: 1.0.4
  @aws-amplify/backend-auth: 1.1.1
  @aws-amplify/backend-cli: 1.2.2
  @aws-amplify/backend-data: 1.1.0
  @aws-amplify/backend-deployer: 1.0.3
  @aws-amplify/backend-function: 1.3.0
  @aws-amplify/backend-output-schemas: 1.1.0
  @aws-amplify/backend-output-storage: 1.0.2
  @aws-amplify/backend-secret: 1.0.0
  @aws-amplify/backend-storage: 1.0.4
  @aws-amplify/cli-core: 1.1.1
  @aws-amplify/client-config: 1.1.2
  @aws-amplify/deployed-backend-client: 1.2.0
  @aws-amplify/form-generator: 1.0.0
  @aws-amplify/model-generator: 1.0.3
  @aws-amplify/platform-core: 1.0.4
  @aws-amplify/plugin-types: 1.1.0
  @aws-amplify/sandbox: 1.1.1
  @aws-amplify/schema-generator: 1.2.0
  aws-amplify: 6.5.0
  aws-cdk: 2.151.0
  aws-cdk-lib: 2.151.0
  typescript: 5.5.4
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

I'm trying out Astro with Amplify Gen2 and came across an error when attempting to import the Schema from defineData into a React component in Astro

Here are my components

todo/[...slug].astro

---
import { amplifyDataClient } from "@/components/react/ConfigureAmplifyClientSide";
import { TodoCard } from "@/components/react/TodoCard";
import type { Schema } from "amplify/data/resource"; // <-- this doesn't throw an error

export async function getStaticPaths() {
  const { data } = await amplifyDataClient.models.Todo.list();
  return data.map((todo) => ({
    params: { slug: todo.id },
    props: todo,
  }));
}

type Props = Schema["Todo"]["type"];
const todo = Astro.props;
---

<TodoCard todo={todo} client:load />

TodoCard.tsx


import { type Schema } from "amplify/data/resource"; // <-- This throws an error by simply importing it
type Todo = Schema["Todo"]["type"];

export const TodoCard = ({ todo }: { todo: Todo }) => {

  return JSON.stringify(todo);
}

With this I get the following error in the browser console

Screenshot 2024-08-13 at 2 51 14 PM

nadetastic avatar Aug 13 '24 05:08 nadetastic

Just solved this I think, the issue seems to be the syntax

import { type Schema } from "amplify/data/resource"; // <-- Throws an error
import type { Schema } from "amplify/data/resource"; // <-- Works 

Not sure if its something specific to astro, as I haven't had this issue before and Im relatively new to astro

nadetastic avatar Aug 13 '24 05:08 nadetastic

Hey,👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂

ykethan avatar Aug 13 '24 18:08 ykethan

Adding tsconfig

{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "react",
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    }
  }
}

nadetastic avatar Aug 13 '24 20:08 nadetastic

Import declarations written with import type, export declarations written with export type { ... }, and import or export specifiers prefixed with the type keyword are all guaranteed to be elided from the output JavaScript. https://www.typescriptlang.org/docs/handbook/modules/reference.html#type-only-imports-and-exports

While I'm not able to reproduce this in a .astro file I have not yet attempted to import in a React component, therefore this may be due to how those components are transpiled and passed to the bundler.

That being said this should be removed when verbatimModuleSyntax is set to true, as specified by Astro's base tsconfig

@nadetastic does this occur with import { type... in Astro components themselves?

josefaidt avatar Aug 14 '24 00:08 josefaidt

@josefaidt Nope that doesn't throw an error - seems specific to react components, haven't tried Vue/others

nadetastic avatar Aug 14 '24 03:08 nadetastic

Going to mark this as a bug for now since it's consistently reproducible. I couldn't find any existing bugs in the Astro repo or elsewhere. Seems unique to react components in Astro at the moment.

But, while it's a runtime issue, it doesn't seem to be related to data-schema. So, i'm going to transfer this to the JS repo.

chrisbonifacio avatar Aug 19 '24 18:08 chrisbonifacio

Closing this issue as it's not Amplify related, but rather on the Astro side of things. We'll monitor Astro repo for a related bug and cross link.

cwomack avatar Oct 24 '24 20:10 cwomack