96

My react folder structure is as below

enter image description here

I've not used the create-react-app version. I tried using GENERATE_SOURCEMAP=false. But It didn't work.

Where can I find the .map files. How can I delete those files?

I cannot find a build folder. I've tried using the below script But It cannot work in removing source maps

 "scripts": {

    "start": "react-scripts start",
   "build": "GENERATE_SOURCEMAP=false && npm run build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
Bharath Pabba
  • 1,725
  • 5
  • 16
  • 24

10 Answers10

158

just remove &&

"scripts": {    
    "start": "react-scripts start",
    "build": "GENERATE_SOURCEMAP=false react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
sandrooco
  • 8,016
  • 9
  • 48
  • 86
Kyaw Kyaw Soe
  • 3,258
  • 1
  • 14
  • 25
71

You have to create a .env file in your root directory (same folder as package.json) and set GENERATE_SOURCEMAP=false on a single line.

for additional configurations, you may refer to the documentation here: https://facebook.github.io/create-react-app/docs/advanced-configuration

jsnewbie
  • 1,721
  • 13
  • 9
36

What I have tested and which is working is to add this code in your .env.production file or .env file

GENERATE_SOURCEMAP=false
Nehemie KOFFI
  • 1,159
  • 12
  • 11
36

Solution 1

Edit your package.json like below:

  • Windows:
    "scripts": {
        "start": "react-scripts start",
        "build": "set \"GENERATE_SOURCEMAP=false\" &&  react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
      },
  • Linux:
    "scripts": {
        "start": "react-scripts start",
        "build": "GENERATE_SOURCEMAP=false react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
      },

Solution 2 (Recommended)

This solution is not operating system dependent and works on both Linux and Windows. Just create a file called .env in the root path of your project and add the following line to it:

GENERATE_SOURCEMAP=false
Sky
  • 4,244
  • 7
  • 54
  • 83
14

For windows cmd and create-react-app + react-scripts,

You should use set and close with \" YOUR_TMP_ENV_VAR \"

See example:

"deploy:prod:hosting": "set \"GENERATE_SOURCEMAP=false\" && npm run build

this answer helped me: How to set environment variable in React JS..?

Raz
  • 434
  • 7
  • 6
  • Works for me. But I don't understand why when I use `&& set PORT=3001` it's also working but without quotes. Do you know why ? Is that because of the `_` (underscore) inside `GENERATE_SOURCEMAP`? – Julha Jun 20 '19 at 09:57
  • 1
    This is the CORRECT answer. Tested on Win10. Thanks! – hsluoyz Jan 20 '20 at 01:42
8

This works for me. Hope it helps anyone.

// package.json

"build": "react-scripts build",
"postbuild": "rimraf build/**/*.map"

This way, it will auto delete map files during build generation.

Krupal Patel
  • 512
  • 8
  • 12
  • 6
    This shall work, but not reasonable to follow. Better not to create *.map than to create and remove it. – Ponleu May 09 '19 at 04:59
  • @Ponleu That's right. But I was not aware that how we can prevent that unnecessary map file so this was the solution at the moment. If you find better way then you can suggest edit, that will be highly appreciated. – Krupal Patel May 13 '19 at 06:03
  • 4
    But when you load the application in browser, there will be 404 error for .map files – Shivang Gupta Sep 13 '19 at 07:52
  • @ShivangGupta I tested this in Chrome and Firefox and you'll only get a 404 on the source maps if you open dev tools. – Johnny Oshika Feb 28 '21 at 06:21
6

just add GENERATE_SOURCEMAP=false in .env

venky royal
  • 1,860
  • 2
  • 11
  • 19
4

Put this one in your package.json

   "build": "cross-env GENERATE_SOURCEMAP=false react-scripts build",

It works on Windows and Linux...

Toni
  • 41
  • 1
3

Solution for ejected create-react-app v2.1.3.

Go to /config/webpack.config.js directory and change the following line:

const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';

To:

const shouldUseSourceMap = false;

And Bob is your uncle.

Phoenix
  • 437
  • 3
  • 6
  • 15
0

After long struggle nothing worked. Finally what worked for me is changing sourcemap: false in webpack.config.prod.js inside nodemodules/react-script/config hopefully it will work for you too.

Kunal Burangi
  • 568
  • 6
  • 16