5

I've tried everything i could find so far and i'm still getting the error "exports is not defined".

I am using ReactJS.NET (for NetCore2) and it is mandatory otherwise my entire app will not load under NetCore2.

This is my .babelrc

{
  "presets": [
    "@babel/preset-react",
    "@babel/preset-env",
    "@babel/preset-typescript"
  ],
  "plugins": [
    "add-module-exports",
    "@babel/plugin-proposal-class-properties",
    [
      "@babel/plugin-transform-runtime",
      {
        "corejs": 2,
        "helpers": true,
        "regenerator": true,
        "useESModules": true
      }
    ]
  ]
} 

Everything worked fine with the older babel and "add-module-exports". Is there an alternative for babel 7?

Dante R.
  • 902
  • 3
  • 13
  • 41

1 Answers1

4

A couple of things:

Update

I did not notice that you are using typescript. In that case, you probably want to do this instead:

1) Change your .babelrc to:

{
  "presets": [
    "react",
    ["env", {"modules": false} ],
    "typescript"
  ],
  "plugins": [
    "add-module-exports",
    "@babel/plugin-proposal-class-properties",
    [
      "@babel/plugin-transform-runtime",
      {
        "corejs": 2,
        "helpers": true,
        "regenerator": true,
        "useESModules": true
      }
    ]
  ]
}

2) make sure that your tsconfig.json has the following entry: "module": "commonjs",

Josep
  • 12,926
  • 2
  • 42
  • 45
  • Adding `plugin-transform-modules-commonjs` fixed my problem. `"modules":false` says its a removed option (not available in babel v7). I'm using TS and JS in the same project (i plan on moving everything to TS later on). Well anyway, i suppose it works since that error does not pop-up anymore. Now ReactJS.NET fails to render with the beloved `Invariant Violation: expected a string...` error T_T. Thanks ! – Dante R. Nov 26 '18 at 11:24
  • In the same answer, you first advise that "Babel 7 has dropped the use of add-module-exports plugin", then in the addition for TypeScript, you advise changes to .babelrc that include the add-module-exports plugin. Now has it's use been dropped or not? – ProfK Mar 06 '23 at 23:00