Issue with VueJS compilation: JSON data import causing failure

I'm attempting to bring in a static .json file within the <script> section of a .Vue file using the code snippet

import Test from '@assets/test.json'

From what I've gathered about webpack, this should work effortlessly. I have even gone as far as explicitly specifying both the .json extension in webpack resolve and including the json loader under loaders.

Despite my efforts, it continues to fail with the following error:

ERROR  Failed to compile with 1 errors                                                                                                                                                                                                     9:14:24 AM

This dependency was not found:

* @assets/test.json in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/Settings.vue?vue&type=script&lang=js&

To install it, you can run: npm install --save @assets/test.json

This seems peculiar since it's not a dependency that is meant to be installed?

The content of my test.json file simply contains {}, but I've also attempted it with an actual data file resulting in the same error message.

Thank you in advance for any assistance.

Edit: A big thank you to @tao for assisting me in resolving the issue through chat. The mistake was indeed straightforward:

import Test from '@assets/test.json'
should have been
import Test from '@/assets/test.json'

Answer №1

To enable TypeScript support, you must include the following:

"resolveJsonModule": true,

inside the compilerOptions section of your tsconfig.json file.

Add the following as well:

declare module '*.json' {
  const value: unknown;
  export default value;
}

This code should be added to your shims-vue.d.ts file.


In JavaScript, there is no special setup required. The following code will work:

test.json:

{
  "foo": "bar"
}

Your *.vue single file component (SFC) file:

<script>
import * as json from './test.json';

export default {
  data() {
    return {
      json
    }
  },
  mounted() {
    console.log(this.json)
  }
}
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Learn how to verify the existence of a URL or webpage using AJAX or jQuery

When a user is creating a profile, they have the option to include links to their websites or blogs that will be displayed on their profile page. Before saving these links to the database, I would like to verify if the provided URL exists. Is there a meth ...

What could be the reason for the page scrolling upwards when clicking on href="#"?

I am encountering an issue with a hyperlink <a href="#" id="someID">Link</a> that is located further down the page. This link is used to trigger an Ajax request, but whenever it is clicked, the page automatically scrolls back to the top. I have ...

Exploring the possibilities: Establishing a Connection between Node.js and MySQL

I encountered an issue while attempting to connect node.js to MySQL. Despite having installed MySQL and the necessary libraries, I am unable to establish a connection. How can I troubleshoot this error? Additionally, what is the best approach for retrievin ...

Prevent Nuxt from refreshing the page when the URL is modified

Currently, I am in the process of transitioning an application to Nuxt3 and encountering an issue regarding page reloading when the URL changes. Within my application, I have multiple links that all lead to the same page. To address this issue, I had to ma ...

The process for changing the textContent to X when an image is clicked

How can I create a function that changes the text content to 'X' when an image is clicked? I already have a function that updates the title based on the image dataset, but combining the two functions has been unsuccessful. Can someone help me con ...

"Enhance your website with interactive jQuery lightbox buttons for easy navigation

As I explore the jquery lightbox plugin, I find it to be quite straightforward. However, I am curious if there is a way to make the "previous" and "next" buttons of the plugin function without the images needing to be named sequentially (e.g., "image1.jpg, ...

Incorporate a PHP GET parameter through an onclick event to redirect using window.location in JavaScript when a button is

I am trying to modify the onclick event of buttons that look like this: <button class="buttons" onclick="window.location='page_test.php?VAR=1&VAR2=2'">BUTTON 1</button> <button class="buttons" onclick="window.location='p ...

Alter the style type of a Next.js element dynamically

I am currently working on dynamically changing the color of an element based on the result of a function //Sample function if ("123".includes("5")) { color = "boldOrange" } else { let color = "boldGreen" } Within my CSS, I have two clas ...

Facing problem with Angular 7 when making a GET request for non-JSON data

Currently, I am retrieving JSON data from a URL using the following method: this.http.get('http://localhost:3200/mydata').subscribe(data => { console.log(data); }); The response is in JSON format, and everything seems to be working fine. ...

Having trouble with reactjs and typescript? Getting the error message that says "Type 'string' is not assignable to type 'never'"?

When trying to setState with componentDidMount after an axios request is completed, you may encounter the error message Type 'string' is not assignable to type 'never'. Below is the code snippet: import * as React from 'react&apos ...

None of the NPM commands are working due to an error stating: "TypeError: Class extends value undefined is not a constructor

For years, I've had no issues using n to install node. Recently, on my Big Sur mac, I decided to update from v14.17.6 to v16 without any problems. Now when I check the version with node -v, it shows "16.13.1." However, after the update, every npm com ...

Tips for uploading my npm project to the GitLab NPM Registry?

I recently activated a trial gold plan in order to explore the functionality of GitLab NPM Registry. First, I created a new GitLab project named "bar". Next, I generated an authentication token by creating a file called auth.txt with the following line ...

Looking to add some random circle markers to your SVG map?

I currently have a single marker displayed on an SVG Map. My goal is to scatter markers randomly across the map within the path itself. I would greatly appreciate some assistance in implementing this feature. svg { border: 1px solid; overflow: visib ...

Creating a dual-column checkbox design with CSS only

I'm working with a dynamically generated form element that can only be styled using CSS. I need help achieving a specific layout without making any HTML changes, except for the classes. The "element" and "formField" classes are common and cannot be mo ...

Tips for including an object as a prop using Vue Router

When working with Vue JS3, I have encountered an issue regarding passing props in my Vue component. Here is how I am currently doing it: <script> export default { data() { return { links: [], }; }, methods: { editLink(el) { this.$ ...

Encountered a problem while attempting to launch the next js development

An error has occurred: 'NextJS\the-wild-oasis-website\node_modules.bin' is not recognized as an internal or external command, operable program or batch file. node:internal/modules/cjs/loader:1148 throw err; Error: Cannot find module &ap ...

Capturing information within a jQuery function by accessing data from another function

Can data be collected from another function while the function is running? // Custom Function function getData(){ var name = 'tom'; return name } // Main Target Area $('.myDiv').click(function(){ // I need to retrieve dat ...

What is the best way to clear an input value in Vuejs after it has been submitted?

Could you help me with my to-do list in Vuejs? I'm having trouble resetting the input value when a new item is added. Any suggestions on how to achieve this? I've attempted to retrieve the input value and set it to an empty string, but unfortuna ...

What is the best way to render a view, hide parameters from the URL, and pass data simultaneously?

In order to display the current view: statVars["sessions"] = userSessions; res.render("statsSessions", statVars); Nevertheless, I must find a way to hide the URL parameters from showing up in the browser's address bar (these parameters are sourced ...

Having difficulty sending string values with Axios and FormData in Vue.js

Is there a way to send both a file input and text input in a single request using Axios and FormData in Vue.js? I've come across a solution that seems straightforward: const formData = new FormData(); formData.append('file', file); formData. ...