env.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. const fs = require('fs');
  3. const path = require('path');
  4. const paths = require('./paths');
  5. // Make sure that including paths.js after env.js will read .env variables.
  6. delete require.cache[require.resolve('./paths')];
  7. const NODE_ENV = process.env.NODE_ENV;
  8. if (!NODE_ENV) {
  9. throw new Error(
  10. 'The NODE_ENV environment variable is required but was not specified.'
  11. );
  12. }
  13. console.log('env=>',process.env);
  14. process.env.PORT=80;
  15. // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
  16. var dotenvFiles = [
  17. `${paths.dotenv}.${NODE_ENV}.local`,
  18. `${paths.dotenv}.${NODE_ENV}`,
  19. // Don't include `.env.local` for `test` environment
  20. // since normally you expect tests to produce the same
  21. // results for everyone
  22. NODE_ENV !== 'test' && `${paths.dotenv}.local`,
  23. paths.dotenv,
  24. ].filter(Boolean);
  25. // Load environment variables from .env* files. Suppress warnings using silent
  26. // if this file is missing. dotenv will never modify any environment variables
  27. // that have already been set. Variable expansion is supported in .env files.
  28. // https://github.com/motdotla/dotenv
  29. // https://github.com/motdotla/dotenv-expand
  30. dotenvFiles.forEach(dotenvFile => {
  31. if (fs.existsSync(dotenvFile)) {
  32. require('dotenv-expand')(
  33. require('dotenv').config({
  34. path: dotenvFile,
  35. })
  36. );
  37. }
  38. });
  39. // We support resolving modules according to `NODE_PATH`.
  40. // This lets you use absolute paths in imports inside large monorepos:
  41. // https://github.com/facebook/create-react-app/issues/253.
  42. // It works similar to `NODE_PATH` in Node itself:
  43. // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
  44. // Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
  45. // Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
  46. // https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
  47. // We also resolve them to make sure all tools using them work consistently.
  48. const appDirectory = fs.realpathSync(process.cwd());
  49. process.env.NODE_PATH = (process.env.NODE_PATH || '')
  50. .split(path.delimiter)
  51. .filter(folder => folder && !path.isAbsolute(folder))
  52. .map(folder => path.resolve(appDirectory, folder))
  53. .join(path.delimiter);
  54. // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
  55. // injected into the application via DefinePlugin in Webpack configuration.
  56. const REACT_APP = /^REACT_APP_/i;
  57. function getClientEnvironment(publicUrl) {
  58. const raw = Object.keys(process.env)
  59. .filter(key => REACT_APP.test(key))
  60. .reduce(
  61. (env, key) => {
  62. env[key] = process.env[key];
  63. return env;
  64. },
  65. {
  66. // Useful for determining whether we’re running in production mode.
  67. // Most importantly, it switches React into the correct mode.
  68. NODE_ENV: process.env.NODE_ENV || 'development',
  69. // Useful for resolving the correct path to static assets in `public`.
  70. // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
  71. // This should only be used as an escape hatch. Normally you would put
  72. // images into the `src` and `import` them in code to get their paths.
  73. PUBLIC_URL: publicUrl,
  74. }
  75. );
  76. // Stringify all values so we can feed into Webpack DefinePlugin
  77. const stringified = {
  78. 'process.env': Object.keys(raw).reduce((env, key) => {
  79. env[key] = JSON.stringify(raw[key]);
  80. return env;
  81. }, {}),
  82. };
  83. return { raw, stringified };
  84. }
  85. module.exports = getClientEnvironment;