Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

GENERATE_SOURCEMAP=false does nothing in a Create React App build on Windows

ErrorGENERATE_SOURCEMAP=false in a package.json script has no effect on a Windows machine

Short answer

GENERATE_SOURCEMAP is an environment variable that react-scripts reads for production builds only. The Linux form VAR=value command does not work in Windows cmd or PowerShell. Put the variable in a .env file, or set it with the syntax of your shell, or through cross-env, then run npm run build again.

  • Applies to react-scripts 5, Node.js 22, Windows 11 cmd and PowerShell
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Build, then count the source map files. If any .map file exists, the variable did not reach react-scripts.

    npm run build
    dir /s /b build\static\*.map
  2. 2Match what you see, then jump to its fix

    1. 1The script is written as GENERATE_SOURCEMAP=false react-scripts buildThe Linux inline syntax is used in cmd or PowerShellFix
    2. 2The value works in one terminal and not in another, or in CIThe variable has to live in a .env fileFix
    3. 3You expect fewer source maps while running npm startThe variable is only used for production buildsFix
    4. 4The same package.json is used on Windows and Linux machinesYou need one npm script that works on every operating systemFix

What does the error look like?

  • GENERATE_SOURCEMAP=false in a package.json script has no effect on a Windows machine
  • Source map (.map) files are still created in build/static after setting GENERATE_SOURCEMAP=false

Cause 1 of 4

The Linux inline syntax is used in cmd or PowerShell

1Check

The VAR=value command form is Bash syntax. Windows cmd and PowerShell use their own forms.

findstr GENERATE_SOURCEMAP package.json

2Fix

Use the syntax of your shell. In cmd keep the quotes around the assignment, because they avoid a trailing space in the value.

:: cmd.exe
set "GENERATE_SOURCEMAP=false" && npm run build

# PowerShell
($env:GENERATE_SOURCEMAP = "false") -and (npm run build)

3Verify

Build again and check that no .map files are listed.

dir /s /b build\static\*.map

Confirmed by create-react-app.dev

Did this fix it?

Cause 2 of 4

The variable has to live in a .env file

1Check

The docs say you can set these variables in your shell or in .env. A .env file in the project root is read the same way on every operating system.

type .env

2Fix

Create a .env file in the project root (next to package.json) with the line below. Restart any running dev server after editing it.

GENERATE_SOURCEMAP=false

3Verify

Run the production build and check build/static for .map files.

npm run build

Confirmed by create-react-app.dev

Did this fix it?

Cause 3 of 4

The variable is only used for production builds

1Check

In the Create React App settings table GENERATE_SOURCEMAP is ignored in development and used in production.

npm run build

2Fix

Set it for npm run build. The development server is not affected by it, so it does not need the variable.

# .env.production
GENERATE_SOURCEMAP=false

3Verify

A production build should no longer write .map files.

npm run build

Confirmed by create-react-app.dev

Did this fix it?

Cause 4 of 4

You need one npm script that works on every operating system

1Check

The cross-env README explains that most Windows command prompts do not accept the VAR=value form, and that Windows uses a different variable syntax.

npm ls cross-env

2Fix

Install cross-env and put it in front of the command in the script. The project is in maintenance mode and its repository is archived, so a .env file is the simpler long-term choice.

npm install --save-dev cross-env

"scripts": {
  "build": "cross-env GENERATE_SOURCEMAP=false react-scripts build"
}

3Verify

Run npm run build on both systems.

npm run build

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Prefer a committed .env.production for settings the whole team needs.
  • Remember the variable affects builds only; do not expect it to change npm start.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.