106

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"
  },
7
  • Add your webpack.config.js to the question. Commented Aug 23, 2018 at 10:59
  • 2
    I didn't have a webpack.config.js file in my file structure Commented Aug 23, 2018 at 11:00
  • What did you use to generate the project? Commented Aug 23, 2018 at 11:01
  • I use npm run start Commented Aug 23, 2018 at 11:03
  • 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) Commented Aug 23, 2018 at 11:03

12 Answers 12

167

just remove &&

"scripts": {    
    "start": "react-scripts start",
    "build": "GENERATE_SOURCEMAP=false react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
Sign up to request clarification or add additional context in comments.

14 Comments

but when i try the above code the browser still shows the code. Thanks
yes. I used the same. The browser still shows the same
you have to generate project with npm run build
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:5000
Doesn't work: 'GENERATE_SOURCEMAP' is not recognized as an internal or external command, operable program or batch file.
|
73

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

1 Comment

Correct solution with [email protected]
46

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"
      },

3 Comments

set \"... windows trick works, cheers.
This method works...
Solution 1 is the preferred way. For solution 2 keep in mind that you're setting environment variables and that's why it's different between Windows and Linux.
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

Comments

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..?

2 Comments

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?
This is the CORRECT answer. Tested on Win10. Thanks!
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.

4 Comments

This shall work, but not reasonable to follow. Better not to create *.map than to create and remove it.
@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.
But when you load the application in browser, there will be 404 error for .map files
@ShivangGupta I tested this in Chrome and Firefox and you'll only get a 404 on the source maps if you open dev tools.
7

just add GENERATE_SOURCEMAP=false in .env

Comments

6

Put this one in your package.json

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

It works on Windows and Linux...

Comments

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.

Comments

1

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.

Comments

1

install cross-env npm install cross-env --save-dev

Put this one in your package.json "build": "cross-env GENERATE_SOURCEMAP=false react-scripts build", It works on Windows

Comments

0

If you're using Vite, update your vite.config.js:

export default defineConfig({
  build: {
    sourcemap: false, // Disable source maps
  },
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.