LoginSignup
50
48

More than 5 years have passed since last update.

Webpackのモジュール内でprocess.env.NODE_ENVを読み込む

Last updated at Posted at 2015-09-18

概要

Webpackでビルドするモジュール内でprocess.env.NODE_ENVを読み込む。

方法

Webpackのpluginsで以下のように書く。process.env.NODE_ENVの内容が読み込まれるようにJSON.stringifyする。

webpack.js
    plugins: [
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
      })
    ]

これでWebpackでビルドするときにprocess.env.NODE_ENVが読み込めるので、実際のアプリケーションで以下のように書ける。

config.coffee

'use strict'

config = ''

switch process.env.NODE_ENV
  when 'development'
    config = {
      apiurl: 'http://aaa.com'
    }
  when 'staging'
    config = {
      apiurl: 'http://bbb.com'
    }
  when 'production'
    config = {
      apiurl: 'http://ccc.com'
    }
  else
    config = {
      apiurl: 'http://aaa.com'
    }

module.exports = config;
50
48
0

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
50
48