Skip to content

Switch to JSON module imports #14611

@brendankenny

Description

@brendankenny

I was looking at https://nodejs.org/api/esm.html#json-modules and noticed there's no mention of --experimental-json-modules anymore, removed in nodejs/node#41736.

I thought it would be a very long process to get there, but import assertions and json modules went stage 3, and since they were already shipping in V8 for some time, the change was mostly just removing code preventing json imports without the flag. That also made backporting the change to 16.15.0 easy, which means all node versions that Lighthouse supports will also support json modules.

Two nice things about this:

  • loading json becomes a regular import (just import pkg from '../package.json' assert {type: 'json'} or pass {assert: {type: 'json'}} into the dynamic import())
  • tsc's resolveJsonModule works with them, so the imported JSON automatically gets types like they had when we were using commonjs

The only tooling change needed is "module": "esnext" in tsconfig-base.json and any json files imported will need to be in the tsconfig include list, and then any json imports should work in node, as well as correctly type check and lint.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions