I have to create an package of vue component and then install it in another project.
My code sample is given here. After using npm run build command I got a converted js file.
I install it in another project. And in my project in main.js, I am using below line to add this dependency.
import 'test'
but in my browser I am getting this error.
[Vue warn]: Unknown custom element: - did you register the
component correctly? For recursive components, make sure to provide
the "name" option.
(found in )
I dont understand what I am missing for adding funnel component to my project.
You need to also declare in the components option.
import test from 'test
export default {
components: {
test
}
}
Related
I'm working on a simple Vue component in a codesandbox. The codesandbox picks up the test.js file that I've created but running the test returns the following error:
There was an error while evaluating the file: Vue is not defined
https://codesandbox.io/s/nice-firefly-wls69e?file=/src/components/__tests__/MatchbackCheckbox.test.js
It's about the most simple test possible. Any idea what I might be missing here? Is there an additional dependency that I need to add to the codesandbox?
I've written a Vue JS component/plugin and have it in the following formats:
Common
ESM
UMD
I'm trying to import the UMD version into my Nuxt JS project (I'e also tried the others) and use it.
I've added a scripts array to my nuxt.config.js file to the head section:
scripts: [
{ src: '/js/vue-les-offers.umd.js' }
]
My JS file lives in static/js, and I've gone ahead and included my component in my page:
...
<vue-les-offers></vue-les-offers>
...
But I'm getting an error thrown in my console:
[Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.
What am I missing?
I've tried:
installing via the plugins array
installing via the modules (used the ESM variant)
tried wrapping in a client-only element
tried both min and non-minified JS variants
I'm trying to migrate from vue-cli to Vite using Vue 2.0.
I've some JavaScript-generated files for GRPC communication; alongside each file, there is a declarative file because I'm using Vue with TypeScript. When running Vite, I get this error:
Uncaught SyntaxError: The requested module '/src/proto/admin_config_grpc_web_pb.js' does not provide an export named 'AdminConfigurationServicePromiseClient'
However, I've a corresponding declaration file which contains this line:
export class AdminConfigurationServiceClient {
Anybody has encountered this issue and has a solution?
Thanks
This error is similar to the vite issue https://github.com/vitejs/vite/issues/2117.
Do not re-export typescript type or interface in vite. You can just export it in file A and import it in file B. Don't try to export it in file B again
BTW,
https://github.com/originjs/webpack-to-vite
This is a github project that I found when I searched for error messages when I was converting an old project. It lists some conversion items and error repair methods. It can even convert an old project to a vite project with one click. It’s great, I recommend it!
A workaround can be if you declare a new interface that inherits from the one that you want to re-export.
a.vue
export interface AItem extends ItemModel {}
b.vue
export interface A2Item extends ItemModel {}
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!
I'm working on a project where I use electron-vue and to make the app look better I use bootstrap-vue. After a lot of debugging, I have found that changing a data property(in the parent component) that is linked to bootstrap components props. It will give me error messages telling me not to mutate props values, and that they are read-only. As it seems for me, the code works and executes, but will give me a lot of errors in the console. When I say it seems to work, what I mean is that both console.log and visually on bootstrap component it seems to change the variables correctly.
After writing a lot of test cases I have found out that changing a data property does not give an error. But when changing a data property that is linked to a bootstrap component prop it will.
A test case that shows where these error messages show up is in the code below:
<template>
<b-progress :max="maxNumberOfFiles" show-value>
<b-progress-bar :value="currentNumberOfErrorFiles"
:max="maxNumberOfFiles"
variant="danger"
show-value
/>
</b-progress>
</template>
export default {
data() {
maxNumberOfFiles: 1,
currentNumberOfErrorFiles: 0
},
methods {
test: function() {
currentNumberOfErrorFiles = 1;
}
}
}
The code above will result in 3 errors:
$attrs is readonly
$listeners is readonly
Avoid mutating a prop directly since the value will be overwritten whenever
the parent component re-renders. Instead, use a data or computed property
based on the prop's value. Prop being mutated: "value"
But this code produces zero errors:
<template>
<progress :value="currentNumberOfErrorFiles"
:max="maxNumberOfFiles"
>
</progress>
</template>
export default {
data() {
maxNumberOfFiles: 1,
currentNumberOfErrorFiles: 0
},
methods {
test: function() {
currentNumberOfErrorFiles = 1;
}
}
}
I have tried to use google for similar problems and look at the doc for both electron-vue and bootstrap-vue, and can't find anything that helped me. Is there anyone that have run into the same problem or have a solution on how to get rid of those errors?
So after a lot of headaches, I finally found a way to avoid all these warnings and errors. When I initialized the project I used these commands:
$ npm install vue-cli -g
$ vue init simulatedgreg/electron-vue <<project-name>>
After a suggestion on reinitializing the project using the vue-cli and add the plugin for electron after (This person created a quick project and had no problems). So when initializing the project again I used these commands:
npm install vue-clie -g
vue create <<project-name>>
cd <<project-name>>
vue add electron-builder
npm install bootstrap-vue
npm install
If I remeber correctly those were all the npm install commands you needed, but if you get an error of a package missing, just use npm install <> to install it.
Now I had to move every .vue file over to the new project and check that all the import statements are correct, and import and use bootstrap again in the index.js.
If you use vue-router, vuex or vuex-electron these also need to be moved over and installed again. This should just be to move the files over to the new project and check where they were imported in the old files and copy that over.
For me, it seemed that the vue init command did something that the bootstrap-vue package doesn't like. I didn't have a very large project, so the whole process took about 15-20 min.
To run an auto update dev server use the command npm run electron:serve and the command npm run electron:build to build the project. These commands can be changed in the package.json file.
The folder structure is a bit different, there will no longer be a renderer and main folder. Everything will be in the src folder. The main.js from the main folder is now named background.js. Apart from that, I think it is similar enough to figure out by just looking through the files.