Use a library as a plugin

Hello, I'm trying to setup driver.js 1.x in a Nuxt v3 project Here's what I've done: `npm install driver.js`` Then I've created a plugin under plugins/driver.js with :
import { Driver } from 'driver.js';
import 'driver.js/dist/driver.css'

export default defineNuxtPlugin((nuxtApp) => {
const driver = new Driver()

nuxtApp.provide('driver', driver)
});
import { Driver } from 'driver.js';
import 'driver.js/dist/driver.css'

export default defineNuxtPlugin((nuxtApp) => {
const driver = new Driver()

nuxtApp.provide('driver', driver)
});
` Then i've registering this plugin in nuxt.config.js:
const isDevelopment = process.env.NODE_ENV === 'development';

export default defineNuxtConfig({
srcDir: "src",
pages: true,
ssr: true,
devtools: {
enabled: true,
timeline: {
enabled: true,
},
telemetry: false
},
runtimeConfig: {
public: {
domain: process.env.NUXT_PUBLIC_DOMAIN_NAME,
http: process.env.NUXT_PUBLIC_HTTP,
}
},
build: {
transpile: ['driver.js'],
},
plugins: [
{ src: '~/plugins/driver.js', mode: 'client' }
],
})
const isDevelopment = process.env.NODE_ENV === 'development';

export default defineNuxtConfig({
srcDir: "src",
pages: true,
ssr: true,
devtools: {
enabled: true,
timeline: {
enabled: true,
},
telemetry: false
},
runtimeConfig: {
public: {
domain: process.env.NUXT_PUBLIC_DOMAIN_NAME,
http: process.env.NUXT_PUBLIC_HTTP,
}
},
build: {
transpile: ['driver.js'],
},
plugins: [
{ src: '~/plugins/driver.js', mode: 'client' }
],
})
And i got this error (cf capture): Do you think i have to import it as a plugin ? Or can i use it directly in the components ?
No description
3 Replies
kapa.ai
kapa.ai3w ago
Hi @BobLamarley. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources and your question, I can offer some guidance on setting up driver.js as a plugin in your Nuxt 3 project. First, it's important to note that in Nuxt 3, you don't need to explicitly register plugins in the nuxt.config.js file if they are located in the plugins/ directory. Nuxt 3 automatically scans and registers plugins from this directory as mentioned in the Nuxt documentation (https://nuxt.com/docs/guide/directory-structure/plugins#registered-plugins). Here's how you can set up driver.js as a plugin in Nuxt 3: 1. Create a plugin file: Create a file named driver.client.ts (or .js if you're not using TypeScript) in the plugins/ directory. The .client suffix ensures that this plugin only runs on the client-side. 2. Implement the plugin:
import { Driver } from 'driver.js'
import 'driver.js/dist/driver.css'

export default defineNuxtPlugin((nuxtApp) => {
const driver = new Driver()

return {
provide: {
driver
}
}
})

import { Driver } from 'driver.js'
import 'driver.js/dist/driver.css'

export default defineNuxtPlugin((nuxtApp) => {
const driver = new Driver()

return {
provide: {
driver
}
}
})

This setup uses the defineNuxtPlugin helper function, which is the recommended way to create plugins in Nuxt 3 as shown in the Nuxt documentation (https://nuxt.com/docs/guide/directory-structure/plugins#using-composables). 3. Remove the plugin registration from nuxt.config.ts: You can remove the plugins array from your Nuxt configuration since the plugin will be auto-registered. 4. Keep the build.transpile configuration:
export default defineNuxtConfig({
// ... other configurations
build: {
transpile: ['driver.js'],
},
// ... other configurations
})

export default defineNuxtConfig({
// ... other configurations
build: {
transpile: ['driver.js'],
},
// ... other configurations
})

This step is necessary to ensure that driver.js is properly transpiled during the build process. With this setup, you should be able to use driver.js in your components. You can access it using the useNuxtApp() composable:
<script setup>
const { $driver } = useNuxtApp()

// Use $driver here
</script>
<script setup>
const { $driver } = useNuxtApp()

// Use $driver here
</script>
The error you're seeing might be related to how the plugin is being loaded or used. Make sure that you're only using driver.js on the client-side, as it requires access to the DOM. If you still encounter issues, it might be helpful to check the Nuxt DevTools or browser console for more detailed error messages. Also, ensure that you're using a version of driver.js that is compatible with Nuxt 3 and Vue 3. Remember, you don't need to import driver.js directly in your components if you set it up as a plugin. The plugin makes it available throughout your Nuxt application. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@314138038995255298> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
BobLamarley
BobLamarleyOP2w ago
No one has an idea ?
Want results from more Discord servers?
Add your server