1
votes

There is another question just like that: Using dc.js with node.js

But the example in the answer uses dc.js on the client side. I want to use it in the server side.

I created a .js file and I put this line:

const dc = require('dc');

When I execute the file I get this error:

/home/fagner/projects/node-test/node_modules/dc/dc.js:475 if (!Element.prototype.matches) { ^

ReferenceError: Element is not defined at _dc (/home/fagner/projects/node-test/node_modules/dc/dc.js:475:1) at /home/fagner/projects/node-test/node_modules/dc/dc.js:13649:26 at Object. (/home/fagner/projects/node-test/node_modules/dc/dc.js:13654:2) 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 Module.require (internal/modules/cjs/loader.js:637:17) at require (internal/modules/cjs/helpers.js:22:18)

What am I missing?

There are many questions related to this on StackOverflow, but it seems that everyone can import dc.js without any problem. Do I need to install any other dependency?

I know that Element is not recognized by the server, so how can I mock it using domino?

2

2 Answers

1
votes

I have never tried it, and it is about 5 years old, but someone contributed this simple library:

https://github.com/dc-js/dc-node

which wraps dc for node and brings in DOM emulation.

It uses jsdom - you may be right that domino is a better choice today.

If you do fix it up, please consider making a pull request!

1
votes

I had the same error. When I looked to the offending code block in dc.js:475:1, I have found that it starts with the comment

    // polyfill for IE
    // from https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill

So, it looks, it is written for compatibility with IE. I commented the block and after that the module has been loaded with requre('dc') without problems.