Error when trying to deploy Astro/Sanity project to Netlify

65 Views Asked by At

I got an error trying to deploy an Astro/Sanity project to Netlify.

I have followed the official integration and instructions provided here: Sanity Astro integration, and here: Astro Netlify integration amongst many other ressources, but as far as I am concerned, some part are either missing or hard to find/put together.

To give the context of the project, here's the folder/file tree:

frontend
 ┣ .netlify
 ┃ ┣ functions-internal
 ┃ ┃ ┗ ssr
 ┃ ┃ ┃ ┣ chunks
 ┃ ┃ ┃ ┃ ┣ astro
 ┃ ┃ ┃ ┃ ┣ pages
 ┣ .vscode
 ┣ node-modules
 ┣ public
 ┣ src
 ┃ ┣ assets
 ┃ ┃ ┗ images
 ┃ ┣ components
 ┃ ┃ ┣ ui
 ┃ ┣ layouts
 ┃ ┣ lib
 ┃ ┣ pages
 ┃ ┃ ┣ api
 ┃ ┃ ┣ project
 ┃ ┣ sanity
 ┃ ┣ styles
 ┃ ┗ env.d.ts
 ┣ .env
 ┣ .gitignore
 ┣ .prettierignore
 ┣ .prettierrc.mjs
 ┣ README.md
 ┣ astro.config.ts
 ┣ components.json
 ┣ config.ts
 ┣ package-lock.json
 ┣ package.json
 ┣ sanity.config.ts
 ┣ tailwind.config.ts
 ┗ tsconfig.json

studio
 ┣ .sanity
 ┃ ┗ runtime
 ┣ dist
 ┃ ┣ static
 ┃ ┣ favicon.ico
 ┃ ┗ index.html
 ┣ node-modules
 ┣ schemas
 ┣ static
 ┣ .eslintrc
 ┣ .gitignore
 ┣ README.md
 ┣ package-lock.json
 ┣ package.json
 ┣ sanity.cli.ts
 ┣ sanity.config.ts
 ┗ tsconfig.json

The frontend folder, package.json file:

{
  "name": "modern-portfolio",
  "type": "module",
  "version": "0.0.1",
  "scripts": {
    "dev": "astro dev",
    "start": "astro dev",
    "format": "prettier -w . --cache",
    "build": "astro check && astro build",
    "preview": "astro preview"
  },
  "dependencies": {
    "@astrojs/check": "^0.4.1",
    "@astrojs/netlify": "^5.1.3",
    "@astrojs/react": "^3.0.10",
    "@astrojs/tailwind": "^5.1.0",
    "@hookform/resolvers": "^3.3.4",
    "@radix-ui/react-accordion": "^1.1.2",
    "@radix-ui/react-avatar": "^1.0.4",
    "@radix-ui/react-label": "^2.0.2",
    "@radix-ui/react-navigation-menu": "^1.1.4",
    "@radix-ui/react-slot": "^1.0.2",
    "@radix-ui/react-toast": "^1.1.5",
    "@sanity/astro": "^2.2.0",
    "@sanity/client": "^6.12.4",
    "astro": "^4.3.1",
    "astro-portabletext": "^0.9.6",
    "class-variance-authority": "^0.7.0",
    "clsx": "^2.1.0",
    "groqd": "^0.15.10",
    "lucide-react": "^0.321.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-hook-form": "^7.50.1",
    "resend": "^3.2.0",
    "sanity": "^3.28.0",
    "sanity-image": "^0.1.7",
    "tailwind-merge": "^2.2.1",
    "tailwindcss": "^3.4.1",
    "tailwindcss-animate": "^1.0.7",
    "zod": "^3.22.4"
  },
  "devDependencies": {
    "@types/node": "^20.11.16",
    "@types/react": "^18.2.55",
    "@types/react-dom": "^18.2.19",
    "prettier": "^3.2.5",
    "prettier-plugin-astro": "^0.13.0",
    "prettier-plugin-astro-organize-imports": "^0.4.1",
    "prettier-plugin-tailwindcss": "^0.5.11",
    "typescript": "^5.3.3"
  }
}

Frontend folder, astro.config.ts file:

import { sanityIntegration } from '@sanity/astro'
import { defineConfig } from 'astro/config'
import react from '@astrojs/react'
import tailwind from '@astrojs/tailwind'
import { HOMEPAGE_URL } from './config'
import netlify from '@astrojs/netlify'

// https://astro.build/config
export default defineConfig({
  output: 'server',
  site: HOMEPAGE_URL,
  integrations: [
    react(),
    tailwind({
      applyBaseStyles: false,
    }),
    sanityIntegration({
      projectId: import.meta.env.VITE_SANITY_PROJECT_ID,
      dataset: 'production',
      // Set useCdn to false if you're building statically.
      useCdn: false,
      token: import.meta.env.VITE_SANITY_TOKEN,
      studioBasePath: '/admin',
    }),
  ],
  adapter: netlify(),
})

Frontend folder, sanity.config.ts file:

import { defineConfig } from 'sanity'
import { structureTool } from 'sanity/structure'

import { schemaTypes } from '../studio/schemas'

export default defineConfig({
  name: 'default',
  title: 'portfolio',
  projectId: 'xxxxxxxx',
  dataset: 'production',
  plugins: [structureTool()],
  schema: {
    types: schemaTypes,
  },
})

Studio folder, sanity.config.ts file:

import {defineConfig} from 'sanity'
import {structureTool} from 'sanity/structure'
import {visionTool} from '@sanity/vision'
import {schemaTypes} from './schemas'

export default defineConfig({
  name: 'default',
  title: 'portfolio',

  projectId: 'xxxxxxxx',
  dataset: 'production',

  plugins: [structureTool(), visionTool()],

  schema: {
    types: schemaTypes,
  },
})

Studio folder, sanity.cli.ts file:

import {defineCliConfig} from 'sanity/cli'

export default defineCliConfig({
  api: {
    projectId: 'xxxxxxxx',
    dataset: 'production'
  }
})

Finally, the Netlify deploy settings:

Runtime
    Not set
Base directory
    frontend
Package directory
    Not set
Build command
    npm run build
Publish directory
    Not set
Functions directory
    frontend/.netlify/functions-internal/
Deploy log visibility
    Logs are public
Build status
    Active

The error occurs at build time, and here's the full log:

5:18:42 PM: build-image version: fcb0c1b3ada6d25c1cb58e8bc514f5f23cc14f15 (focal)
5:18:42 PM: buildbot version: 742cd4f739071a396139ba5020a96a927941b57d
5:18:42 PM: Building without cache
5:18:42 PM: Starting to prepare the repo for build
5:18:42 PM: No cached dependencies found. Cloning fresh repo
5:18:42 PM: git clone --filter=blob:none https://github.com/Remondo02/modern-portfolio
5:18:43 PM: Preparing Git Reference refs/heads/main
5:18:44 PM: Custom publish path detected. Proceeding with the specified path: "frontend"
5:18:44 PM: Custom functions path detected. Proceeding with the specified path: "frontend/.netlify/functions-internal"
5:18:44 PM: Starting to install dependencies
5:18:44 PM: Python version set to 3.8
5:18:45 PM: Attempting Ruby version 2.7.2, read from environment
5:18:45 PM: Using Ruby version 2.7.2
5:18:46 PM: Started restoring cached go cache
5:18:46 PM: Finished restoring cached go cache
5:18:47 PM: go version go1.19.13 linux/amd64
5:18:48 PM: Using PHP version 8.0
5:18:49 PM: Downloading and installing node v18.19.1...
5:18:49 PM: Downloading https://nodejs.org/dist/v18.19.1/node-v18.19.1-linux-x64.tar.xz...
5:18:49 PM: Computing checksum with sha256sum
5:18:49 PM: Checksums matched!
5:18:51 PM: Now using node v18.19.1 (npm v10.2.4)
5:18:52 PM: Enabling Node.js Corepack
5:18:52 PM: Started restoring cached build plugins
5:18:52 PM: Finished restoring cached build plugins
5:18:52 PM: Started restoring cached corepack dependencies
5:18:52 PM: Finished restoring cached corepack dependencies
5:18:52 PM: No npm workspaces detected
5:18:52 PM: Started restoring cached node modules
5:18:52 PM: Finished restoring cached node modules
5:18:52 PM: Installing npm packages using npm version 10.2.4
5:19:17 PM: added 1144 packages, and audited 1145 packages in 25s
5:19:17 PM: 254 packages are looking for funding
5:19:17 PM:   run `npm fund` for details
5:19:17 PM: found 0 vulnerabilities
5:19:17 PM: npm packages installed
5:19:17 PM: Successfully installed dependencies
5:19:17 PM: Starting build script
5:19:18 PM: Detected 1 framework(s)
5:19:18 PM: "astro" at version "4.4.5"
5:19:18 PM: Section completed: initializing
5:19:20 PM: ​
5:19:20 PM: Netlify Build                                                 
5:19:20 PM: ────────────────────────────────────────────────────────────────
5:19:20 PM: ​
5:19:20 PM: ❯ Version
5:19:20 PM:   @netlify/build 29.36.0
5:19:20 PM: ​
5:19:20 PM: ❯ Flags
5:19:20 PM:   baseRelDir: true
5:19:20 PM:   buildId: 65deec56334cc4bd9964a1a7
5:19:20 PM:   deployId: 65deec56334cc4bd9964a1a9
5:19:20 PM: ​
5:19:20 PM: ❯ Current directory
5:19:20 PM:   /opt/build/repo/frontend
5:19:20 PM: ​
5:19:20 PM: ❯ Config file
5:19:20 PM:   No config file was defined: using default values.
5:19:20 PM: ​
5:19:20 PM: ❯ Context
5:19:20 PM:   production
5:19:20 PM: ​
5:19:20 PM: Build command from Netlify app                                
5:19:20 PM: ────────────────────────────────────────────────────────────────
5:19:20 PM: ​
5:19:20 PM: $ npm run build
5:19:20 PM: > [email protected] build
5:19:20 PM: > astro check && astro build
5:19:22 PM: https://www.johndoe.dev
5:19:22 PM: 08:19:22 [check] Getting diagnostics for Astro files in /opt/build/repo/frontend...
5:19:26 PM: Result (29 files): 
5:19:26 PM: - 0 errors
5:19:26 PM: - 0 warnings
5:19:26 PM: - 0 hints
5:19:26 PM: 
5:19:27 PM: https://www.johndoe.dev
5:19:27 PM: 08:19:27 [WARN] [config] The feature "assets" is experimental and subject to change (used by @astrojs/netlify).
5:19:27 PM: 08:19:27 [build] output: "server"
5:19:27 PM: 08:19:27 [build] directory: /opt/build/repo/frontend/dist/
5:19:27 PM: 08:19:27 [build] adapter: @astrojs/netlify
5:19:27 PM: 08:19:27 [build] Collecting build info...
5:19:27 PM: 08:19:27 [build] ✓ Completed in 81ms.
5:19:27 PM: 08:19:27 [build] Building server entrypoints...
5:19:29 PM: 08:19:29 [vite] ✓ built in 1.62s
5:19:29 PM: 08:19:29 [build] ✓ Completed in 1.65s.
5:19:29 PM:  building client (vite) 
5:19:29 PM: 08:19:29 [vite] transforming...
5:19:34 PM: Failed during stage "building site": Build script returned non-zero exit code: 2
5:19:34 PM: 08:19:34 [vite] ✓ 2187 modules transformed.
5:19:34 PM: 08:19:34 [ERROR] [vite] x Build failed in 4.37s
5:19:34 PM: [vite]: Rollup failed to resolve import "sanity" from "/opt/build/repo/studio/schemas/blockContent.ts".
5:19:34 PM: This is most likely unintended because it can break your application at runtime.
5:19:34 PM: If you do want to externalize this module explicitly add it to
5:19:34 PM: `build.rollupOptions.external`
5:19:34 PM:   Stack trace:
5:19:34 PM:     at viteWarn (file:///opt/build/repo/frontend/node_modules/vite/dist/node/chunks/dep-jDlpJiMN.js:67078:27)
5:19:34 PM:     at onRollupWarning (file:///opt/build/repo/frontend/node_modules/vite/dist/node/chunks/dep-jDlpJiMN.js:67103:9)
5:19:34 PM:     at file:///opt/build/repo/frontend/node_modules/rollup/dist/es/shared/node-entry.js:18308:13
5:19:34 PM:     at ModuleLoader.handleInvalidResolvedId (file:///opt/build/repo/frontend/node_modules/rollup/dist/es/shared/node-entry.js:18909:26)
5:19:34 PM: ​
5:19:34 PM: "build.command" failed                                        
5:19:34 PM: ────────────────────────────────────────────────────────────────
5:19:34 PM: ​
5:19:34 PM:   Error message
5:19:34 PM:   Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
5:19:34 PM: ​
5:19:34 PM:   Error location
5:19:34 PM:   In Build command from Netlify app:
5:19:34 PM:   npm run build
5:19:34 PM: ​
5:19:34 PM:   Resolved config
5:19:34 PM:   build:
5:19:34 PM:     base: /opt/build/repo/frontend
5:19:34 PM:     command: npm run build
5:19:34 PM:     commandOrigin: ui
5:19:34 PM:     environment:
5:19:34 PM:       - VITE_RESEND_API_KEY
5:19:34 PM:       - VITE_SANITY_PROJECT_ID
5:19:34 PM:       - VITE_SANITY_TOKEN
5:19:34 PM:     publish: /opt/build/repo/frontend
5:19:34 PM:     publishOrigin: default
5:19:34 PM:   functionsDirectory: /opt/build/repo/frontend/.netlify/functions-internal
5:19:34 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
5:19:34 PM: Failing build: Failed to build site
5:19:35 PM: Finished processing build request in 52.497s

I would really appreciate some help because right now, the only option I have to update the live version is to do npm run build followed by netlify deploy --prod on the frontend folder in my terminal.

I tried to follow the instructions provides on the various ressources available online, but without any success.

0

There are 0 best solutions below