Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transpiling failure for module.exports syntax within src/

See original GitHub issue

I don’t think this is necessarily a bug, and may be related to the 2.0+ changes to transpilation, but…

Steps to reproduce (I was using Node 10.13.0, if that matters):

  1. Create a new create-react-app project with react-scripts@2.1.2.
  2. Create two files: src/es6.js and src/cjs.js.
// es6.js
export default ({ foo, }) => true;

// cjs.js
module.exports = ({ foo, }) => true;
  1. Run yarn start.

  2. Import both of them into App.js, one at a time:

import es6 from "./es6.js";
// or
import cjs from "./cjs.js";

The use of the Object rest/spread syntax is allowed in the ES6 module syntax but not in the CommonJS module.exports version.

In the latter case, you get one of two errors, depending on whether it’s the first attempt to build or a subsequent attempt:

Attempted import error: './cjs.js' does not contain a default export (imported as 'cjs').


TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

If you don’t use the rest/spread syntax, and just use plain destructuring (e.g. ({ foo, bar }) => true), both files work.

It seems like this has something to do with the dynamic module.exports triggering a different transpilation, but I’m not sure exactly what’s happening. It clearly does get transpiled - if you inspect the built output, it’s been rendered ES5-compatible. But the rejection of the rest/spread indicates that the CJS file is getting transpiled… less?

Is this the intended behavior? Should CJS modules in src/ have a different set of language features allowed than ES6 modules? If so, is there a point in the config where this behavior can be changed? Am I missing something obvious?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

stale[bot]commented, Mar 6, 2019

This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript external node modules sometimes transpile to ...
exports " so I can't explain why this is causing mocha to fail but I do know that if I switch between the...
Read more >
How to transpile ES modules with webpack and Node.js
Learn how webpack interacts with and supports ES modules in this deep dive tutorial on transpilation in Node.js.
Read more >
How To Use Modules in TypeScript | DigitalOcean
Creating Modules in TypeScript with export. In this section, you will create modules in TypeScript using the TypeScript module syntax.
Read more >
JavaScript - Parcel
ES module syntax is the standard way to import and export values between files in JavaScript. It should be preferred over CommonJS for...
Read more >
ts-node - npm
Modules should always be published in a format node.js can consume; Transpiling the entire dependency tree will make your project slower ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found