0

I am trying to use babel module resolver plugin with eslint + create react app but I am unable to start the application, getting the error

internal/modules/cjs/loader.js:1237
throw err;
^

SyntaxError: C:\Users\enisr\Desktop\projects\pcPartPicker\jsconfig.json: 
Unexpected token } in JSON at position 166
at parse (<anonymous>)

I have set up a git repo showcasing the problem https://github.com/sgoulas/pcPartPicker

I have read the instructions in the docs and in the original repository and I am unable to configure it correctly.

My configuration files are the following:

.babelrc

{
"plugins": [
    ["module-resolver", {
        "extensions": [
            ".js",
            ".jsx",
            ".es",
            ".es6",
            ".mjs"
        ],
            "root": ["./src"],
            "alias": {
                "@components": "./src/components"
            }
        }
    ]
]
}

jsconfig.json

{
"compilerOptions": {
    "baseUrl": ".",
    "paths": {
         "*": ["src/*"],
        "@components/*": ["./src/components/*"],
    }
}
}

webpack.config.dev.js

var path = require("path");

module.exports = {
  include: path.appSrc,
  loader: require.resolve("babel-loader"),
  options: {
    plugins: [
     [
    "module-resolver",
    {
      root: ["./src/App"],
      alias: {
        "@components": "./src/components",
      },
        },
      ],
    ],
    cacheDirectory: true,
  },
};

My component:

import { GPUtable, CPUtable } from "@components/Tables";

const App = () => {
  return (
    <>
      <GPUtable />
      <CPUtable />
    </>
  );
};

export default App;

Prinny
  • 208
  • 6
  • 16
  • @JBallin I don't think so, since this React project has no typescript in it. – Prinny Nov 24 '20 at 09:28
  • Are you sure you don't need to eject your create-react-app for this? I don't think you just add a webpack config on top of CRA. – JBallin Nov 25 '20 at 02:26

1 Answers1

1

There are some minor fixes you need to make (below), but the main issue is that Create React App does not expose the webpack config, you'll need to eject to edit that.

  1. npm run eject
  2. Merge the babel configs: delete the babel key + value at the bottom of the package.json, and paste the value into your bablrc ("presets": ["react-app"],).
  3. Add import React from 'react'; to the top of App.js

Confirmed locally that the app will run.


Other suggested fixes

  1. Your jsconfig has a trailing comma after the array value in @components/*. You need to remove it because JSON doesn’t support them.
  2. You need to fix the include path in weback.config.dev.js. appSrc isn't something defined on the node path module. Try using path.resolve(__dirname, 'src') - the example in their docs is creating/importing a paths object with appSrc pointing to this value.
  3. You're missing test: /\.(js|jsx|mjs)$/, in webpack.config.dev.js.
JBallin
  • 8,481
  • 4
  • 46
  • 51
  • Removing the trailing comma does not solve the issue. I still get "Can't resolve '@components/Tables" error compilation error. – Prinny Nov 24 '20 at 17:35
  • I implemented the changes and now I get "Module not found: Can't resolve '@components/Tables' in 'C:\Users\enisr\Desktop\projects\pcPartPicker\src' Error from chokidar (C:\): Error: EBUSY: resource busy or locked, lstat 'C:\DumpStack.log.tmp'". In any case I am pushing all the changes I am doing to the repo. – Prinny Nov 24 '20 at 21:51
  • I am still getting './src/App.js Module not found: Can't resolve '@components/Tables' in 'C:\Users\enisr\Desktop\projects\pcPartPicker\src' Error from chokidar (C:\): Error: EBUSY: resource busy or locked, lstat 'C:\DumpStack.log.tmp'; in my compilation terminal. Updated the repo with the change you mentioned. – Prinny Nov 25 '20 at 01:24
  • Alright I got it working now, finally had a chance to pull it down. – JBallin Nov 25 '20 at 02:41
  • the issue was resolved after following your instructions. Thank you for your help and for sticking with me so far. Have a nice day. – Prinny Nov 25 '20 at 14:59
  • Awesome, you too! Thank you for the bounty ❤️ – JBallin Nov 25 '20 at 19:50