Flow: requiring doesn't work with backticks

Created on 27 Oct 2016  路  10Comments  路  Source: facebook/flow

// @flow

const Hapi = require(`hapi`)

gives this:

server.js:9
  9: const Hapi = require(`hapi`)
                  ^^^^^^^^^^^^^^^ The parameter passed to require() must be a literal string.
enhancement

Most helpful comment

Sorry I didn't see this before, but if you wish to suppress this particular kind of error you can use the following config flag:

module.ignore_non_literal_requires=true

Note that if you do this, Flow won't be able to extract a type for the require(), though (which is why it errors by default).

All 10 comments

backticks*

thanks :-)

Sorry I didn't see this before, but if you wish to suppress this particular kind of error you can use the following config flag:

module.ignore_non_literal_requires=true

Note that if you do this, Flow won't be able to extract a type for the require(), though (which is why it errors by default).

I think we should just support template literals as long as there are no expressions

@jeffmo it would be nice to parse contents of template and figure out that it's static though

The changelog says this was fixed in v0.38.0 but I'm using v0.41.0 and still seeing this error:

65:         const originalCode = routeParams.component ? require(`raw-loader!./../examples/${routeParams.component}`) : null;
                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The parameter passed to require() must be a literal string.

Any idea what could be going wrong?

@EvNaverniouk You are using ${routeParams.component} to inject a non-constant string into your module name. This is not allowed.

This is because flow runs staticly. It can't verify which module it will resolve to dynamically.

How could I mitigate this? Is there a way to tell Flow not to check this line? Or to accept a non-constant string for all require statements?

@EvNaverniouk Is this not precisely what module.ignore_non_literal_requires=true does? (mentioned above)

Was this page helpful?
0 / 5 - 0 ratings