Union find
Union find

Reputation: 8150

Importing local json in main.js in Svelte

What is the pattern for importing JSON into Svelte's main.js file?

I'm trying:

import App from './App.html';
const dataset = require('./../posts.json');

console.log(dataset);

const app = new App({
  target: document.body,
  data: dataset
});

export default app;

But this does not resolve as JSON cannot be imported as an es6 module.

Upvotes: 7

Views: 7774

Answers (1)

Rich Harris
Rich Harris

Reputation: 29605

Svelte isn't involved in this process — it's entirely up to your bundler. If you're using Rollup, you'll need to add the rollup-plugin-json plugin to your rollup.config.js file.

Upvotes: 13

Related Questions