N
Nuxt8mo ago
ciaddi

How to import functions in a .vue component from a .js file?

import { myfunctions} from "/js/stats.js" doesnt work for me: Error 500 vite_ssr_import_7.default.getData is not a function
7 Replies
pyplacca
pyplacca8mo ago
You probably don’t have a function with that name And check the path too. You probably wanted to type ”~/js/stats.js” or ”./js/stats.js” instead of ”/js/stats.js”
ciaddi
ciaddiOP8mo ago
Hello, there is a function called getData inside my js file: "export const utils = { getData: function () { return "Test" } }". and then import { utils } from "/js/stats.js" but utils.getData() gives me the error "Cannot read properties of undefined (reading 'getData')". Also your suggestions doesnt work "[vite-node] [ERR_LOAD_URL] ~/js/stats.js"
pyplacca
pyplacca8mo ago
Care to share a screenshot of your directory tree?
ciaddi
ciaddiOP8mo ago
No description
pyplacca
pyplacca8mo ago
I’d recommend you relocate the js folder outside of the public folder. I don’t think nuxt allows you to import from that directory inside your scripts
ciaddi
ciaddiOP8mo ago
ohh sure inside assets thx now its working
pyplacca
pyplacca8mo ago
You can also make use of the root-level utils directory nuxt offers, and keep all your utility functions there. This directory has built-in support for auto imports (if it’s something you prefer)
Want results from more Discord servers?
Add your server