Selenium Webdriver: extending Navigation causes circular dependency issue - node.js

I have a very simple app.ts file that looks like this:
import { Navigation, WebDriver } from "selenium-webdriver";
class MyWebDriver extends WebDriver {
navigate(): Navigation {
return new MyNavigation(this);
}
}
class MyNavigation extends Navigation {
constructor(driver: WebDriver) {
super(driver);
}
}
I have also installed the NPM package selenium-webdriver#4.0.0-alpha5 and I'm using NodeJS version 10.15.3.
Now when I build the project and run the app.js file on the command line, I get the following circular dependency exception:
C:\temp\MyTestNodejsProject>node app.js
C:\temp\MyTestNodejsProject\app.js:9
class MyNavigation extends selenium_webdriver_1.Navigation {
^
TypeError: Class extends value undefined is not a constructor or null
at Object.<anonymous> (C:\temp\MyTestNodejsProject\app.js:9:49)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
The above circular dependency exception is complaining about line 9 of the app.js file below:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const selenium_webdriver_1 = require("selenium-webdriver");
class MyWebDriver extends selenium_webdriver_1.WebDriver {
navigate() {
return new MyNavigation(this);
}
}
class MyNavigation extends selenium_webdriver_1.Navigation {
constructor(driver) {
super(driver);
}
}
//# sourceMappingURL=app.js.map
Can someone please help me figure out how to fix this circular dependency? I've spent days on this and don't see where the circular dependency is happening. Please provide a working code snippet.
============ EDIT =============
I did a sanity check to make sure that Navigation was properly imported (to make sure that wasn't the issue) and modified the code to be:
import { Navigation, WebDriver } from "selenium-webdriver";
class MyWebDriver extends WebDriver {
navigate(): Navigation {
return new Navigation(this);
}
}
Then when I built & ran app.js, the circular dependency exception went away, and app.js ran just fine.

Related

NextJS - Unexpected Token Import

While integrating react-syntax-highlighter into my next-js project I've used the following code:
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { okaidia } from "react-syntax-highlighter/dist/esm/styles/prism";
...
<SyntaxHighlighter language="jsx" style={okaidia}>
{some code goes here}
</SyntaxHighlighter>
...
I get the following error upon running npm run dev, but only if I run the page directly.
Unexpected token export
/Users/johndetlefs/repos/tal-gel-framework/node_modules/react-syntax-highlighter/dist/esm/styles/prism/index.js:1
(function (exports, require, module, __filename, __dirname) { export { default as coy } from './coy';
^^^^^^
SyntaxError: Unexpected token export
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
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 Object.react-syntax-highlighter/dist/esm/styles/prism (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:7242:18)
at __webpack_require__ (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:23:31)
at Module../pages/components.js (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:6839:104)
at __webpack_require__ (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:23:31)
at Object.3 (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:7175:18)
at __webpack_require__ (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:23:31)
at module.exports../js/config/libs/_theme-foundation--colors.js.config.themes.list.name (/Users/johndetlefs/repos/tal-gel-framework/.next/server/static/development/pages/components.js:91:18)
If I navigate to the page via another page then everything works great. If I then refresh the page I get the error.
Removing the line
import { okaidia } from "react-syntax-highlighter/dist/esm/styles/prism";
and removing the style attribute from the component fixes everything, but uses the default prism style, which is not the desired outcome.
Looking around I can see people have similar issues, and that the fix probably has something to do with the next.js.config file, and how the css file is being loaded server-side, but I'm not 100% what to do there.
Assuming the next.js.config file is a part of the solution, here are the current contents.
const withSass = require("#zeit/next-sass");
const withCSS = require("#zeit/next-css");
module.exports = withCSS(
withSass({
webpack(config) {
config.module.rules.push({
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: {
loader: "url-loader",
options: {
limit: 100000
}
}
});
config.module.rules.push({
test: /\.svg$/,
use: ["#svgr/webpack"]
});
return config;
}
})
);
I've tried both with & without withCSS and the issue stays the same.
Any help would be much appreciated! 👍
After digging around for a while, I checked out the npm packages directory and found that there are two types of dists: cjs & esm. The simple fix is just using the cjs dist instead of the esm dist.
import { darcula } from 'react-syntax-highlighter/dist/cjs/styles/prism';
Hope this helps :)

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');
});
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.
I also had this problem when upgrading from sqlite3 version 4.0.4 to version 4.1.0. Pinning my dependency to 4.0.4 got it working again. You can also see some other workarounds here and here, and discussion of usage in browser environments here.

How can I export a class with multiple static methods

I am trying to export this class in node.js:
export class className {
static method1(param1) {
// do something
}
static method2(param1, param2) {
// do something
}
}
But it's getting me the following errors in terminal:
complete_path.....\node_modules#babel\runtime\helpers\esm\classCallCheck.js:1 [2] (function (exports, require, module, __filename, __dirname) {
export default function _classCallCheck(instance, Constructor) { [2]
^^^^^^ [2] [2] SyntaxError: Unexpected token export [2] at new
Script (vm.js:83:7) [2] at createScript (vm.js:267:10) [2] at
Object.runInThisContext (vm.js:319:10) [2] at Module._compile
(internal/modules/cjs/loader.js:685:28) [2] at
Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
[2] at Module.load (internal/modules/cjs/loader.js:620:32) [2]
at tryModuleLoad (internal/modules/cjs/loader.js:560:12) [2] at
Function.Module._load (internal/modules/cjs/loader.js:552:3) [2]
at Module.require (internal/modules/cjs/loader.js:658:17) [2] at
require (internal/modules/cjs/helpers.js:22:18) [2] [nodemon] app
crashed - waiting for file changes before starting...
The export keyword is not supported by Node.js yet. You have to use the exports or module.exports ones.
In your case you should use the module.exports:
module.exports = class className {
static method1(param1) {
// do something
}
static method2(param1, param2) {
// do something
}
}
For more information about the difference between the exports and module.exports I suggest you this post.
Use module.exports and not export
module.exports = class className {
static method1(param1) {
// do something
}
static method2(param1, param2) {
// do something
}
}

Extend SchemaDirectiveVisitor To Use Apollo Server Schema Directives in NodeJS

I'm trying to extend SchemaDirectiveVisitor in order to make a custom directive in Apollo Server 2. I'm specifically using the 2.2.6 hapi node module.
Here's my server.js code:
const { ApolloServer } = require('apollo-server-hapi');
const { SchemaDirectiveVisitor } = ApolloServer;
class ViewTemplateGroup extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
console.log('Im calling this directive!');
return;
}
}
When I start up my server I immediately get the following error:
TypeError: Class extends value undefined is not a constructor or null
at Object.<anonymous> (/Users/garrett.kim/Desktop/Projects/Test Web/poc-graphQL-forms-gyk/server.js:36:33)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:193:16)
at bootstrap_node.js:617:3
To my knowledge, I'm following the Apollo Server 2 example very closely.
https://www.apollographql.com/docs/apollo-server/features/creating-directives.html
Any help getting directives working would be appreciated.
The ApolloServer class does not have a SchemaDirectiveVisitor property on it; therefore, calling ApolloServer.SchemaDirectiveVisitor results in undefined and a class cannot extend undefined as the error indicates. Just import SchemaDirectiveVisitor directly from the apollo-server-hapi module:
const { ApolloServer, SchemaDirectiveVisitor } = require('apollo-server-hapi')

Webpack 3.3 & NodeJs 8.1 Cross-file inheritance gives TypeError?

So I'm having a strange error with Webpack 3.3 and NodeJS 8.10 (running on AWS Lambda). The error message is:
module initialization error: TypeError
at Object.<anonymous> (/var/task/presenters/api/financial/posts.js:6480:55)
at __webpack_require__ (/var/task/presenters/api/financial/posts.js:20:30)
at Object.<anonymous> (/var/task/presenters/api/financial/posts.js:6134:21)
at __webpack_require__ (/var/task/presenters/api/financial/posts.js:20:30)
at Object.<anonymous> (/var/task/presenters/api/financial/posts.js:6287:24)
at __webpack_require__ (/var/task/presenters/api/financial/posts.js:20:30)
at DEFAULT_DESCRIPTION (/var/task/presenters/api/financial/posts.js:63:18)
at Object.<anonymous> (/var/task/presenters/api/financial/posts.js:66:10)
... truncated
Line 6480 of the webpack file is:
const FinancialEvent = __webpack_require__(59);
// Other requires removed for clarity.
const {
financial: {
environments
}
} = __webpack_require__(14);class DwollaEvent extends FinancialEvent {
Over in the parent class file FinancialEvent.js, there's:
class FinancialEvent extends Object {
// Truncated for clarity.
}
module.exports = FinancialEvent;
If I eliminate this cross-file inheritance, the error goes away:
const FinancialEvent = __webpack_require__(59);
// Other requires removed for clarity.
const {
financial: {
environments
}
} = __webpack_require__(14);
class DummyParent extends Object {
}
class DwollaEvent extends DummyParent {
The error also stops if I copy the FinancialEvent class into DwollaEvent.js and refer to it directly.
Is this a known issue with Webpack & cross-file inheritance in NodeJS? Does anyone have a solution? Just so you know, upgrading to Webpack 4.0 isn't a solution for the moment, because of the significant changes that were made to the configuration files, etc. Upgrading NodeJS isn't a solution because that's what AWS Lambda supports.
Thanks in advance!

Resources