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

NuxtJS

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';

Vue.use(VueFlashMessage);

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');
Vue.use(VueFlashMessage);
})
}

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

Fix error when install npm package as global package

NodeJS

Today, I will guide you to fix error when install npm package as global package.
I ran below command to install npm package as global package but installation was failed.
Command: sudo npm install -g electron
Error will look like:
Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/electron/.electron'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] postinstall: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/ubuntu/.npm/_logs/2018-03-18T10_25_47_793Z-debug.log

And this option saved my time:
--unsafe-perm=true --allow-root

I hope this post is helpful.

Keep database connection on connecting to MySQL/MariaDB using SQLAlchemy

Python 3

You want the database connection will be keep on connect to MySQL/MariaDB, just use pool_recycle parameter in method create_engine().
Example:
create_engine(db_url, pool_recycle=1)

pool_recycle

this setting causes the pool to recycle connections after the given number of seconds has passed. It defaults to -1, or no timeout. For example, setting to 3600 means connections will be recycled after one hour. Note that MySQL in particular will disconnect automatically if no activity is detected on a connection for eight hours (although this is configurable with the MySQLDB connection itself and the server configuration as well).

End.

%d bloggers like this: