How to disable source maps for React JS Application?
Asked Answered
C

10

99

My react folder structure is as below:

enter image description here

I haven't 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 can't remove source maps.

 "scripts": {

    "start": "react-scripts start",
   "build": "GENERATE_SOURCEMAP=false && npm run build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
Cremona answered 23/8, 2018 at 10:54 Comment(7)
Add your webpack.config.js to the question.Keturahkeung
I didn't have a webpack.config.js file in my file structureCremona
What did you use to generate the project?Keturahkeung
I use npm run startCremona
you can not do it because create-react-app config webpack for you, if you want to manually setup your webpack, you have to run npm run eject (which is not recommended for beginner)Aleph
Sorry I didn't get you. How can I resolve this.Cremona
@BharathPabba, for build you can enable it, I've answered it belowAleph
A
161

just remove &&

"scripts": {    
    "start": "react-scripts start",
    "build": "GENERATE_SOURCEMAP=false react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
Aleph answered 23/8, 2018 at 11:6 Comment(14)
but when i try the above code the browser still shows the code. ThanksCremona
are you sure that your code is this "build": "GENERATE_SOURCEMAP=false react-scripts build" not this "build": "GENERATE_SOURCEMAP=false && react-scripts build"Aleph
As you are new to React, i strongly advice you to gor for create-react-app option and follow the react documentation ;)Sacerdotalism
yes. I used the same. The browser still shows the sameCremona
you have to generate project with npm run buildAleph
if you want to run build file first you need to install serve globally ‘npm run serve -g’ and ‘serve build’, after that you can open it in browser with localhost:5000Aleph
I cannot use serve on ec2 properly. Please reply if you can help me out this Issue #51996732Cremona
"build": "GENERATE_SOURCEMAP=false react-scripts build", Worked for me!Dinger
The below answer is recommendedEggert
Doesn't work: 'GENERATE_SOURCEMAP' is not recognized as an internal or external command, operable program or batch file.Polyphone
@PointerNull use cross-env: "build": "cross-env GENERATE_SOURCEMAP=false react-scripts build"Philbrick
@Philbrick 'cross-env' is not recognized as an internal or external command,Ewaewald
@B.ÖNER you have to install the module.Philbrick
@Philbrick Thanks. It worked after installing using "npm install --save-dev cross-env"Ewaewald
B
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

Blacksnake answered 9/2, 2019 at 19:13 Comment(1)
Correct solution with [email protected]Enwomb
C
41

Solution 1 (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

Solution 2

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"
      },
Carruth answered 4/9, 2020 at 17:12 Comment(1)
set \"... windows trick works, cheers.Outpoint
H
37

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

GENERATE_SOURCEMAP=false
Hylotheism answered 6/3, 2020 at 14:7 Comment(0)
B
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..?

Buckeen answered 6/6, 2019 at 10:38 Comment(2)
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?Immunogenic
This is the CORRECT answer. Tested on Win10. Thanks!Miniaturize
P
9

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.

Peirsen answered 22/11, 2018 at 4:42 Comment(4)
This shall work, but not reasonable to follow. Better not to create *.map than to create and remove it.Dogmatics
@Dogmatics 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.Peirsen
But when you load the application in browser, there will be 404 error for .map filesHouseman
@ShivangGupta I tested this in Chrome and Firefox and you'll only get a 404 on the source maps if you open dev tools.Oospore
O
6

just add GENERATE_SOURCEMAP=false in .env

Outboard answered 9/9, 2021 at 8:41 Comment(0)
O
5

Put this one in your package.json

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

It works on Windows and Linux...

Obstinate answered 6/5, 2021 at 13:43 Comment(0)
F
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.

Fluviatile answered 13/1, 2019 at 20:41 Comment(0)
R
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.

Ripuarian answered 15/7, 2019 at 5:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.