3

I have a react app that is created using https://github.com/facebook/create-react-app but I'd like to import some code that is shared with a NodeJs project (and thus use the commonjs syntax)

The code is exported with

exports.Agreement = agreement

When I import that code in my project using

const Agreement = require('../../common/model/agreement').Agreement

I start getting this error in my browser:

ReferenceError: exports is not defined
Module.../common/model/agreement.js
/Users/teone/Sites/cla-manager/common/model/agreement.js:217
  214 | 
  215 | export const AgreementType = agreementType;
  216 | export const AgreementCollection = agreementCollection;
> 217 | exports.Agreement = agreement;

Is commonJs supported in CRA? Is it possible to achieve this without ejecting the project?

Couldn't find any solution to it out on the web, just abandoned discussions.

A solution would be to migrate the NodeJs code to es7 import/export but then I'll have to transpile that, and I would like to avoid this unnecessary step.

Any advice is much appreciated

GockOckLock
  • 138
  • 1
  • 11
teone
  • 2,153
  • 3
  • 25
  • 49
  • Aren't you using es6 modules just above that on line 216? You could try changing line 217 to `export const Agreement = agreement` – Ben Lorantfy Feb 04 '20 at 19:15
  • No, I need to move everything to commonJs format, meaning I need to change line 215 and 216 to ``` exports.AgreementType = agreementType exports.AgreementCollection = agreementCollection ``` – teone Feb 04 '20 at 21:17

0 Answers0