Vue 3 - Dynamic require of "highcharts" is not supported - vue.js

I am trying to include highcharts-vue like this:
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import HighchartsVue from "highcharts-vue";
const app = createApp(App)
app.use(createPinia())
app.use(router)
app.use(HighchartsVue);
app.mount('#app')
But I'm getting the error:
Dynamic require of "highcharts" is not supported
package.json
{
"name": "mwc-web",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview --port 5050"
},
"dependencies": {
"#popperjs/core": "^2.11.4",
"bootstrap": "^5.1.3",
"bootstrap-vue-3": "^0.1.9",
"highcharts": "^10.0.0",
"highcharts-vue": "^1.4.0",
"moment": "^2.29.1",
"pinia": "^2.0.11",
"vue": "^3.2.31",
"vue-router": "^4.0.12"
},
"devDependencies": {
"#vitejs/plugin-vue": "^2.2.2",
"#vue/cli-plugin-babel": "^5.0.4",
"sass": "^1.49.9",
"sass-loader": "^12.6.0",
"vite": "^2.8.4"
}
}
vite.config.js
import { fileURLToPath, URL } from 'url'
import { defineConfig } from 'vite'
import vue from '#vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'#': fileURLToPath(new URL('./src', import.meta.url))
}
},
optimizeDeps: {
exclude: ['highcharts'],
}
})
babel.config.js
module.exports = {
presets: [
'#vue/cli-plugin-babel/preset'
]
}

I think the cause of the issue is this config:
optimizeDeps: {
exclude: ['highcharts'], // ❌ don't do this
}
The solution would be to remove highcharts from optimizeDeps.exclude.
Also, you don't need a Babel config when using Vite. That might be another source of the problem.
demo

Here's a working example of vue3 + highcharts-vue: https://codesandbox.io/s/vue3-highcharts-example-k98kyz
Be sure to update to the latest versions, and to use babel or any other transpilers to work with ESM packages.

Related

Vue router not routing with URL paths

So I'm working with Vue 3 and Vue Router 4 and I'm having an issue when running my code with webpack-dev-server. Whenever I run my app webpack-dev-server --mode development the landing page looks great and loads everything from my App.vue file. However, whenever I change the route from the base route / to my new component /test it just loads the same exact view as the base route. I've been browsing for hours and can't seem to figure out what's wrong when I'm sure it's a simple fix. Here's the current structure of my directory:
src
components
Homepage.vue
router
index.js
App.vue
index.html
main.js
package.json
webpack.config.js
here is what's inside some of those important files
src/router/index.js
import Vue from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import App from '../App.vue'
import Homepage from '../components/Homepage.vue'
const routes = [
{ path: '/', component: App },
{ path: '/test', component: Homepage }
]
const router = createRouter({
history: createWebHistory(),
routes: routes
})
export default router
src/main.js
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
createApp(App)
.use(router)
.mount('#app');
package.json
{
"name": "web",
"version": "1.0.0",
"description": "",
"scripts": {
"start": "node index.js",
"serve": "webpack-dev-server --mode development",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.2.5",
"bootstrap": "^5.2.1",
"express": "^4.18.1",
"uuid": "^9.0.0",
"vue": "^3.2.39",
"vue-router": "^4.0.13"
},
"devDependencies": {
"#babel/core": "^7.19.0",
"#babel/preset-env": "^7.19.0",
"babel-loader": "^8.2.5",
"css-loader": "^6.7.1",
"html-webpack-plugin": "^5.5.0",
"vue-loader": "^17.0.0",
"vue-style-loader": "^4.1.3",
"vue-template-compiler": "^2.7.10",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.0",
"webpack-merge": "^5.8.0"
}
}
webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
// const VueLoaderPlugin = require('vue-loader/lib/plugin');
const { VueLoaderPlugin } = require('vue-loader');
module.exports = {
entry: './src/main.js',
output: {
publicPath: "/"
},
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
allowedHosts: 'all',
compress: true,
https: true,
historyApiFallback: true
},
module: {
rules: [
{ test: /\.js$/, use: 'babel-loader' },
{ test: /\.vue$/, use: 'vue-loader' },
{ test: /\.css$/, use: ['vue-style-loader', 'css-loader']},
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
}),
new VueLoaderPlugin(),
]
};
I tried modifying my routes, installing new packages/libraries, messing with my webpack config file, copying over loads of code from online, but alas, nothing worked.

can not resolve Uncaught TypeError: vue__WEBPACK_IMPORTED_MODULE_0__.default is undefined

I am working on vue js 2 version and I needed to download font-awsome library for styling purpose. However, when I downloaded the files it seemed not compatible with my project so I deleted/uninstalled them (the font-awsome libraries), and then all of a sudden when I tried to refresh the server I got this error in the console :
Uncaught TypeError: vue__WEBPACK_IMPORTED_MODULE_0__.default is
undefined
Note that the code was working fine before installing and uninstalling the the font-awsome libraries, I don't really know what happened I also did try to ctrl + z to restore the main.js but is still showing the same error.
1- main.js:
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import VueFirestore from 'vue-firestore'
import JQuery from 'jquery'
import {BootstrapVue, IconsPlugin} from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import 'bootstrap/dist/js/bootstrap.min.js'
import './assets/app.scss'
import swal from 'sweetalert2';
// import VoerroTagsInput from '#voerro/vue-tagsinput';
Vue.use(BootstrapVue)
Vue.use(IconsPlugin)
window.$ = JQuery
Vue.use(VueFirestore, {
key: 'id', // the name of the property. Default is '.key'.
enumerable: true // whether it is enumerable or not. Default is true.
})
Vue.use(VueFirestore)
Vue.config.productionTip = false
// Make BootstrapVue available throughout your project
// Vue.use(createPopper)
let app = '';
if(!app){
new Vue({
router,
render: h => h(App)
}).$mount("#app");
}
window.Swal = swal;
const Toast = Swal.mixin({
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 3000
});
window.Toast = Toast;
2- index.js:
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'books',
component: () => import('../components/books')
},
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
3- package.json:
{
"name": "vue-firebase-crud",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"bootstrap": "^4.5.3",
"bootstrap-vue": "^2.22.0",
"core-js": "^3.8.3",
"firebase": "^8.10.0",
"jquery": "^3.6.1",
"sweetalert2": "^11.4.37",
"vue-firestore": "^0.3.30",
"vue-router": "^3.6.5",
"vue2-editor": "^2.10.3"
},
"devDependencies": {
"#babel/core": "^7.12.16",
"#babel/eslint-parser": "^7.12.16",
"#vue/cli-plugin-babel": "~5.0.0",
"#vue/cli-plugin-eslint": "~5.0.0",
"#vue/cli-service": "~5.0.0",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3",
"node-sass": "^7.0.3",
"sass": "^1.55.0",
"sass-loader": "^13.1.0",
"vue-template-compiler": "^2.6.14"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "#babel/eslint-parser"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
4- error in console:

[Vue warn]: Failed to resolve component: router-view

I'm a newbie to Vue.js and first time dealing with vue-router.
Just created a few files and for some reason I don't get to see the template. Compiled successfully but in the browser I get the following error: Failed to resolve component: router-view
main.js
import { createApp } from 'vue';
import VueRouter from 'vue-router';
import { store } from './store';
import App from './App.vue';
import AuthHandler from './components/AuthHandler';
const router = new VueRouter({
mode: 'history',
routes: [
{ path: '/oauth2/callback', component: AuthHandler }
]
});
const app = createApp(App)
app.use(router)
app.use(store)
app.mount('#app')
App.vue
<template>
<div>
<AppHeader></AppHeader>
<router-view></router-view>
</div>
</template>
<script>
import AppHeader from "./components/AppHeader";
export default {
name: "App",
components: {
AppHeader
}
};
</script>
components/AuthHandler.vue
<template>
<div>
... Please wait
</div>
</template>
<script>
export default {
name: "AuthHandler"
};
</script>
Here is the package.json
package.json
{
"name": "images",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"axios": "^0.21.1",
"core-js": "^3.6.5",
"lodash": "^4.17.21",
"qs": "^6.10.1",
"vue": "^3.0.0",
"vue-router": "^3.5.2",
"vuex": "^4.0.2"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~4.5.0",
"#vue/cli-plugin-eslint": "~4.5.0",
"#vue/cli-service": "~4.5.0",
"#vue/compiler-sfc": "^3.0.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^7.0.0"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/vue3-essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
I resolved the problem in the following way. I believe the issue relay on the vue/vue-router versions. I hope this is the correct way :-)
main.js
import { createApp } from 'vue';
import App from './App.vue';
// import VueRouter from './vue-router';
import { createWebHistory, createRouter } from "vue-router";
import { store } from './store';
import AuthHandler from './components/AuthHandler';
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: '/about', component: AuthHandler }
]
});
const app = createApp(App)
app.use(router)
app.use(store)
app.mount('#app')
I was an idiot, I wanted to use 'vue add vue-router'. Of course it didn't work.
Vue add router
is the command.
npm install vue-router#4
only installs the package, won't add it to the app. If all is well, main.js should look like this:
import App from "./App.vue";
import router from "./router";
createApp(App).use(router).mount("#app");
Unfortunately they forgot to add it to the documentation...

vue js - Cannot read property 'use' of undefined

I'm using vue js 3, but im taking this error. I try to use router.
my main.js file is in here:
import { createApp } from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router' // i added this
import Vue from 'vue'; // and this
Vue.use(VueRouter); // and this
createApp(App).mount('#app')
my package.json file is in here:
{
"name": "router",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"core-js": "^3.6.5",
"vue": "^3.0.0",
"vue-router": "^3.5.1"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~4.5.0",
"#vue/cli-plugin-eslint": "~4.5.0",
"#vue/cli-service": "~4.5.0",
"#vue/compiler-sfc": "^3.0.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^7.0.0"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/vue3-essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
Where am i wrong? The error message is:
Uncaught TypeError: Cannot read property 'use' of undefined
If you help me i will be glad.
First you've to follow my answer here to install the correct version of vue router,
then you should use the correct syntax of vue-router 4 which is compatible with vue 3 :
import { createApp } from 'vue'
import App from './App.vue'
import {createRouter, createWebHistory} from 'vue-router'
const routes = [
{
path: '/',
name: 'Home',
component: Home,//shsould be imported
},
]
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
})
createApp(App).use(router).mount('#app')

"SyntaxError: Cannot use import statement outside a module" error while testing React Native project with Jest and #testing-library/react-native?

Error I'm getting Anytime I run npm test:
FAIL ./App.test.js
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/Users/bestes/Desktop/react-native-training/node_modules/react-native-status-bar-height/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { Dimensions, Platform, StatusBar } from 'react-native';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (node_modules/react-native-elements/src/config/index.js:1:1)
My Test:
import 'react-native';
import React from 'react';
import { render } from '#/../testing/test-utils'
import App from './App'
test('should render app component', () => {
const result = render(<App />)
expect(result).toMatchSnapshot()
})
My test-utils.js file:
import React from 'react'
import { render } from '#testing-library/react-native'
import { store } from '#/bootstrap/redux'
import { Provider } from 'react-redux'
const AllTheProviders = ({ children }) => {
return (
<Provider store={store}>
{children}
</Provider>
)
}
const customRender = (ui, options) =>
render(ui, { wrapper: AllTheProviders, ...options })
// re-export everything
export * from '#testing-library/react-native'
// override render method
export { customRender as render }
My package.json file:
{
"name": "ReactNativeTraining",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"#react-native-community/geolocation": "2.0.2",
"#react-native-community/masked-view": "0.1.10",
"#react-native-picker/picker": "1.9.10",
"#reduxjs/toolkit": "1.5.0",
"axios": "0.21.1",
"dayjs": "1.10.4",
"lodash": "4.17.20",
"react": "16.13.1",
"react-native": "0.63.2",
"react-native-config": "1.4.2",
"react-native-elements": "3.0.0-alpha.1",
"react-native-geocoding": "0.5.0",
"react-native-gesture-handler": "1.9.0",
"react-native-permissions": "3.0.0",
"react-native-picker-select": "8.0.4",
"react-native-reanimated": "1.13.2",
"react-native-safe-area-context": "3.1.9",
"react-native-screens": "2.17.0",
"react-native-size-matters": "0.4.0",
"react-native-vector-icons": "8.0.0",
"react-navigation": "4.0.2",
"react-navigation-stack": "1.5.4",
"react-navigation-tabs": "2.4.1",
"react-redux": "7.2.2"
},
"devDependencies": {
"#babel/core": "7.12.10",
"#babel/runtime": "7.12.5",
"#react-native-community/eslint-config": "2.0.0",
"#testing-library/jest-native": "4.0.1",
"#testing-library/react-native": "7.2.0",
"babel-jest": "^26.6.3",
"babel-plugin-module-resolver": "4.0.0",
"eslint": "7.18.0",
"jest": "26.6.3",
"metro-react-native-babel-preset": "0.64.0",
"react-test-renderer": "16.13.1"
},
"type": "module",
"jest": {
"verbose": true,
"preset": "react-native",
"setupFilesAfterEnv": ["#testing-library/jest-native/extend-expect"],
"transformIgnorePatterns": [
"node_modules/(?!(react-native",
"#react-native-community/geolocation",
"#react-native-community/masked-view",
"|#react-native-picker/picker",
"|#reduxjs/toolkit",
"|axios",
"|dayjs",
"|lodash",
"|react",
"|react-native",
"|react-native-config",
"|react-native-elements",
"|react-native-geocoding",
"|react-native-gesture-handler",
"|react-native-permissions",
"|react-native-picker-select",
"|react-native-reanimated",
"|react-native-safe-area-context",
"|react-native-screens",
"|react-native-size-matters",
"|react-native-vector-icons",
"|react-navigation",
"|react-navigation-stack",
"|react-navigation-tabs",
"|react-redux",
")/)"
]
}
}
My babel.config.js file:
module.exports = function (api) {
api.cache(true)
const presets = [
'module:metro-react-native-babel-preset'
]
const plugins = [
[
'module-resolver', {
'root': ['./src/'],
'extensions': ['.js', '.ios.js', '.android.js'],
'alias': {
'#': './src/'
}
}
]
]
return {
presets,
plugins
}
}
What I've tried:
Adding "type": "module" to my root level package.json file, which fixed a similar error with exporting,
Adding Transform Ignore Patterns to the Jest key in the package.json file.
I'd added ALL of my dependencies to the transformIgnorePatterns as it kept throwing errors on each one, until I added all and now it throws on react-native modules imports.
[Solved] Work for me
Install below
npm install --save-dev #babel/core
npm install --save-dev #babel/preset-env
after that create the "babel.config.js" file in the root and that content should be as below
module.exports = {
presets: ['#babel/preset-env', '#babel/preset-react'],
env: {
test: {
plugins: ["#babel/plugin-transform-runtime"]
}
}
};