How to solve ‘ReferenceError: window is not defined’ when importing Vue plugin in Nuxt.JS

I need to use Vue plugin named vue-flash-message in Nuxt.JS. I installed it via NPM and created file vue-flash-message.js in folder plugins.

import Vue from 'vue';
import VueFlashMessage from 'vue-flash-message';


Then I tried to run `npm run dev` to test whether the plugin works. But it doesn’t work.

The reason is the default mode of Nuxt is SSR. The variable window is only available in the browser, not on the server.

And I found the solution, I just change the file content like below:

import Vue from 'vue';

if (process.browser) {
window.onNuxtReady(() => {
const VueFlashMessage = require('vue-flash-message');

After that, it should work fine. You also apply the idea for other Vue plugins.