# @astrojs/node This adapter allows Astro to deploy your SSR site to Node targets. - [Why Astro Node](#why-astro-node) - [Installation](#installation) - [Configuration](#configuration) - [Usage](#usage) - [Troubleshooting](#troubleshooting) - [Contributing](#contributing) - [Changelog](#changelog) ## Why @astrojs/node If you're using Astro as a static site builder—its behavior out of the box—you don't need an adapter. If you wish to [use server-side rendering (SSR)](https://docs.astro.build/en/guides/server-side-rendering/), Astro requires an adapter that matches your deployment runtime. [Node.js](https://nodejs.org/en/) is a JavaScript runtime for server-side code. @astrojs/node can be used either in standalone mode or as middleware for other http servers, such as [Express](https://expressjs.com/). ## Installation Add the Node adapter to enable SSR in your Astro project with the following `astro add` command. This will install the adapter and make the appropriate changes to your `astro.config.mjs` file in one step. ```sh # Using NPM npx astro add node # Using Yarn yarn astro add node # Using PNPM pnpm astro add node ``` If you prefer to install the adapter manually instead, complete the following two steps: 1. Install the Node adapter to your project’s dependencies using your preferred package manager. If you’re using npm or aren’t sure, run this in the terminal: ```bash npm install @astrojs/node ``` 1. Add two new lines to your `astro.config.mjs` project configuration file. ```js ins={3, 6-9} // astro.config.mjs import { defineConfig } from 'astro/config'; import node from '@astrojs/node'; export default defineConfig({ output: 'server', adapter: node({ mode: 'standalone', }), }); ``` ## Configuration @astrojs/node can be configured by passing options into the adapter function. The following options are available: ### Mode Controls whether the adapter builds to `middleware` or `standalone` mode. - `middleware` mode allows the built output to be used as middleware for another Node.js server, like Express.js or Fastify. ```js import { defineConfig } from 'astro/config'; import node from '@astrojs/node'; export default defineConfig({ output: 'server', adapter: node({ mode: 'middleware', }), }); ``` - `standalone` mode builds to server that automatically starts with the entry module is run. This allows you to more easily deploy your build to a host without any additional code. ## Usage First, [performing a build](https://docs.astro.build/en/guides/deploy/#building-your-site-locally). Depending on which `mode` selected (see above) follow the appropriate steps below: ### Middleware The server entrypoint is built to `./dist/server/entry.mjs` by default. This module exports a `handler` function that can be used with any framework that supports the Node `request` and `response` objects. For example, with Express: ```js import express from 'express'; import { handler as ssrHandler } from './dist/server/entry.mjs'; const app = express(); // Change this based on your astro.config.mjs, `base` option. // They should match. The default value is "/". const base = '/'; app.use(base, express.static('dist/client/')); app.use(ssrHandler); app.listen(8080); ``` Or, with Fastify (>4): ```js import Fastify from 'fastify'; import fastifyMiddie from '@fastify/middie'; import fastifyStatic from '@fastify/static'; import { fileURLToPath } from 'node:url'; import { handler as ssrHandler } from './dist/server/entry.mjs'; const app = Fastify({ logger: true }); await app .register(fastifyStatic, { root: fileURLToPath(new URL('./dist/client', import.meta.url)), }) .register(fastifyMiddie); app.use(ssrHandler); app.listen({ port: 8080 }); ``` Additionally, you can also pass in an object to be accessed with `Astro.locals` or in Astro middleware: ```js import express from 'express'; import { handler as ssrHandler } from './dist/server/entry.mjs'; const app = express(); app.use(express.static('dist/client/')); app.use((req, res, next) => { const locals = { title: 'New title', }; ssrHandler(req, res, next, locals); }); app.listen(8080); ``` Note that middleware mode does not do file serving. You'll need to configure your HTTP framework to do that for you. By default the client assets are written to `./dist/client/`. ### Standalone In standalone mode a server starts when the server entrypoint is run. By default it is built to `./dist/server/entry.mjs`. You can run it with: ```shell node ./dist/server/entry.mjs ``` For standalone mode the server handles file servering in addition to the page and API routes. #### Custom host and port You can override the host and port the standalone server runs on by passing them as environment variables at runtime: ```shell HOST=0.0.0.0 PORT=4321 node ./dist/server/entry.mjs ``` #### HTTPS By default the standalone server uses HTTP. This works well if you have a proxy server in front of it that does HTTPS. If you need the standalone server to run HTTPS itself you need to provide your SSL key and certificate. You can pass the path to your key and certification via the environment variables `SERVER_CERT_PATH` and `SERVER_KEY_PATH`. This is how you might pass them in bash: ```bash SERVER_KEY_PATH=./private/key.pem SERVER_CERT_PATH=./private/cert.pem node ./dist/server/entry.mjs ``` #### Runtime environment variables If an `.env` file containing environment variables is present when the build process is run, these values will be hard-coded in the output, just as when generating a static website. During the build, the runtime variables must be absent from the `.env` file, and you must provide Astro with every environment variable to expect at run-time: `VARIABLE_1=placeholder astro build`. This signals to Astro that the actual value will be available when the built application is run. The placeholder value will be ignored by the build process, and Astro will use the value provided at run-time. In the case of multiple run-time variables, store them in a seperate file (e.g. `.env.runtime`) from `.env`. Start the build with the following command: ```sh export $(cat .env.runtime) && astro build ``` ## Troubleshooting ### SyntaxError: Named export 'compile' not found You may see this when running the entry script if it was built with npm or Yarn. This is a known issue that may be fixed in a future release. As a workaround, add `"path-to-regexp"` to the `noExternal` array: ```js ins={9-13} // astro.config.mjs import { defineConfig } from 'astro/config'; import node from '@astrojs/node'; export default defineConfig({ output: 'server', adapter: node(), vite: { ssr: { noExternal: ['path-to-regexp'], }, }, }); ``` For more help, check out the `#support` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help! You can also check our [Astro Integration Documentation][astro-integration] for more on integrations. ## Contributing This package is maintained by Astro's Core team. You're welcome to submit an issue or PR! ## Changelog See [CHANGELOG.md](CHANGELOG.md) for a history of changes to this integration. [astro-integration]: https://docs.astro.build/en/guides/integrations-guide/ xpose-frontmatter-to-build-done-hook Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/SECURITY_CONTACTS (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2025-04-02[ci] formatGravatar Yury Michurin 2-97/+112
2025-04-02Allow to pass custom fetch for fetching pre-rendered errors (#13403)Gravatar Yury Michurin 11-514/+937
2025-04-02[ci] formatGravatar Marocco2 1-1/+1
2025-04-02feat: Add `eagerness` support for `prefetch()` (#13405)Gravatar Marocco2 2-2/+40
2025-04-02chore(deps): update tj-actions/changed-files action to v46.0.3 (#13497)Gravatar renovate[bot] 1-1/+1
2025-03-31[ci] release (#13513)astro@5.5.6@astrojs/vue@5.0.9@astrojs/svelte@7.0.9@astrojs/studio@0.1.6@astrojs/solid-js@5.0.7@astrojs/react@4.2.3@astrojs/preact@4.0.8@astrojs/netlify@6.2.5@astrojs/mdx@4.2.3@astrojs/markdoc@0.13.3@astrojs/db@0.14.10@astrojs/cloudflare@12.4.0@astrojs/alpinejs@0.4.5Gravatar Houston (Bot) 54-178/+238
2025-03-31fix: update `vite` to latest version (#13526)Gravatar Junseong Park 15-101/+118
2025-03-31[ci] formatGravatar Emanuele Stoppa 3-4/+2
2025-03-31fix(i18n): return value from `preferredLocale` (#13524)Gravatar Emanuele Stoppa 5-15/+51
2025-03-31chore(tailwind): delete integration (#13511)Gravatar Florian Lefebvre 16-1083/+1
2025-03-31fix(deps): update all non-major dependencies (#13521)Gravatar renovate[bot] 14-163/+163
2025-03-31[ci] formatGravatar Matt Kane 3-4/+3
2025-03-31feat(cloudflare): add KV session storage support (#13514)Gravatar Matt Kane 19-487/+490
2025-03-31fix(deps): update astro client runtimes (#13522)Gravatar renovate[bot] 4-6/+7
2025-03-27fix(deps): update astro dependencies (#13498)Gravatar renovate[bot] 47-412/+427
2025-03-27[ci] formatGravatar Emanuele Stoppa 3-8/+7
2025-03-27refactor(actions): use `Omit` to avoid leaking types to shared context (#13429)Gravatar Emanuele Stoppa 5-11/+41
2025-03-26[ci] release (#13504)astro@5.5.5@astrojs/vue@5.0.8@astrojs/tailwind@6.0.2@astrojs/svelte@7.0.8@astrojs/studio@0.1.5@astrojs/solid-js@5.0.6@astrojs/react@4.2.2@astrojs/preact@4.0.7@astrojs/netlify@6.2.4@astrojs/mdx@4.2.2@astrojs/markdoc@0.13.2@astrojs/db@0.14.9@astrojs/cloudflare@12.3.1@astrojs/alpinejs@0.4.4Gravatar Houston (Bot) 56-132/+197
2025-03-26[ci] formatGravatar Florian Lefebvre 2-2/+2
2025-03-26fix(astro): dynamically import actions (#13510)Gravatar Florian Lefebvre 12-51/+96
2025-03-26[ci] formatGravatar Matt Kane 2-2/+2
2025-03-26fix: cache raw cookie value and decode when getting (#13485)Gravatar Matt Kane 3-17/+37
2025-03-25fix: update vite (#13505)Gravatar Emanuele Stoppa 16-104/+123
2025-03-24[ci] formatGravatar Emanuele Stoppa 1-1/+1
2025-03-24fix(app): call renderer when routes don't match (#13483)Gravatar Emanuele Stoppa 4-1/+30
2025-03-21[ci] release (#13460)astro@5.5.4@astrojs/vercel@8.1.3@astrojs/tailwind@6.0.1@astrojs/svelte@7.0.7@astrojs/preact@4.0.6@astrojs/cloudflare@12.3.0Gravatar Houston (Bot) 45-118/+119
2025-03-21fix: better error handling on Stackblitz (#13484)Gravatar Matt Kane 2-0/+12
2025-03-21Repair server islands to work with client router (#13481)Gravatar Martin Trapp 2-6/+11
2025-03-21fix: generate correct external redirects (#13480)Gravatar Matt Kane 3-4/+21
2025-03-21fix(deps): update all non-major dependencies (#13440)Gravatar renovate[bot] 24-435/+436
2025-03-21[ci] formatGravatar Florian Lefebvre 2-2/+2
2025-03-21feat(cloudflare): global env (#13444)Gravatar Florian Lefebvre 6-47/+130
2025-03-21fix(deps): update astro client runtimes (#13474)Gravatar renovate[bot] 7-345/+348
2025-03-20Small change to linking style (#13472)Gravatar Chris Swithinbank 1-1/+1
2025-03-20Add deprecation notice to Tailwind integration README (#13471)Gravatar Chris Swithinbank 2-6/+11
2025-03-20[ci] formatGravatar Matt Kane 2-2/+1
2025-03-20fix(preact,svelte): empty target container before rendering `client:only` isl...Gravatar Matt Kane 5-5/+41
2025-03-20chore(renovate): group updates (#13466)Gravatar Emanuele Stoppa 1-11/+58
2025-03-19[ci] formatGravatar Matt Kane 1-3/+2
2025-03-19fix: don't attempt to move files after build with base (#13463)Gravatar Matt Kane 8-13/+66
2025-03-19[ci] formatGravatar Emanuele Stoppa 2-6/+18
2025-03-19fix(routing): don't add site to static redirects (#13447)Gravatar Emanuele Stoppa 7-10/+49
2025-03-18[ci] formatGravatar Matt Kane 2-2/+1
2025-03-18fix: set correct statusText for custom error pages (#13457)Gravatar Matt Kane 4-2/+19
2025-03-18chore(deps): update github-actions (#13459)Gravatar renovate[bot] 9-13/+13
2025-03-18chore(deps): update github-actions (#13458)Gravatar renovate[bot] 1-5/+5
2025-03-18[ci] formatGravatar Emanuele Stoppa 1-1/+3
2025-03-18chore: remove deprecated package (#13455)Gravatar Emanuele Stoppa 3-23/+16
2025-03-18fix(deps): update dependency miniflare to v4 (#13441)Gravatar renovate[bot] 2-31/+31