Amplify CLI JavaScript Frontend Plugin
The `amplify-frontend-javascript` package functions as an essential internal plugin for the AWS Amplify Command Line Interface (CLI), dedicated to streamlining the configuration and management of JavaScript and TypeScript frontend projects. It is primarily utilized by the Amplify CLI to correctly scaffold, build, and deploy applications that integrate with various AWS backend services such as authentication (Amazon Cognito), APIs (AWS AppSync, Amazon API Gateway), and storage (Amazon S3). Unlike the main `amplify-cli` which currently operates at version `14.x` and experiences frequent updates, this specific plugin (version `3.7.6`) maintains its own, more stable release cadence. This indicates that its core functionality, which revolves around configuring the client-side `aws-exports.js` file and facilitating frontend deployments, is mature and undergoes fewer modifications. Developers do not directly import this package into their application code; instead, it is an underlying component of the Amplify CLI workflow, distinguishing it from the `aws-amplify` client library that developers use in their frontend applications. Its primary value lies in providing seamless integration within the Amplify ecosystem for developing full-stack applications.
Common errors
-
No Amplify configuration found. Please configure Amplify in your app.
cause The `aws-exports.js` file was not imported, or `Amplify.configure()` was not called with the configuration object.fixEnsure `import config from './aws-exports'; Amplify.configure(config);` is present and correctly located in your application's entry point. -
AuthError: Current user is not authenticated
cause An operation requiring an authenticated user (e.g., accessing protected API routes, storage) was attempted without a valid user session.fixImplement proper authentication flows (signup, signin, session checks) using `Amplify.Auth` methods before attempting protected operations. Check user login state with `Amplify.Auth.currentAuthenticatedUser()` or similar methods. -
Type 'Observable<object>' is not assignable to type 'any'
cause Often seen in TypeScript projects when using older versions of Amplify's API category or when observables are not correctly handled, specifically with GraphQL subscriptions.fixEnsure you are using `aws-amplify` v5 or higher and have `@aws-amplify/api` installed if using subscriptions. Explicitly type the observables or cast them where appropriate, or use `async/await` for GraphQL queries/mutations. -
Error: Command 'amplify add frontend' is not a valid amplify command.
cause The installed `amplify-cli` version is too old or the plugin for handling 'frontend' commands is missing/corrupted.fixUpdate the Amplify CLI to the latest version globally (`npm update -g @aws-amplify/cli`) and ensure all related frontend plugins are correctly installed/updated.
Warnings
- gotcha The package `amplify-frontend-javascript` is a CLI plugin and not meant for direct import into your application code. Application code should import from the `aws-amplify` client library.
- breaking Amplify CLI and client library versions (e.g., `aws-amplify@5` to `aws-amplify@6`) can introduce breaking changes, especially in authentication workflows, API client generation, and UI components. Always consult the migration guide for major version upgrades.
- gotcha The `aws-exports.js` configuration file generated by the CLI must be correctly imported and passed to `Amplify.configure()`. Changes to your Amplify backend (e.g., adding a new API or Auth category) require running `amplify push` and often regenerating `aws-exports.js`.
- gotcha Module resolution issues (CommonJS vs. ESM) can arise in some JavaScript bundler configurations, especially in projects migrating to pure ESM or using older build tools. `aws-amplify` aims for universal compatibility but might require specific bundler configurations.
- breaking Recent Amplify CLI versions (v14.x onwards) are migrating categories to AWS SDK v3. While `amplify-frontend-javascript` itself might not be directly affected, custom resources or functions that interact with AWS SDKs might experience breaking changes if not updated.
Install
-
npm install amplify-frontend-javascript -
yarn add amplify-frontend-javascript -
pnpm add amplify-frontend-javascript
Imports
- Amplify
const Amplify = require('aws-amplify');import { Amplify } from 'aws-amplify'; - Auth
import Auth from 'aws-amplify/lib/Auth';
import { Auth } from 'aws-amplify'; - API
import { API } from '@aws-amplify/api';import { API } from 'aws-amplify';
Quickstart
import { Amplify } from 'aws-amplify';
import config from './aws-exports';
Amplify.configure(config);
// Example: Authenticate a user
async function signUp() {
try {
const { user } = await Amplify.Auth.signUp({
username: 'testuser',
password: 'password123',
attributes: {
email: 'test@example.com' // optional
}
});
console.log('User signed up successfully:', user);
} catch (error) {
console.error('Error signing up:', error);
}
}
// Example: Fetch data from a GraphQL API
async function fetchData() {
try {
const apiData = await Amplify.API.graphql({
query: `
query ListTodos {
listTodos {
items {
id
name
description
}
}
}
`
});
console.log('API Data:', apiData);
} catch (error) {
console.error('Error fetching data:', error);
}
}
signUp(); // Or fetchData();