From 8cd6bd3fc3ee7b7eee887092a086cfb1e9c639db Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Sat, 2 Dec 2017 18:13:39 +0100 Subject: [PATCH] Add check for writeable directory (#3370) * Add check for writeable directory Followup of https://github.com/zeit/now-cli/issues/175 * Add link to docs --- errors/build-dir-not-writeable.md | 30 ++++++++++++++++++++++++++++++ readme.md | 14 +++++++++++++- server/build/index.js | 8 ++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 errors/build-dir-not-writeable.md diff --git a/errors/build-dir-not-writeable.md b/errors/build-dir-not-writeable.md new file mode 100644 index 00000000..0f654b50 --- /dev/null +++ b/errors/build-dir-not-writeable.md @@ -0,0 +1,30 @@ +# Build directory not writeable + +#### Why This Error Occurred + +The filesystem does not allow writing to the specified directory. A common cause for this error is starting a [custom server](https://github.com/zeit/next.js#custom-server-and-routing) in development mode on a production server, for example, [now.sh](https://zeit.co) which [doesn't allow you to write to the filesystem after your app is built](https://zeit.co/docs/deployment-types/node#file-system-specifications). + +#### Possible Ways to Fix It + +When using a custom server with a server file, for example called `server.js`, make sure you update the scripts key in `package.json` to: + +```json +{ + "scripts": { + "dev": "node server.js", + "build": "next build", + "start": "NODE_ENV=production node server.js" + } +} +``` + +and the custom server starts Next in production mode when `NODE_ENV` is `production` + +```js +const dev = process.env.NODE_ENV !== 'production' +const app = next({ dev }) +``` + +### Useful Links + +- [Custom Server documentation + examples](https://github.com/zeit/next.js#custom-server-and-routing) diff --git a/readme.md b/readme.md index 25d41160..e7ecea64 100644 --- a/readme.md +++ b/readme.md @@ -669,7 +669,19 @@ export default ({ url }) =>

-Typically you start your next server with `next start`. It's possible, however, to start a server 100% programmatically in order to customize routes, use route patterns, etc +Typically you start your next server with `next start`. It's possible, however, to start a server 100% programmatically in order to customize routes, use route patterns, etc. + +When using a custom server with a server file, for example called `server.js`, make sure you update the scripts key in `package.json` to: + +```json +{ + "scripts": { + "dev": "node server.js", + "build": "next build", + "start": "NODE_ENV=production node server.js" + } +} +``` This example makes `/a` resolve to `./pages/b`, and `/b` resolve to `./pages/a`: diff --git a/server/build/index.js b/server/build/index.js index ba311bf8..10149b32 100644 --- a/server/build/index.js +++ b/server/build/index.js @@ -10,6 +10,14 @@ import md5File from 'md5-file/promise' export default async function build (dir, conf = null) { const buildId = uuid.v4() const buildDir = join(tmpdir(), uuid.v4()) + + try { + await fs.access(buildDir, fs.constants.W_OK) + } catch (err) { + console.error(`> Failed, build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable`) + throw err + } + const compiler = await webpack(dir, { buildId, buildDir, conf }) try {