44

I created an npm library with rollup. Then, I install and use it in a React project. When i npm start the project it throw me this line:

Failed to parse source map from 'C:\Users\XXXX\Desktop\example\node_modules\@AAA\BBB\src\components\Button\Button.tsx' file: 
Error: ENO ENT: no such file or directory, open 
'C:\Users\XXXX\Desktop\example\node_modules\@AAA\BBB\src\components\Button\Button.tsx' 

This is like a warning more like an error because the button works and webpack says me "No issues found."

In the node_modules folder I have the @AAA/BBB folder with /dist/cjs and /dist/esm. I dont know what can produce the search in /src in the node modules.

Thank you for your time <3

imbr
  • 6,226
  • 4
  • 53
  • 65
RandomQuestions
  • 541
  • 1
  • 3
  • 3

8 Answers8

64

You can remove the warning by adding GENERATE_SOURCEMAP=false to your .env file

Actually, CRA with Webpack 5.x cause it. They are working on resolving (https://github.com/facebook/create-react-app/pull/11752)

Laurance Nguyen
  • 649
  • 3
  • 4
7

If you want to disable sourcemap locally only, you can disable it inside your local yarn or npm script.

 "scripts": {
    "start": "GENERATE_SOURCEMAP=false && react-scripts start",

Now, source map is disabled only when you are running the app LOCALLY.

Simran Singh
  • 2,323
  • 12
  • 21
  • 'GENERATE_SOURCEMAP' is not recognized as an internal or external command, operable program or batch file :/ – Phoxer Nov 08 '22 at 15:40
  • 1
    @Phoxer On Windows you need to run this as `set GENERATE_SOURCEMAP=false && react-scripts start`, or alternatively write `GENERATE_SOURCEMAP=false` in a file named `.env.development`. – 6infinity8 Nov 08 '22 at 15:57
3

If you do not want to use react-app-rewired, downgrade to react-scripts v4 or set GENERATE_SOURCEMAP=false, you can check out my idea. It is ugly but it works.

Create a script like so:

const { readFileSync, writeFileSync } = require("fs");

const path = "node_modules/source-map-loader/dist/utils.js";
const regex = /.*throw new Error\(`Failed to parse source map from '\${sourceURL}' file: \${error}`\);*/;

const text = readFileSync(path, "utf-8");
const newText = text.replace(regex, `buffer="";sourceURL="";`);
writeFileSync(path, newText, "utf-8");

And then add it to your package.json:

  "scripts": {
    "start": "node suppress-source-map-warnings.js && react-scripts start"

Using GENERATE_SOURCEMAP=false will make it harder for you to debug your own code so I think this script is a lesser evil.

This code will stop working when source-map-loader changes the error message. But I hope we will a real solution for it by then. Like some kind of flag to suppress warnings when source maps are not found.

2

I have similar issue with you. I resolved this issue after I install the source-map-loader package again.

npm install source-map-loader

Enjoy it.

W. Dan
  • 906
  • 8
  • 10
2

I had the same problem with my component library with Rollup + React. The issue I had was caused by conflicts between Rollup sourcemap collapser and the Typescript plugin sourcemap generator.

What worked for me was answered in this post: Rollup is not generating typescript sourcemap

In my rollup.config.mjs I had to set sourceMap: false. My working config file below:

export default [
  {
    input: 'src/index.ts',
    external: ['react', 'react-dom'],
    output: [
      {
        file: 'dist/cjs/index.js',
        format: 'cjs',
        sourcemap: true,
      },
      {
        file: 'dist/esm/index.js',
        format: 'esm',
        sourcemap: true,
      },
    ],
    plugins: [
      resolve(),
      commonjs(),
      typescript({ tsconfig: './tsconfig.json', sourceMap: false }),
      postcss(),
    ],
  },
  {
    input: 'dist/esm/types/index.d.ts',
    output: [{ file: 'dist/index.d.ts', format: 'esm' }],
    plugins: [dts()],
    external: [/\.(css|less|scss)$/],
  },
];

Disclaimer: When fixing this, I do get the warning while compiling esm directory:

src/index.ts → dist/cjs/index.js, dist/esm/index.js...
(!) Plugin typescript: @rollup/plugin-typescript: Typescript 'sourceMap' compiler option must be set to generate source maps.
created dist/cjs/index.js, dist/esm/index.js in 3.7s

The source map problem is fixed, but I'll have to resolved this warning later.

Hope this helps!

1

Updated answer: I solved this issue (amongst others), by installing an npm package called 'rollup-plugin-sourcemaps' and having sourcemaps() inside my plugins array. It is extremely important to consider the order in which the plugins are located at in the array as it will run from left to right. So that means you could be trying to map sources before you have the appropriate code to reference it. This is the order in which I have my plugins in:

plugins: [
          resolve(),
          peerDepsExternal(),
          babel(
            {
              plugins: ['babel-plugin-styled-components'],
              exclude: ['node_modules/**', 'public/**'],
              extensions: ['.js', '.jsx', '.ts', '.tsx'],
              inputSourceMap: true

            }
            ),
          typescript({ tsconfig: './tsconfig.json'}),
          sourcemaps(),
          commonjs({ extensions: ['.js', '.jsx', '.ts', '.tsx'], 
               include: /node_modules/ }),
          url(),
          postcss(),
          terser()
          ]

If the problem persists, you may need to change some properties in the compilerOptions in your tsconfig.json to target src such as "sourceRoot": "src".

Old Answer: I've had the same problem now in 04/2023. When using sourcemapping in our case, the mapping is looking for the src folder as a reference. It turns out that everything should be working as intended, but I didn't generate the src folder it is looking for. You don't need to disable source mapping, as it is highly recommend you use them. Adding "src" in the "files" array in package.json finally generated a copy of the src folder from my project.

"files": [ "dist", "src" ]

When you bundle everything with run rollup, the src folder is not present in the dist folder. But after downloading the package, it appears. These are the rollup.config.mjs and tsconfig.json files that worked for me. Before I included the src folder with its contents in the result bundle, limited testing on react 17 and nextjs 13 had everything working for me but produced the same error messages about source mapping. Including the src folder in the build only fixed the multiple errors it was displaying but still worked as before. This didn't really increase the bundle size for me though, but I'm not sure if it is a concern on bigger libraries.

const getTypesPath = (jsFile) => {
  const pathInfo = parse(jsFile);
  return format({
      ...pathInfo,
      base: '',
      dir: `${pathInfo.dir}/types`,
      ext: '.d.ts',
  });
};

const config =  [
  {
    input: "src/index.ts",
    output: 
    [ {
        file: pkg.main,
        format: "cjs",
        sourcemap: true
      },
      {
        file: pkg.module,
        format: "esm",
        sourcemap: true,
        preserveModulesRoot: 'src'
      }],
    external: ['react','react-dom'],
    plugins: [
      peerDepsExternal(),
      resolve(),
      commonjs({ extensions: ['.js', '.jsx', '.ts', '.tsx'] }),
      typescript({ tsconfig: "./tsconfig.json" }),
      url(),
      postcss(), 
      terser(),
    ]
  },
  {
    input: getTypesPath(pkg.module ?? pkg.main),
    output: [{ file: pkg.types, format: 'esm' }],
    plugins: [dts()],
}
]
{
  "compilerOptions": {
    "target": "ES2016", 
    "esModuleInterop": true, 
    "forceConsistentCasingInFileNames": true,
    "strict": true, 
    "skipLibCheck": true,
"resolveJsonModule": true,
    "jsx": "react-jsx", 
    "module": "ESNext",  
    "declaration": true,
    "declarationDir": "./types",
    "sourceMap": true,
    "outDir": "types",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true, 
    "emitDeclarationOnly": true,
    "rootDir": "src",
    "paths": {
      "common": ["./src/*"],
      "components": ["./src/components/*"]
    }
  },
   "exclude": 
      [
        "node_modules",
        "**/*.stories.tsx",
        "**/*.test.tsx",
        "**/tests",
        "**/setupTests.ts"
      ]           
}
Jason Cruz
  • 11
  • 1
  • 3
0

I am using Webpack 5. Had the same problem as you.
The error message looks like this:

WARNING in ./node_modules/{{Placeholder}}/{{Placeholder}}.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from 'D:\Users\{{Placeholder}}\Desktop\test-app\node_modules\{{Placeholder}}\{{Placeholder}}.ts' file: Error: ENOENT: no such file or directory, open 'D:\Users\{{Placeholder}}\Desktop\test-app\node_modules\{{Placeholder}}.ts'

The solution is that you can ignore packages you don't need in the Webpack config file.

rules: [
  // Handle node_modules packages that contain sourcemaps
  shouldUseSourceMap && {
    enforce: 'pre',
    exclude: [/@babel(?:\/|\\{1,2})runtime/, /node_modules(\/|\\)somePackage/],
    test: /\.(js|mjs|jsx|ts|tsx|css)$/,
    loader: require.resolve('source-map-loader'),
  },
];

FYI: https://webpack.js.org/configuration/module/#ruleexclude

Jef
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 24 '23 at 00:52
-3
  1. delete package-lock.json file
  2. update npm => I have tried and succeeded