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
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”
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"
Care to share a screenshot of your directory tree?
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
ohh sure inside assets
thx now its working
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)