# next-react-app-scripts

> Configuration and scripts for Create Next React App.

Latest version **3.0.2** (published 2018-08-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install next-react-app-scripts
pnpm add next-react-app-scripts
yarn add next-react-app-scripts
bun add next-react-app-scripts
```

Provides the command `next-react-app-scripts`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2018-08-21 |
| First published | 2018-04-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 24 |
| Unpacked size | 40.1 KB |
| Known vulnerabilities | 0 (+11 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | dave-hillier, davethemac, fbarbare, missbruni, mkredz, pbazydlo, rameshanandakrishnan, tlacan |

## Links

- npm: https://www.npmjs.com/package/next-react-app-scripts
- Repository: https://github.com/guestlinelabs/create-next-react-app
- Homepage: https://github.com/guestlinelabs/create-next-react-app#readme
- Issues: https://github.com/guestlinelabs/create-next-react-app/issues
- npm.io page: https://npm.io/package/next-react-app-scripts

## Dependencies (24)

- [ncp](https://npm.io/package/ncp.md) ^2.0.0
- [raf](https://npm.io/package/raf.md) ^3.4.0
- [next](https://npm.io/package/next.md) 6.1.1
- [dotenv](https://npm.io/package/dotenv.md) ^5.0.1
- [eslint](https://npm.io/package/eslint.md) ^4.1.1
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [promise](https://npm.io/package/promise.md) ^8.0.1
- [webpack](https://npm.io/package/webpack.md) 3.1.0
- [fs-extra](https://npm.io/package/fs-extra.md) ^5.0.0
- [camelcase](https://npm.io/package/camelcase.md) ^5.0.0
- [cross-spawn](https://npm.io/package/cross-spawn.md) ^6.0.5
- [babel-eslint](https://npm.io/package/babel-eslint.md) ^7.2.3
- [whatwg-fetch](https://npm.io/package/whatwg-fetch.md) ^2.0.4
- [object-assign](https://npm.io/package/object-assign.md) ^4.1.1
- [@zeit/next-css](https://npm.io/package/@zeit/next-css.md) ^0.2.0
- [react-dev-utils](https://npm.io/package/react-dev-utils.md) ^5.0.0
- [eslint-plugin-react](https://npm.io/package/eslint-plugin-react.md) ^7.1.0
- [eslint-plugin-import](https://npm.io/package/eslint-plugin-import.md) ^2.6.0
- [eslint-plugin-flowtype](https://npm.io/package/eslint-plugin-flowtype.md) ^2.34.1
- [eslint-plugin-jsx-a11y](https://npm.io/package/eslint-plugin-jsx-a11y.md) ^5.1.1
- [webpack-bundle-analyzer](https://npm.io/package/webpack-bundle-analyzer.md) ^2.13.1
- [eslint-config-next-react-app](https://npm.io/package/eslint-config-next-react-app.md) ^1.0.0
- [babel-plugin-transform-define](https://npm.io/package/babel-plugin-transform-define.md) ^1.3.0
- [babel-plugin-transform-assets-import-to-string](https://npm.io/package/babel-plugin-transform-assets-import-to-string.md) ^1.2.0

## Recent versions

- 3.0.2 (latest) — 2018-08-21
- 3.0.1 — 2018-08-21
- 3.0.0 — 2018-08-21
- 2.0.4 — 2018-08-09
- 2.0.3 — 2018-08-08
- 2.0.2 — 2018-06-27
- 2.0.1 — 2018-06-27
- 2.0.0 — 2018-06-25
- 1.0.7 — 2018-05-10
- 1.0.6 — 2018-05-10
- 1.0.5 — 2018-05-10
- 1.0.4 — 2018-05-02
- 1.0.3 — 2018-04-26
- 1.0.2 — 2018-04-24
- 1.0.1 — 2018-04-14
- … 1 more at https://npm.io/package/next-react-app-scripts/versions

## README

# Create Next React App

This package includes a set of scripts that makes configuring a project easier. It is intended to be the likes of Create React App but for NextJS applications.

If you want to start a new project, see [create-next-react-app](https://github.com/guestlinelabs/create-next-react-app/packages/create-next-react-app).

If you want to migrate an existing project, please refer to the folder structure, or the [`template`](https://github.com/guestlinelabs/create-next-react-app/packages/next-react-app-scripts/template) folder. The folder is following the one of CRA to make migrations between the 2 as painless as possible.

## NextJS by default

We support all the default options that NextJS provides by using their `next/babel` configuration as a Babel preset and by using their build script.

## Table of contents

* [Folder Structure](#default-folder-structure)
* [Getting Started](#getting-started)
* [NextJS by default](#nextjs-by-default)
* [Browsers/NodeJS support](#browsers/nodejs-support)
* [CSS](#css)
* [Assets](#assets)
* [Environment variables in client side](#environment-variables-in-client-side)
* [CDN](#leverage-assets-using-cdn)
* [Port](#customize-the-port)
* [Config](#use-your-own-config)
* [Polyfills](#use-polyfills)
* [Tests](#tests)
* [Moment.js locales](#momentjs-locales)

## Folder Structure

```
|_ build // Where the generated files will be added
|_ src
| |_ client
| | |_ components
| | |_ pages
| | |_ static
| |_ server
| |_ server.js
| |_ setupTests.js
|_ .babelrc // To inherit the Babel config
|_ .env
|_ .eslintrc.js // To inherit the ESLint config so you can see it in your IDE
|_ .gitignore
|_ package.json
```

For the project to build, these files must exist with exact filenames:

* `pages` folder with `.js` files that map to your routes
* `server.js` is the NodeJS server entry point.
* `.babelrc` so when building we use our built-in configuration.

## Getting started

After creating your project using Create Next React App, you will have a set of 4 commands:

#### start

`npm start` will start your development environment.

#### build

`npm run build` will create all the necessary files for your production environments.

By running your build with `CI=true`, it will make it fail on ESLint warnings.

#### prod

`npm run prod` will run your server in a production environment. A build needs to have been generated first.

#### test

`npm test` will run your tests in watch mode. To learn more about the test runner, refer to its documentation: [jest-run](https://github.com/guestlinelabs/jest-run)

## Browsers/NodeJS support

For browser/node.js compatibility we use `babel-preset-env` with the following options by default, but you can customize them in your `package.json`:

```js
{
  "targets": {
    "node": "8.9.4",
    "browsers": ["last 2 versions", "ie 11"]
  }
}
```

## CSS

You can Next's bundled [styled-jsx](https://github.com/zeit/styled-jsx), but you can also import CSS files directly:

```js
// Any page/component/...
import React from 'react';
import './style.css';

export default function() {
  return <span className="my-class-name">Some text</span>;
}
```

For the CSS to be served to your application, you will however have to create a `_document.js` file in your `pages` folder:

```js
// src/client/pages/_document.js
import React from 'react';
import Document, { Head, Main, NextScript } from 'next/document';

export default class MyDocument extends Document {
  render() {
    return (
      <html>
        <Head>
          <link rel="stylesheet" href="/_next/static/style.css" />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </html>
    );
  }
}
```

## Assets

Import your assets directly from the `static` folder:

```js
// Any page/component/...
import React from 'react';
import logo from '../static/logo.png';

export default function() {
  return <img src={logo} alt="logo" />;
}
```

## Environment variables in client side

Your project can consume variables declared in your environment as if they were declared locally in your JS files. By default you will have `NODE_ENV` defined for you, and any other environment variables starting with `NEXT_APP_`.

```yaml
// .env
NEXT_APP_TEST=abc
```

```js
// Any page/component/...
import React from 'react';

export default function() {
  return <span>{process.env.NEXT_APP_TEST}</span>;
}
```

> Note: You must create custom environment variables beginning with `NEXT_APP_`. Any other variables except `NODE_ENV` will be ignored to avoid accidentally **exposing a private key on the machine that could have the same name**. Changing any environment variables will require you to restart the development server if it is running.

## Leverage assets using CDN

Using a CDN is just an example, it could be that you want to host your JS/CSS/Static files on a different server or just a different domain.

It will work automatically for `src/client/static` files that you import directly into your code, but you will have to make a small change to your CSS.
To make that happen, you need:

* To create the `PUBLIC_URL` variable and use it as such:

```yaml
// .env
PUBLIC_URL=https://cdn.net/some/path
```

```js
// src/client/pages/_document.js

// Change
<link rel="stylesheet" href="/_next/static/style.css" />

// To
<link rel="stylesheet" href={`${process.env.ASSET_PREFIX}/static/style.css`} />
```

```js
// Any page/component/...
import React from 'react';
import logo from '../static/logo.png';

export default function() {
  return <img src={logo} alt="logo" />;
}
```

> Note: CSS and Static files do not work yet. We're waiting on NextJS to publish their next version for that. Once released, it will start working automatically.

After those changes are made, you just need to upload the content of `build/public` and serve those files from where ever you want.

## Customize the port

Customize the port your application will be running on by declaring a PORT variable:

```bash
PORT=3001 guestline-scripts start
```

## Use your own config

You can use your own config or extend ours by creating a `src/client/next.config.js` file.
This file should follow the [Next.js documentation](https://github.com/zeit/next.js/#custom-configuration).

Remember to use your own config in your `server.js` too.

## Use polyfills

In case you want to add your own set of polyfills for the client, just create a `polyfills.js` file inside the client folder, and add the ones you want to use. create-next-react-app will automatically pick it for you and put it at the beginning of the bundle. You can use directly [core-js](https://github.com/zloirock/core-js), as Next already uses it.

Example:

```javascript
// src/client/polyfills.js

import 'core-js/fn/object/assign';
import 'core-js/fn/array/fill';
import 'core-js/fn/array/from';
```

## Tests

By default the `test` command is using [`jest-run`](https://github.com/guestlinelabs/jest-run). Refer to its documentation to see how to work with it.

## Moment.js locales

If you use a [Moment.js](https://momentjs.com/), you might notice that only the English locale is available by default. This is because the locale files are large, and you probably only need a subset of [all the locales provided by Moment.js](https://momentjs.com/#multiple-locale-support).

To add a specific Moment.js locale to your bundle, you need to import it explicitly.<br>
For example:

```js
import moment from 'moment';
import 'moment/locale/fr';
```

If import multiple locales this way, you can later switch between them by calling `moment.locale()` with the locale name:

```js
import moment from 'moment';
import 'moment/locale/fr';
import 'moment/locale/es';

// ...

moment.locale('fr');
```

This will only work for locales that have been explicitly imported before.

---
_Source: https://npm.io/package/next-react-app-scripts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
