PrimeVue Toast Component Error: Can't resolve './ToastMessage' in */toast - vue.js

I'm currently starting a new project with vuejs and wanted to use primevue for some components.
My knowledge with VueJS in general is not the best, because I'm just getting started with it.
My application has a webpack based build with vue-loader configured, so thats how primevue is getting installed.
I was trying to use the Toast-Component, but when importing Toast from 'primevue/toastservice' webpack throws these two errors:
ERROR in ./node_modules/primevue/components/toast/Toast.vue?vue&type=script&lang=js& (./node_modules/vue-loader/lib??vue-loader-options!./node_modules/primevue/components/toast/Toast.vue?vue&type=script&lang=js&)
Module not found: Error: Can't resolve './ToastMessage' in '*/node_modules/primevue/components/toast'
# ./node_modules/primevue/components/toast/Toast.vue?vue&type=script&lang=js& (./node_modules/vue-loader/lib??vue-loader-options!./node_modules/primevue/components/toast/Toast.vue?vue&type=script&lang=js&) 11:0-42 86:24-36
# ./node_modules/primevue/components/toast/Toast.vue?vue&type=script&lang=js&
# ./node_modules/primevue/components/toast/Toast.vue
# ./node_modules/primevue/toast.js
# ./src/main.js
and
ERROR in ./node_modules/primevue/components/toast/Toast.vue?vue&type=style&index=0&lang=css& (./node_modules/vue-loader/lib??vue-loader-options!./node_modules/primevue/components/toast/Toast.vue?vue&type=style&index=0&lang=css&) 97:0
Module parse failed: Unexpected token (97:0)
File was processed with these loaders:
*./node_modules/vue-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|
|
-> .p-toast {
| position: fixed;
| width: 20em;
# ./node_modules/primevue/components/toast/Toast.vue?vue&type=style&index=0&lang=css& 1:0-118 1:134-137 1:139-254 1:139-254
# ./node_modules/primevue/components/toast/Toast.vue
# ./node_modules/primevue/toast.js
# ./src/main.js
I import the Component as following:
import Vue from "vue";
import App from "./App/App.vue";
import Toast from 'primevue/toast';
import ToastService from 'primevue/toastservice';
Vue.use(ToastService);
Vue.component('Toast', Toast);
new Vue({
render: h => h(App)
}).$mount("#app");
I already tried importing/using the 'Toast' component in my App.js or in other files where i mainly wanna use those Toasts without success.
If I leave out the Toast all works fine, so the ToastService seems alright.
So if theres anybody who uses primevue and also ran into this problem and found a solution I thank you all in advance.

npm install mitt
solves the first issue. It is included in devDependencies of the primevue package, but for some reasons its not installed.

I have a solution for your second error, but not sure how to fix your first "Can't resolve" error, as I am still figuring that one out.
What does your webpack config look like? Do you specify a rule for CSS files? If the component uses a <style> block you need to tell webpack how to handle this.
Specify a rule for .css files, and it will also apply the same to <style> blocks in .vue files. Without this, webpack does not know how to parse the blocks.
So make sure your webpack.config.js rules section has the following CSS part in it (or something similar):
rules: [
{
test: /\.vue$/,
use: 'vue-loader'
},
// the below will apply to both plain `.css` files AND `<style>` blocks in `.vue` files
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
]
}
]
Also make sure to install the relevant tools in your package.json.
I solved something similar, so see my question and answer for more detailed info.

Related

SyntaxError: Cannot use import statement outside a module { AXIOS }

Hello I changed my code from fetch to axios and when I run my tests I get this problem... Can anyone help me with that ?
SyntaxError: Cannot use import statement outside a module
> 1 | import axios from "axios";
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:
This issue occurred after axios update from 0.27.2 to 1.0.0. There is an opened issue, so probably this is going to be fixed soon. Meanwhile they're working on that, you can temporary fix it by setting up transformIgnorePatterns in jest config:
transformIgnorePatterns = ["node_modules/(?!axios)/"]
For me helped the following jest.config.js setup:
transformIgnorePattern: [
'<rootDir>/node_modules/(?!axios)/'
]
alongside with
moduleNameMapper: {
'^axios$': require.resolve('axios'),
}

Jest transformIgnorePatterns can't transpile vee-validate with pnpm

I have a Nuxt app that runs with pnpm and I'd like to test it using Jest and the vee-validate library.
Whenever I launch my tests, I get this error:
FAIL test/integration/components/ui/RegistrationForm.test.ts
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• 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/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/my-project/node_modules/.pnpm/vee-validate#3.4.14_vue#2.6.14/node_modules/vee-validate/dist/rules.js:889
export { alpha, alpha_dash, alpha_num, alpha_spaces, between, confirmed, digits, dimensions, double, email, excluded, ext, image, integer, is, is_not, length, max, max_value, mimes, min, min_value, numeric, oneOf, regex, required, required_if, size };
^^^^^^
SyntaxError: Unexpected token 'export'
34 | } from '#nuxtjs/composition-api';
35 | import { extend, ValidationProvider } from 'vee-validate';
> 36 | import {
| ^
37 | min_value as minValue,
38 | max_value as maxValue,
39 | } from 'vee-validate/dist/rules';
at Runtime.createScriptFromCode (node_modules/.pnpm/jest-runtime#28.1.1/node_modules/jest-runtime/build/index.js:1796:14)
at Object.<anonymous> (components/form/fields/CalendarField.vue:36:1)
at Object.<anonymous> (components/form/fields/FormField.vue:14:1)
I tried to add the option transformIgnorePattern inside jest.config.js but it's not working at all.
This is what I tried:
transformIgnorePatterns: ['/node_modules/(?!vee-validate/dist/rules)']
transformIgnorePatterns: ['/node_modules/(?!(.*vee-validate/dist/rules))/']
The only option that works for me is to import the UMD build but I lose all the benefit of tree-shaking.
Sources
https://github.com/logaretm/vee-validate/issues/2310
https://github.com/facebook/jest/issues/2081#issuecomment-699558143
https://qiita.com/tamonmon/items/6392c1680ef498a8c816
I ran into the same problem and was able to fix it by adjusting my jest.config.js file. I did three things there:
add "vee-validate/dist/rules": "babel-jest", to transform: {}
add the file path to transformIgnorePatterns like this: transformIgnorePatterns: ["/node_modules/", "/components/views/*", "node_modules/vee-validate/dist/rules"],
add isolatedModules to true in ts-jest like: globals: {
"ts-jest": {
isolatedModules: true,
babelConfig: true,
},
},
Since this response is late to for the op, I hope this helps atleast someone :)

Problem using jest with storybook snapshot testing in react native

What I have
I'm trying to use jest and Storybook with Storyshots addon for snapshot test in my react-native/typescript app, but I'm having some issues when I try to run simple test.
According to Snapshot testing section in storybook documentation, the only thing you need to do is create an storybook.test.js file with the following:
storybook.test.js
import initStoryshots from '#storybook/addon-storyshots';
initStoryshots();
After this, everything is supposed to work as expected, but the console throws the following error:
● 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, ign
oring "node_modules".
Here's what you can do:
• If you are trying to use ECMAScript Mo
dules, see https://jestjs.io/docs/en/ecmascri
pt-modules for how to enable it.
• To have some of your "node_modules" fi
les transformed, you can specify a custom "tr
ansformIgnorePatterns" in your config.
• If you need a custom transformation sp
ecify a "transform" option in your config.
• If you simply want to mock your non-JS
modules (e.g. binary assets) you can stub th
em out with the "moduleNameMapper" config opt
ion.
You'll find more details and examples of
these config options in the docs:
https://jestjs.io/docs/en/configuration.h
tml
Details:
C:\Users\myuser\Desktop\myapp\node_m
odules\react-native-swipe-gestures\index.js:3
import React, { Component } from "react";
^^^^^^
SyntaxError: Cannot use import statement
outside a module
at Runtime.createScriptFromCode (node_m
odules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (node_modules/#st
orybook/react-native/dist/preview/components/
OnDeviceUI/navigation/index.js:29:53)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 11.468 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
jest.config.js
module.exports = {
preset: 'react-native',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
};
babel.config.js
module.exports = {
presets: ['module:metro-react-native-babel-preset']
};
If I remove #storybook/addon-storyshots relate things from my tests, everything is working normally without any problem. So I don't know if I need some extra configuration to make work storybook/jest snapshoot testing with react native.
It's enough. Add react-native-swipe-gestures to transformIgnorePatterns of your jest.config.js.
transformIgnorePatterns: ['node_modules/(?!(react-native|#react-navigation|#react-native|react-native-swipe-gestures)/)'],
This did a trick for me.

Bundling a plugin with Rollup but having duplicate Vue.js package imported in the client app's bundle (Nuxt)

Dear Stack Overflow / Vue.js / Rollup community
This could be a noob question for the master plugin developers working with Vue and Rollup. I will write the question very explicitly hoping that it could help other noobs like me in the future.
I have simple plugin that helps with form validation. One of the components in this plugin imports Vue in order to programatically create a component and append to DOM on mount like below:
import Vue from 'vue'
import Notification from './Notification.vue' /* a very simple Vue component */
...
mounted() {
const NotificationClass = Vue.extend(Notification)
const notificationInstance = new NotificationClass({ propsData: { name: 'ABC' } })
notificationInstance.$mount('#something')
}
This works as expected, and this plugin is bundled using Rollup with a config like this:
import vue from 'rollup-plugin-vue'
import babel from 'rollup-plugin-babel'
import { terser } from 'rollup-plugin-terser'
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
export default {
input: 'src/index.js',
output: {
name: 'forms',
globals: {
vue: 'Vue'
}
},
plugins: [
vue(),
babel(),
resolve(),
commonjs(),
terser()
],
external: ['vue']
}
As you can see, Vue.js is getting externalised in this bundle. The aim (and the assumption) is that the client app that imports this plugin will be running on Vue, therefore there's no need to bundle it here (assumption).
The very simple src/index.js that the bundler uses is below:
import Form from './Form.vue'
export default {
install(Vue, _) {
Vue.component('bs-form', Form)
}
}
Rollup creates 2 files (one esm and one umd) and references them in in the plugins package.json file like below:
"name": "bs-forms",
"main": "./dist/umd.js",
"module": "./dist/esm.js",
"files": [
"dist/*"
],
"scripts": {
"build": "npm run build:umd & npm run build:es",
"build:es": "rollup --config rollup.config.js --format es --file dist/esm.js",
"build:umd": "rollup --config rollup.config.js --format umd --file dist/umd.js"
}
Everything works as expected up to this point and the bundles are generated nicely.
The client app (Nuxt SSR) imports this plugin (using npm-link since it's in development) with a very simple import in a plugin file:
/* main.js*/
import Vue from 'vue'
import bsForms from 'bs-forms'
Vue.use(bsForms)
This plugin file (main.js) is added to nuxt.config.js as a plugin:
// Nuxt Plugins
...
plugins: [{src: '~/plugins/main'}]
...
Everything still works as expected but here comes the problem:
Since the clients is a Nuxt app, the Vue is imported by default of course but the externalised Vue module (by the forms plugin) is also imported in the client. Therefore there is a duplication of this package in the client bundle.
I guess the client app can configure its webpack config in order to remove this duplicated module. Perhaps by using something like a Dedupe plugin or something? Can someone suggests how to best handle situation like these?
But what I really want to learn, is the best practice of bundling the plugin at the first place, so that the client doesn't have to change anything in its config and simply imports this plugin and move on.
I know that importing the Vue.js in the plugin may not be a great thing to do at the first place. But there could be other reasons for an import like this as well, for example imagine that the plugin could be written in Typescript and Vue.js / Typescript is written by using Vue.extend statements (see below) which also imports Vue (in order to enable type interface):
import Vue from 'vue'
const Component = Vue.extend({
// type inference enabled
})
So here's the long question. Please masters of Rollup, help me and the community out by suggesting best practice approaches (or your approaches) to handle situations like these.
Thank you!!!!
I had the same problem and I found this answer of #vatson very helpful
Your problem is the combination of "npm link", the nature of nodejs module loading and the vue intolerance to multiple instances from different places.
Short introduction how import in nodejs works. If your script has some kind of library import, then nodejs initially looks in the local node_modules folder, if local node_modules doesn't contain required dependency then nodejs goes to the folder above to find node_modules and your imported dependency there.
You do not need to publish your package on NPM. It is enough if you generate your package locally using npm pack and then install it in your other project npm install /absolute_path_to_your_local_package/your_package_name.tgz. If you update something in your package, you can reinstall it in your other project and everything should work.
Here is the source about the difference between npm pack and npm link https://stackoverflow.com/a/50689049/6072503.
I have sorted this problem with an interesting caveat:
The duplicate Vue package doesn't get imported when the plugin is used via an NPM package (installed by npm install -save <plugin-name> )
However, during development, if you use the package vie npm link (like npm link <plugin-name>) then Vue gets imported twice, like shown in that image in the original question.
People who encounter similar problems in the future, please try to publish and import your package and see if it makes any difference.
Thank you!

how to import bootstrap.bundle.min.js in vue project with webpack

I got a job to migrate bootstrap project to vue project.
I got bootstrap.bundle.min.js( v4.1.3) and the css file and a bunch of third or self-defined js lib.
I have done following work to import these files.
I use below to import css files.
//App.vue
<style src="xxx.css"></style>
And in main.js I want to import jquery and bootstrap.bundle.min.js globally.
//main.js
import 'jquery'
import 'popper.js'
import 'src/bootstrap/bootstrap.bundle.min.js'
I add webpack plugin to solve '$ is not defined' (and I don't know why this emmited in runtime rather compile time)
configureWebpack: {
plugins: [
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
})
],
I have changed html mark to vue component.
After I've done all above, I still got the error
Uncaught TypeError: Cannot set property 'bootstrap' of undefined
at eval (bootstrap.bundle.min.js?6518:7)
I appreciate your help for why this happen and how to solve it.
The way that you are using webpack.ProvidePlugin in your plugins array is called Shimming. Here is the doc. I think it's a bad practice to use such a technique when you have a powerful tool like Webpack. You don't have the $ available because you need to import it like this: import $ from 'jquery'. So make sure you have installed all your dependencies and import them in your modules.
I hope it'll help. If not just reply here and we will talk more. good luck!