Flow: Understand .json imports

Created on 9 Jul 2015  路  10Comments  路  Source: facebook/flow

Per https://nodejs.org/api/modules.html#modules_file_modules, require('foo.json') should return the JSON object. right now we don't allow it at all. we should _at least_ make it resolve successfully. ideally it could also parse the JSON and create the corresponding type.

Most helpful comment

You can provide a type definition for the json object. For example: https://github.com/facebook/mention-bot/commit/32cafebc926d88c05e99d6abdd1dec91d39af086

// interfaces/package.json.js
declare module "./package.json" {
  declare var config: {
    gheHost: string,
    ghePathPrefix: string,
    gheProtocol: string,
    ghePort: number,
  };
}

All 10 comments

Should just require have this behavior, or should we also do this with ES6 import syntax? Babel does convert to require, so it would "work" on Node.js-based projects, but I don't believe ES6 specifies importing non-JS files.

Also, we should be able to co-opt the existing FlowJSON module for this, yeah?

I was about to post this issue - we are using a local json file in development containing our environment settings and it throws a Flow error.

+1, require should work the same way as node.

I would add that require('../foo') should properly first attempt to resolve to .js, then .json then .node.

You can provide a type definition for the json object. For example: https://github.com/facebook/mention-bot/commit/32cafebc926d88c05e99d6abdd1dec91d39af086

// interfaces/package.json.js
declare module "./package.json" {
  declare var config: {
    gheHost: string,
    ghePathPrefix: string,
    gheProtocol: string,
    ghePort: number,
  };
}

PS: @mroch has a proper fix for this that will hopefully make it into v0.20 this week

@mroch @jeffmo, did this make it into 0.20? Running 0.20.1 but seems like getting Required module not found.

yeah, it's there now. make sure the .json file isn't hidden by the [ignores] section, that's a common problem. if not that, try running flow check --debug to see if it lists your .json file (should say something about the initial provider).

@mroch, @jeffmo

I found my problem, I would say this is a bug in json inference.

If foo.json looks like this

{
}

Everything is fine, running flow check --debug list the initial provider <file>.

If foo.json looks like this [...] or "foo" or true, which is also a valid .json file, then the file is not listed when running flow check --debug....

Should I file a separate ticket?

^ That is 0.20.1. I will grab 0.22 and try there as well.

Looks like 9382300fc24 may fix part of this at least.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jamiebuilds picture jamiebuilds  路  3Comments

marcelbeumer picture marcelbeumer  路  3Comments

funtaps picture funtaps  路  3Comments

mjj2000 picture mjj2000  路  3Comments

john-gold picture john-gold  路  3Comments