Create-react-app: Missing export should be a compile error

Created on 15 Feb 2017  路  15Comments  路  Source: facebook/create-react-app

This is only relevant in master which uses Webpack 2.

If I change src/index.js from

import App from './App';

to

import {App} from './App';

I'm getting:

screen shot 2017-02-15 at 00 40 43

The screen is blank (because of the runtime error).

Notice how the real problem is at the very bottom (missing export), yet it looks like the first two are the important ones.

The terminal output is also very restrained (because it is a warning):

screen shot 2017-02-15 at 00 43 03

I wonder what are the reasons for this. I think a missing ES6 export should totally be a compile error because it is so easy to mess it up, and then have weird runtime errors as a result.

I have two questions:

  • Why isn鈥檛 in an error by default?
  • Can we make this an error in CRA setup?

Most helpful comment

Some typescript constructs emit incorrect import/exports (related to the loader impl, not sure if it's fixed now). So we used a warning. But I guess it fine to add an option upgrading it to an error.

All 15 comments

Seems like we need to set strictModuleExceptionHandling to true.
It was added but never documented, and that's why we missed it.

Rad. Anybody wants to make a PR?

The config schema documentation for this says "Handles exceptions in module loading correctly at a performance cost" - should this be switched on in production builds?

Perhaps we can test it on a relatively large application? Would anyone be willing to do this?

It looks like this config triggers generation of try/catch wrappers when calling the generated module function at runtime, but doesn't upgrade missing modules to an error at build time.

After further investigation after what @insin, it seems strictModuleExceptionHandling isn't what we want (despite some various sources I found). It looks like we need to add a custom hook to parse the webpack warnings.

Maybe @sokra could give us some more insight?

+1, doesn't work for me. I've added output: { strictModuleExceptionHandling: true } option in webpack config and import { nonexistentModule } from 'someFile' in a source file, no errors in terminal.

strictModuleExceptionHandling is not related to import. It's about exceptions in modules. There is currently no way to make the warning an error (except with a custom plugin).

Is there a reason why? I鈥檓 just curious what the rationale is.

Some typescript constructs emit incorrect import/exports (related to the loader impl, not sure if it's fixed now). So we used a warning. But I guess it fine to add an option upgrading it to an error.

Once https://github.com/webpack/webpack/pull/4348 is merged we will be able to set module.strictExportPresence to true for this behavior.

Is it worth setting strictModuleExceptionHandling to true? From what I understand it's more compliant with spec.

/cc @gaearon

I'd rather avoid a try catch in init code.

module.strictExportPresence was merged. Waiting for webpack release.

Released in WP 2.3.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fson picture fson  路  3Comments

dualcnhq picture dualcnhq  路  3Comments

alleroux picture alleroux  路  3Comments

adrice727 picture adrice727  路  3Comments

DaveLindberg picture DaveLindberg  路  3Comments