Moritz
Moritz

Reputation: 85

How to import JSON in Javascript/React properly? (VS Code)

I have this example file (example.json):

{"test": {"name": "testName"}}

Then, in my js file I import like this:

import testJson from "./example.json"

When I type: testJson.

I want suggestions to appear in the IDE (preferably in VS Code).

How can I accomplish this?

Upvotes: 1

Views: 3513

Answers (2)

Matt Bierner
Matt Bierner

Reputation: 65313

You don't need any extensions for this.

JSON imports are not supported in every module system so you need to tell VS Code that such imports are valid. To do this, in your jsconfig file, just add:

"resolveJsonModule": true

This will not only let you auto complete json import paths, but also give you proper intellisense for the import.

IntelliSense for a package.json

VS Code 1.44+ also automatically enables resolveJsonModule in all JS files that do not belong to a jsconfig:

Default behavior in VS Code 1.44

Again though, if you have a jsconfig or tsconfig you'll need to explicitly set "resolveJsonModule": true

Upvotes: 1

Mohammad Ali Rony
Mohammad Ali Rony

Reputation: 4915

Use Node JSON Autocomplete it will add autocomplete in vscode.

enter image description here

Upvotes: 2

Related Questions