LoginSignup
5

More than 5 years have passed since last update.

Create React App で dotenv

Posted at

1. ライブラリのインストール

yarn add dotenv

2. .env ファイルの作成

REACT_APP_ という接頭辞を付けないと認識しない。

.env
NODE_PATH=./src
REACT_APP_API_URL=http://api.example.com

3. 読み込み

import 文よりも先に書くと、ルールによっては ESLint に怒られるので、無効にしておく

src/index.js
/* eslint-disable */
require('dotenv').config()

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import './index.css'
import registerServiceWorker from './registerServiceWorker'

/* eslint-enable */

ReactDOM.render(<App />, document.getElementById('root'))
registerServiceWorker()

参考

REACT_APP という接頭辞を付けないと認識しないことに、ちょっとはまった。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5