question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to access custom variables ( defined in .env ) in craco.config.js ?

See original GitHub issue

I want to change the @antd-prefix in CracoAntDesignPlugin, like below:

/* craco.config.js */

module.exports = function () {
  console.log('env....', process.env.REACT_APP_ANTD_PREFIX);
  return {
    eslint: {
      enable: true,
      mode: 'file',
    },
    plugins: [
      {
        plugin: CracoAntDesignPlugin,
        options: {
          customizeTheme: {
            '@ant-prefix': process.env.REACT_APP_ANTD_PREFIX,
          },
          customizeThemeLessPath: path.join(
            __dirname,
            'src/styles/antd.theme.less'
          ),
        },
      },

REACT_APP_ANTD_PREFIX is already defined in .env, but I got undefined here. Is there any way to get the right result ?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

16reactions
namnmcommented, Nov 16, 2020

Try: Install dotenv then add require('dotenv/config') at the top of your craco.config.js

2reactions
pinggicommented, Feb 24, 2022

If you use CRA, you can just import the react script that loads the env files.

After this line in your craco.config.js file:

require('react-scripts/config/env');

you will able to access the env vars defined in your env files.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to access custom variables ( defined in .env ) in craco ...
I want to change the @antd-prefix in CracoAntDesignPlugin, like below: /* craco.config.js */ module.exports = function () { console.log('env ...
Read more >
My Awesome Custom React Environment Variables Setup
Step 1 ~ Install install craco. ... Step 2 ~ Add a craco.config.js file to our folder structure. ... Below is our craco.config.js...
Read more >
Adding Custom Environment Variables | Create React App
Your project can consume variables declared in your environment as if they were declared locally in your JS files.
Read more >
npm:craco-plugin-env - Skypack.dev
First, follow the craco Installation Instructions to install the craco package, create a craco.config.js file, and modify the scripts in ...
Read more >
How to use Environment files (.env) in React App - LinkedIn
2- Make a file called .env.envName in your project root, sush as .env.staging, .env.production, ... to differentiate between variables in each ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found