Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
131 commits
Select commit Hold shift + click to select a range
7be5797
Use the preact-compat dist version. (#760)
arunoda Jan 13, 2017
23cddda
Only show deprecation when using next/css (#762)
timneutkens Jan 13, 2017
5ef34c6
update styletron example to support multiple stylesheets (#763)
skellyb Jan 13, 2017
8a06c7b
example: fix style
nkzawa Jan 14, 2017
60918ee
Fix typo for --help messages in /bin (#770)
davidrossjones Jan 15, 2017
cebed46
chore(package): update mime-types to version 2.1.14 (#772)
greenkeeperio-bot Jan 15, 2017
fae2305
Add correct sync version of error handling with existSync. (#769)
arunoda Jan 15, 2017
6fd5cfd
chore(package): update source-map-support to version 0.4.9 (#777)
greenkeeperio-bot Jan 15, 2017
8318f5b
Add cross browser stacktrace (#776)
timneutkens Jan 15, 2017
b6e72f5
Use UTC time to avoid time differences (#782)
timneutkens Jan 16, 2017
8767165
chore(package): update styled-jsx to version 0.4.2 (#784)
greenkeeperio-bot Jan 16, 2017
6f117ec
Set default NODE_ENV value. (#768)
arunoda Jan 16, 2017
782807b
chore(package): update friendly-errors-webpack-plugin to version 1.1.…
greenkeeperio-bot Jan 16, 2017
0d6ceec
Add styled-jsx-postcss example (#781)
giuseppeg Jan 16, 2017
60ec4de
Add NODE_PATH support for resolveLoaders as well. (#778)
arunoda Jan 16, 2017
da83958
bump webpack
nkzawa Jan 16, 2017
789edb8
Fix typo in README (#787)
possibilities Jan 16, 2017
8fa630d
Add dynamic routing keyword (#788)
sedubois Jan 16, 2017
807d4be
chore(package): update styled-jsx to version 0.4.3 (#792)
greenkeeperio-bot Jan 16, 2017
ffeeb68
Remove useless config override (#790)
sedubois Jan 16, 2017
7cc554c
Fixed hapi example (#795)
impronunciable Jan 16, 2017
70b92e6
Added deprecation message when adding an extra anchor in behalf of th…
impronunciable Jan 16, 2017
a66ff65
Fix link (#789)
sedubois Jan 16, 2017
e28f80c
Updated <Link> in repo, always wrapping an anchor. (#798)
impronunciable Jan 16, 2017
606d89c
Update README.md (#802)
0bit Jan 17, 2017
0627378
chore(package): update glamor to version 2.20.22 (#804)
greenkeeperio-bot Jan 17, 2017
2c2d961
Added Koa example (#800)
impronunciable Jan 17, 2017
c1f2b92
chore(package): update source-map-support to version 0.4.10 (#809)
greenkeeperio-bot Jan 17, 2017
2efabde
Fix koa-404 issue by adding res.statusCode to 200 (#815)
arunoda Jan 18, 2017
0119149
chore(package): update styled-jsx to version 0.4.4 (#817)
greenkeeperio-bot Jan 18, 2017
43149bd
fix not to overwrite the ignored option on Windows (#824)
nkzawa Jan 19, 2017
1e77568
added example with flow (#814)
jagreehal Jan 19, 2017
5be9c04
Custom server example with Hapi in README (#825)
okuryu Jan 19, 2017
c215311
Move nextdata into nextScript and make Main return a single el (#831)
alexnewmannn Jan 20, 2017
cfb1c0f
chore(package): update babel-core to version 6.22.1 (#842)
greenkeeperio-bot Jan 20, 2017
21a22b9
chore(package): update babel-runtime to version 6.22.0 (#840)
greenkeeperio-bot Jan 20, 2017
901ab5f
chore(package): update babel-preset-react to version 6.22.0 (#839)
greenkeeperio-bot Jan 20, 2017
279fc99
chore(package): update babel-preset-es2015 to version 6.22.0 (#838)
greenkeeperio-bot Jan 20, 2017
2aed328
chore(package): update babel-plugin-transform-runtime to version 6.22…
greenkeeperio-bot Jan 20, 2017
1180555
chore(package): update babel-plugin-transform-object-rest-spread to v…
greenkeeperio-bot Jan 20, 2017
865f806
chore(package): update babel-plugin-transform-class-properties to ver…
greenkeeperio-bot Jan 20, 2017
a05a5d2
chore(package): update babel-plugin-transform-async-to-generator to v…
greenkeeperio-bot Jan 20, 2017
318f110
chore(package): update babel-generator to version 6.22.0 (#833)
greenkeeperio-bot Jan 20, 2017
399e510
Make sure lastAppProps always have some value. (#829)
arunoda Jan 20, 2017
f085a6d
chore(package): update styled-jsx to version 0.4.5 (#847)
greenkeeperio-bot Jan 20, 2017
3765b14
bump `styled-jsx`
rauchg Jan 21, 2017
cec8f9d
Release 2.0.0-beta.18
rauchg Jan 21, 2017
98e2951
Use ErrorDebug component on error of react-hot-loader (#852)
nkzawa Jan 21, 2017
e67d62d
Update styled-component docs (#841)
kristojorg Jan 21, 2017
13d6c04
chore(package): update husky to version 0.13.0 (#853)
greenkeeperio-bot Jan 22, 2017
3813f73
Fix error fonts (#826)
Jan 22, 2017
85cd38f
tweak font sizes again 🙈
rauchg Jan 22, 2017
4b25748
Add Apollo example (#780)
adamsoffer Jan 22, 2017
8b94534
Wrap render method created using class properties (2) (#856)
nkzawa Jan 22, 2017
2390c87
chore(package): update husky to version 0.13.1 (#862)
greenkeeperio-bot Jan 23, 2017
8d8d5eb
Example using Fela (#863)
Jan 23, 2017
cbb2d1c
simpler flow task (#857)
jagreehal Jan 24, 2017
3ad3a6c
Added Google AMP example (#793)
impronunciable Jan 24, 2017
a418b35
Added Fela Example to the CSS-in-JS <details> in README (#878)
Jan 25, 2017
f89eaf3
fix(package): update styled-jsx to version 0.5.1 (#879)
greenkeeper[bot] Jan 25, 2017
b4e2b7b
Handles the initial popstate event of older version of Safari. (#870)
arunoda Jan 25, 2017
a76ec83
Update Apollo Example (#888)
adamsoffer Jan 26, 2017
51cbebb
Don't process.exit(null) when e.g build is `SIGKILL`ed (#887)
jamo Jan 26, 2017
293029a
update yarn.lock
nkzawa Jan 26, 2017
386c960
Release 2.0.0-beta.19
nkzawa Jan 26, 2017
0b3db91
Fix handling finished response (#889)
nkzawa Jan 26, 2017
0b131ca
Add `next build` and `next start` to the first How to use sample (#894)
jamo Jan 26, 2017
8922e7b
Update README.md (#895)
Jan 26, 2017
8310f81
Fix Apollo Example (#900)
adamsoffer Jan 27, 2017
e0f49f8
fix(package): update styled-jsx to version 0.5.2 (#902)
greenkeeper[bot] Jan 27, 2017
f944b50
Release 2.0.0-beta.20
rauchg Jan 27, 2017
4481038
chore(package): update gulp-notify to version 3.0.0 (#905)
greenkeeper[bot] Jan 27, 2017
52163f5
fix(package): update source-map-support to version 0.4.11 (#901)
greenkeeper[bot] Jan 27, 2017
db50fc7
chore(package): update wd to version 1.1.3 (#906)
timneutkens Jan 27, 2017
ee717af
examples/with-redux: remove global store (#908)
stephenmathieson Jan 28, 2017
bf467e6
Do not patch prototypes with render exposed only as a getter. (#898)
arunoda Jan 28, 2017
a3bec76
with-apollo: Don't store Redux store and Apollo client in global name…
sedubois Jan 28, 2017
0820204
Remove yarn.lock from examples (#912)
timneutkens Jan 29, 2017
ac2a963
README: fix "Routing with lazy component loading" blob (#915)
stephenmathieson Jan 29, 2017
b4814e1
fix typo (#916)
kandros Jan 29, 2017
72ae013
fix(package): update webpack-hot-middleware to version 2.16.0 (#922)
greenkeeper[bot] Jan 30, 2017
0d2af80
Use service-worker to fetch only JSON pages. (#924)
arunoda Jan 30, 2017
08eadb4
fix(package): update webpack-hot-middleware to version 2.16.1 (#935)
greenkeeper[bot] Jan 30, 2017
bd83ed5
[WIP] Remember scroll position on error (#911)
timneutkens Jan 31, 2017
5188eed
fix(package): update webpack to version 2.2.1 (#938)
greenkeeper[bot] Jan 31, 2017
f3e541f
Add support for Webpack 2's tree-shaking (#926)
arunoda Jan 31, 2017
5790986
Set dev variable consistent with examples (#939)
timneutkens Jan 31, 2017
ec2c8c6
Fix .json import issue (#944)
arunoda Feb 1, 2017
2f7d743
add pretty message if port already in use(#927) (#932)
dex157 Feb 1, 2017
a97ca92
fix(package): update webpack-dev-middleware to version 1.10.0 (#948)
greenkeeper[bot] Feb 1, 2017
f971734
Resolve preset es2015 from next directory (#949)
timneutkens Feb 2, 2017
24edfbd
Resolve styled-jsx/style when transpiling ES2015 modules. (#953)
arunoda Feb 2, 2017
59281ad
Allow parsed url to be passed down (#950)
timneutkens Feb 2, 2017
0a2466a
Added React-MD example (#940)
frol Feb 2, 2017
cf4fb78
fix(package): update styled-jsx to version 0.5.3 (#958)
greenkeeper[bot] Feb 2, 2017
61bdae5
Fix typo in react-md example readme (#959)
timneutkens Feb 2, 2017
3e34430
Release 2.0.0-beta.21
rauchg Feb 2, 2017
fca0a39
examples/with-react-md: Fixed "Cannot read property 'focus' of undefi…
frol Feb 2, 2017
efe0c7b
Resolve all modules through module resolver (#963)
timneutkens Feb 2, 2017
ec83670
Check if BUILD_ID is available before starting (#960)
timneutkens Feb 2, 2017
adf38f3
Release 2.0.0-beta.22
rauchg Feb 2, 2017
729088c
Merge branch 'master' of github.com:zeit/next.js
rauchg Feb 2, 2017
57e9a5e
Find custom babel config location properly. (#969)
arunoda Feb 3, 2017
3143e0d
Add styled-jsx to babel plugins (#970)
timneutkens Feb 3, 2017
ddd93e9
Improve babel settings (#976)
nkzawa Feb 3, 2017
6431f5f
Allow any element to be rendered under Link (#921)
timneutkens Feb 3, 2017
2e81af8
Release 2.0.0-beta.23
rauchg Feb 3, 2017
a8731d0
Added universal configuration example (#991)
timneutkens Feb 4, 2017
5b2854d
Properly handle hash URL changes. (#996)
arunoda Feb 5, 2017
267b74b
Add next.js flowtype definition to with-flow example (#973)
mizchi Feb 5, 2017
592c666
[example] Progressive rendered application (#998)
sergiodxa Feb 5, 2017
2af8a4c
fix(package): update styled-jsx to version 0.5.4 (#1002)
greenkeeper[bot] Feb 5, 2017
f6510c0
Pass parsed request-URL into the run-method (#1000)
herschel666 Feb 5, 2017
1d45634
fix(package): update babel-plugin-module-resolver to version 2.5.0 (#…
greenkeeper[bot] Feb 5, 2017
6832fea
chore(package): update coveralls to version 2.11.16 (#1007)
greenkeeper[bot] Feb 5, 2017
8c1808d
Add contributing.md (#1009)
timneutkens Feb 6, 2017
8dfbbd3
Release 2.0.0-beta.24
rauchg Feb 6, 2017
01cc898
Update Apollo example (#1021)
adamsoffer Feb 7, 2017
aa971d6
Disallow directories in /static (#1028)
timneutkens Feb 8, 2017
4a73ccb
add react and react dom as peer (#1024)
sakulstra Feb 8, 2017
55f4856
Add babel-preset-latest support. (#1027)
arunoda Feb 8, 2017
29e0c3e
add react and react-dom to devDependencies
nkzawa Feb 8, 2017
a9428b8
fix .baberc (#1041)
nkzawa Feb 8, 2017
9348762
add next/error (#1040)
nkzawa Feb 8, 2017
bdcb0f7
Add support to transpile import/export syntax (#1044)
arunoda Feb 9, 2017
53c245b
Throw when parsedUrl is not provided (#1015)
timneutkens Feb 9, 2017
b00f0c2
TypeScript example proposal (#845)
Feb 9, 2017
d6439d1
Add table of contents to README (#735)
lorensr Feb 9, 2017
e46cedd
Update table of contents (#1051)
timneutkens Feb 9, 2017
288aca5
Merge branch 'master' of git://github.com/zeit/next.js into test-get-…
timweprovide Feb 9, 2017
20c2559
Update yarn.lock
timweprovide Feb 9, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"presets": ["es2015", "react"],
"presets": ["latest", "react"],
"plugins": [
"transform-async-to-generator",
"transform-object-rest-spread",
"transform-class-properties",
"transform-runtime"
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ npm-debug.log
# coverage
.nyc_output
coverage

# editors
.idea/*
*.iml
*.sublime-*
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Contributing to Next.js

Our Commitment to Open Source can be found [here](https://zeit.co/blog/oss)

1. [Fork](https://help.github.com/articles/fork-a-repo/) this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device.
2. Install the dependencies: `npm install`
3. Run `npm link` to link the local repo to NPM
4. Run npm run build in a new terminal to build and watch for code changes
5. Then npm link this repo inside any example app with `npm link next`
6. Then you can run your example app with the local version of Next.js (You may need to re-run the example app as you change server side code in the Next.js repository)
82 changes: 69 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,46 @@

Next.js is a minimalistic framework for server-rendered React applications.

**NOTE! the README on the `master` branch might not match that of the [latest stable release](https://github.com/zeit/next.js/releases/latest)! **
_**NOTE! the README on the `master` branch might not match that of the [latest stable release](https://github.com/zeit/next.js/releases/latest)!**_

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
<!-- https://github.com/thlorenz/doctoc -->

- [How to use](#how-to-use)
- [Setup](#setup)
- [Automatic code splitting](#automatic-code-splitting)
- [CSS](#css)
- [Built-in CSS support](#built-in-css-support)
- [CSS-in-JS](#css-in-js)
- [Static file serving (e.g.: images)](#static-file-serving-eg-images)
- [Populating `<head>`](#populating-head)
- [Fetching data and component lifecycle](#fetching-data-and-component-lifecycle)
- [Routing](#routing)
- [With `<Link>`](#with-link)
- [Imperatively](#imperatively)
- [Router Events](#router-events)
- [Prefetching Pages](#prefetching-pages)
- [With `<Link>`](#with-link-1)
- [Imperatively](#imperatively-1)
- [Custom server and routing](#custom-server-and-routing)
- [Custom `<Document>`](#custom-document)
- [Custom error handling](#custom-error-handling)
- [Custom configuration](#custom-configuration)
- [Customizing webpack config](#customizing-webpack-config)
- [Customizing babel config](#customizing-babel-config)
- [Production deployment](#production-deployment)
- [FAQ](#faq)
- [Roadmap](#roadmap)
- [Contributing](#contributing)
- [Authors](#authors)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## How to use

### Setup

Install it:

```bash
Expand All @@ -21,7 +57,9 @@ and add a script to your package.json like this:
```json
{
"scripts": {
"dev": "next"
"dev": "next",
"build": "next build",
"start": "next start"
}
}
```
Expand Down Expand Up @@ -94,8 +132,10 @@ export default () => (
#### CSS-in-JS

<p><details>
<summary><b>Examples</b></summary>
<ul><li><a href="./examples/with-styled-components">Styled components</a></li><li><a href="./examples/with-styletron">Styletron</a></li><li><a href="./examples/with-glamor">Glamor</a></li><li><a href="./examples/with-cxs">Cxs</a></li><li><a href="./examples/with-aphrodite">Aphrodite</a></li></ul>
<summary>
<b>Examples</b>
</summary>
<ul><li><a href="./examples/with-styled-components">Styled components</a></li><li><a href="./examples/with-styletron">Styletron</a></li><li><a href="./examples/with-glamor">Glamor</a></li><li><a href="./examples/with-cxs">Cxs</a></li><li><a href="./examples/with-aphrodite">Aphrodite</a></li><li><a href="./examples/with-fela">Fela</a></li></ul>
</details></p>

It's possible to use any existing CSS-in-JS solution. The simplest one is inline styles:
Expand Down Expand Up @@ -271,7 +311,7 @@ Here's a list of supported events:

> Here `url` is the URL shown in the browser. If you call `Router.push(url, as)` (or similar), then the value of `url` will be `as`.

Here's how to property listen to the router event `routeChangeStart`:
Here's how to properly listen to the router event `routeChangeStart`:

```js
Router.onRouteChangeStart = (url) => {
Expand Down Expand Up @@ -327,7 +367,7 @@ export default () => (
When this higher-level `<Link>` component is first used, the `ServiceWorker` gets installed. To turn off prefetching on a per-`<Link>` basis, you can use the `prefetch` attribute:

```jsx
<Link href='/contact' prefetch={false}>Home</Link>
<Link href='/contact' prefetch={false}><a>Home</a></Link>
```

#### Imperatively
Expand Down Expand Up @@ -356,6 +396,8 @@ export default ({ url }) => (
<ul>
<li><a href="./examples/custom-server">Basic custom server</a></li>
<li><a href="./examples/custom-server-express">Express integration</a></li>
<li><a href="./examples/custom-server-hapi">Hapi integration</a></li>
<li><a href="./examples/custom-server-koa">Koa integration</a></li>
<li><a href="./examples/parameterized-routing">Parameterized routing</a></li>
<li><a href="./examples/ssr-caching">SSR caching</a></li>
</ul>
Expand All @@ -370,19 +412,21 @@ const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')

const app = next({ dev: true })
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
createServer((req, res) => {
const { pathname, query } = parse(req.url, true)
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl

if (pathname === '/a') {
app.render(req, res, '/b', query)
} else if (pathname === '/b') {
app.render(req, res, '/a', query)
} else {
handle(req, res)
handle(req, res, parsedUrl)
}
})
.listen(3000, (err) => {
Expand All @@ -406,12 +450,14 @@ Supported options:
<p><details>
<summary><b>Examples</b></summary>
<ul><li><a href="./examples/with-styled-components">Styled components custom document</a></li></ul>
<ul><li><a href="./examples/with-amp">Google AMP</a></li></ul>
</details></p>

Pages in `Next.js` skip the definition of the surrounding document's markup. For example, you never include `<html>`, `<body>`, etc. But we still make it possible to override that:
Pages in `Next.js` skip the definition of the surrounding document's markup. For example, you never include `<html>`, `<body>`, etc. To override that default behavior, you must create a file at `./pages/_document.js`, where you can extend the `Document` class:

```jsx
import Document, { Head, Main, NextScript } from `next/document`
// ./pages/_document.js
import Document, { Head, Main, NextScript } from 'next/document'

export default class MyDocument extends Document {
static async getInitialProps (ctx) {
Expand Down Expand Up @@ -580,8 +626,7 @@ No in that it enforces a _structure_ so that we can do more advanced things like
- Automatic code splitting

In addition, Next.js provides two built-in features that are critical for every single website:
- Routing with lazy component loading: `
>` (by importing `next/link`)
- Routing with lazy component loading: `<Link>` (by importing `next/link`)
- A way for components to alter `<head>`: `<Head>` (by importing `next/head`)

If you want to create re-usable React components that you can embed in your Next.js app or other React applications, using `create-react-app` is a great idea. You can later `import` it and keep your codebase clean!
Expand Down Expand Up @@ -635,6 +680,13 @@ On the client side, we have a parameter call `as` on `<Link>` that _decorates_ t
It’s up to you. `getInitialProps` is an `async` function (or a regular function that returns a `Promise`). It can retrieve data from anywhere.
</details>

<details>
<summary>Can I use it with GraphQL?</summary>

Yes! Here's an example with [Apollo](./examples/with-apollo).

</details>

<details>
<summary>Can I use it with Redux?</summary>

Expand All @@ -658,6 +710,10 @@ As we were researching options for server-rendering React that didn’t involve

Our Roadmap towards 2.0.0 [is public](https://github.com/zeit/next.js/wiki/Roadmap#nextjs-200).

## Contributing

Please see our [CONTRIBUTING.md](./CONTRIBUTING.md)

## Authors

- Naoyuki Kanezawa ([@nkzawa](https://twitter.com/nkzawa)) – ▲ZEIT
Expand Down
14 changes: 13 additions & 1 deletion bin/next
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,19 @@ const bin = join(__dirname, 'next-' + cmd)

const startProcess = () => {
const proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] })
proc.on('close', (code) => process.exit(code))
proc.on('close', (code, signal) => {
if (code !== null) {
process.exit(code)
}
if (signal) {
if (signal === 'SIGKILL') {
process.exit(137)
}
console.log(`got signal ${signal}, exitting`)
process.exit(1)
}
process.exit(0)
})
proc.on('error', (err) => {
console.error(err)
process.exit(1)
Expand Down
30 changes: 14 additions & 16 deletions bin/next-build
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env node

import { resolve, join } from 'path'
import { exists } from 'mz/fs'
import { existsSync } from 'fs'
import parseArgs from 'minimist'
import build from '../server/build'
import { printAndExit } from '../lib/utils'

process.env.NODE_ENV = process.env.NODE_ENV || 'production'

const argv = parseArgs(process.argv.slice(2), {
alias: {
Expand All @@ -29,21 +31,17 @@ if (argv.help) {
const dir = resolve(argv._[0] || '.')

// Check if pages dir exists and warn if not
exists(dir)
.then(async () => {
if (!(await exists(join(dir, 'pages')))) {
if (await exists(join(dir, '..', 'pages'))) {
console.error('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
} else {
console.error('> Couldn\'t find a `pages` directory. Please create one under the project root')
}
process.exit(1)
if (!existsSync(dir)) {
printAndExit(`> No such directory exists as the project root: ${dir}`)
}

if (!existsSync(join(dir, 'pages'))) {
if (existsSync(join(dir, '..', 'pages'))) {
printAndExit('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
}
})
.catch((err) => {
console.error(err)
process.exit(1)
})

printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root')
}

build(dir)
.catch((err) => {
Expand Down
45 changes: 27 additions & 18 deletions bin/next-dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
import 'source-map-support/register'
import { resolve, join } from 'path'
import parseArgs from 'minimist'
import { exists } from 'mz/fs'
import { existsSync, readFileSync } from 'fs'
import Server from '../server'
import { printAndExit } from '../lib/utils'
import pkgUp from 'pkg-up'

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const argv = parseArgs(process.argv.slice(2), {
alias: {
Expand All @@ -30,29 +34,25 @@ if (argv.help) {

Options
--port, -p A port number on which to start the application
--help, -p Displays this message
--help, -h Displays this message
`)
process.exit(0)
}

const dir = resolve(argv._[0] || '.')

// Check if pages dir exists and warn if not
exists(dir)
.then(async () => {
if (!(await exists(join(dir, 'pages')))) {
if (await exists(join(dir, '..', 'pages'))) {
console.error('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
} else {
console.error('> Couldn\'t find a `pages` directory. Please create one under the project root')
}
process.exit(1)
if (!existsSync(dir)) {
printAndExit(`> No such directory exists as the project root: ${dir}`)
}

if (!existsSync(join(dir, 'pages'))) {
if (existsSync(join(dir, '..', 'pages'))) {
printAndExit('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
}
})
.catch((err) => {
console.error(err)
process.exit(1)
})

printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root')
}

const srv = new Server({ dir, dev: true })
srv.start(argv.port)
Expand All @@ -62,6 +62,15 @@ srv.start(argv.port)
}
})
.catch((err) => {
console.error(err)
process.exit(1)
if (err.code === 'EADDRINUSE') {
let errorMessage = `Port ${argv.port} is already in use.`
const pkgAppPath = pkgUp.sync('.')
const appPackage = JSON.parse(readFileSync(pkgAppPath, 'utf8'))
const nextScript = Object.entries(appPackage.scripts).find(scriptLine => scriptLine[1] === 'next')
if (nextScript) errorMessage += `\nUse \`npm run ${nextScript[0]} -- -p <some other port>\`.`
console.error(errorMessage)
} else {
console.error(err)
}
process.nextTick(() => process.exit(1))
})
2 changes: 1 addition & 1 deletion bin/next-init
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (argv.help) {
If no directory is provided the current directory will be used.

Options
--help, -p Displays this message
--help, -h Displays this message
`)

process.exit(0)
Expand Down
8 changes: 5 additions & 3 deletions bin/next-start
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import parseArgs from 'minimist'
import Server from '../server'
import { existsSync } from 'fs'

process.env.NODE_ENV = process.env.NODE_ENV || 'production'

const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
Expand All @@ -31,7 +33,7 @@ if (argv.help) {

Options
--port, -p A port number on which to start the application
--help, -p Displays this message
--help, -h Displays this message
`)
process.exit(0)
}
Expand All @@ -40,8 +42,8 @@ const dir = resolve(argv._[0] || '.')

const srv = new Server({ dir })

if (!existsSync(resolve(dir, '.next'))) {
console.error(`> Could not find the '.next' directory! Try building your app with 'next build' before starting the server.`)
if (!existsSync(resolve(dir, '.next', 'BUILD_ID'))) {
console.error(`> Could not find a valid build in the '.next' directory! Try building your app with 'next build' before starting the server.`)
process.exit(1)
}

Expand Down
Loading