0

How do you do this?

I have a statement that looks like this:

import data from '/../server/data.json';

I want to see the full path of the resolved file.

I know that there's resolve() for node but thats backend I want to do it on front end

How to get full path from relative path

The problem with this answer is it's jquery - can you do it without?

Converting Relative Paths to Absolute Paths

Not JS but java

Javascript paths relative to file

This is the closest, so I tried

import data from '/../server/data.json';

console.log(data.href)

This obviously doesn't work because it's not a valid attr.

My biggest issue here is trying to do this with an import statement. none of the attached answers discuss this.

Is this possible? Or am I in a black hole?

kawnah
  • 3,204
  • 8
  • 53
  • 103
  • This doesn't make sense and won't work. Imports are for files that are local to frontend project. Their content is retrieved at the time when the app is built. Otherwise just fetch a file from some public location. Since it's .json and not .js, there won't be problems with this – Estus Flask Aug 29 '22 at 21:43
  • except there was, and I was trying to debug it – kawnah Sep 01 '22 at 15:46

1 Answers1

1

If using Vite, you can use the url suffix on the import path:

                                         
import dataUrl from '../server/data.json?url'

If the file size is less than 4KB, it will be automatically inlined, so you'd see a data URL instead of a file path. To disable this inlining, set build.assetsInlineLimit to 0:

// vite.config.js
import { defineConfig } from 'vite'

export default defineConfig({
  build: {
    assetsInlineLimit: 0,
  },
})

demo

tony19
  • 125,647
  • 18
  • 229
  • 307