Search code examples
node.jsbasharmknex.jsnode-sqlite3

Knex: How to fix "Cannot read property 'prototype' of undefined" on ARM for initial-setup


I am trying to initialize a sqlite3 database with knex on an ARM-Device, but getting the error:

Knex: run

$ npm install sqlite3 --save
TypeError: Cannot read property 'prototype' of undefined
    at inherits (/home/user/node_modules/sqlite3/lib/sqlite3.js:27:16)
    at Object.<anonymous> (/home/user/node_modules/sqlite3/lib/sqlite3.js:66:1)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Client_SQLite3._driver (/home/user/sWave-Gateway/node_modules/knex/lib/dialects/sqlite3/index.js:79:12)
    at Client_SQLite3.initializeDriver (/home/user/sWave-Gateway/node_modules/knex/lib/client.js:254:26)
    at Client_SQLite3.Client (/home/user/sWave-Gateway/node_modules/knex/lib/client.js:115:10)
    at new Client_SQLite3 (/home/user/sWave-Gateway/node_modules/knex/lib/dialects/sqlite3/index.js:62:20)
    at Knex (/home/user/node_modules/knex/lib/index.js:60:34)
    at Object.<anonymous> (/home/user/dist/db/knex-data-access-layer/index.js:28:28)
    at Module._compile (module.js:653:30)

I already tried to set the NODE_ENV in different ways set the rights of the files with chmod to 777 but nothing worked so far. I am kind of despaired because i have not changed anything on this part for a long time and it suddenly stopped working.

The Command i use:

NODE_ENV=production node dist/initial-setup.js

It executes the following code:

import * as config from 'config';
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as mkdirp from 'mkdirp';
import * as path from 'path';
import { boot } from './boot';
import * as constants from './constants';
import { dataAccessLayer } from './db';
import * as shell from 'shelljs';
// tslint:disable:no-console

boot();

let logPath: string = config.get(constants.CONFIG_LOG_DIR);
if (!fs.existsSync(logPath)) {
  console.log(`Creating logs directory at ${logPath} ...`);
  mkdirp.sync(logPath);
}

let secretDirPath: string = config.get(constants.CONFIG_SECRET_DIR);
if (!fs.existsSync(secretDirPath)) {
  console.log(`Creating secret directory at ${secretDirPath} ...`);
  mkdirp.sync(secretDirPath);
}

let jwtSecret: string = crypto.randomBytes(config.get(constants.CONFIG_JWT_RANDOM_BYTES)).toString('hex');
let jwtSecretPath: string = path.join(secretDirPath, config.get(constants.CONFIG_JWT_SECRET_FILE));
fs.writeFileSync(jwtSecretPath, jwtSecret, 'utf8');

async function setupDb(): Promise<void> {
  await dataAccessLayer.migrate();

  try {
    await dataAccessLayer.seed();
  } catch (e) {
    // ignore missing production seeds, rethrow otherwise
    if (e.toString().indexOf('volatile-seeds/production') === -1) {
      throw e;
    }
  }
}

setupDb().catch(e => console.log(e))
         .then(()=> {
           shell.exec('tskill node');
         });

Solution

  • The problem was that the newest sqlite3 4.0.8 version will not work correctly on this ARM-processor. I downgraded it to 4.0.6 and now it works flawless.