0

I am using react server side rendering with node to generate pdf.

I have 3 files

index.js, PDF.jsx, helper.jsx

index.js contains following code.

let fileContent = await Fsp.readFile('PDF.jsx', 'utf-8');
let code = Babel.transform(fileContent, {
  presets: ['@babel/preset-env', '@babel/preset-react'],
  comments: false,
  minified: true
}).code;

PDF.jsx contains following import

import * as Helper from './Helper.jsx';

Helper.jsx contains following code

class Helper{
}
export default Helper;

But it gives following error

export default Helper;
^^^^^^
SyntaxError: Unexpected token export
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18) 
  • In this case, you can look for esm module – BENARD Patrick Jul 15 '20 at 11:00
  • Have you tried import {default as Helper } from './Helper.jsx'; in your pdf.jsx file – Afaq Ahmed Khan Jul 15 '20 at 11:09
  • It does not work – Arkesh Kalathiya Jul 15 '20 at 15:09
  • @BENARDPatrick Can you explain me in little bit more detail? is it plugin or preset? Until now I was using ready made boilerplate, so I am not familiar with these concepts. – Arkesh Kalathiya Jul 15 '20 at 15:12
  • I tried something for a personnal project. My expressjs is made with normal `module.exports` and I had to read react files.... So in running my project I had to run `node esm -r myapp.js` which allow me to read and import `import default` file from react source... The only problem I had was to import the react file containing the JSX render... Anyway, I didn't need it, I'm working on a react framework with logic and view separated... I only need the logic... – BENARD Patrick Jul 15 '20 at 15:39
  • Yes, that's the place where I have issues, because my Helper.jsx file contains mainly rendering logic. – Arkesh Kalathiya Jul 16 '20 at 03:32

0 Answers0