Download plotly open source on node js - node.js

Can you download the entire module of plotly using node js. Right now, I am streaming data with plotly using node js by using my API keys. If there is a way, can yo give step by step instructions? I tried https://www.npmjs.com/package/plotly.js, but it does not work.
var plotly = require('plotly.js');
var initdata = [{x:[], y:[], stream:{token:'t2166m92ft', maxpoints:50}}];
var initlayout = {fileopt : 'overwrite', filename : 'nodenodenode5'};
plotly.plot(initdata, initlayout, function (err, msg) {
if (err) return console.log(err);
console.log(msg);
var stream1 = plotly.stream('t2166m92ft', function (err, res) {
if (err) return console.log(err);
console.log(res);
clearInterval(loop); // once stream is closed, stop writing
});
var i = 0;
var loop = setInterval(function () {
client.once('message', function (message) {
var data = { x : i , y : message.toString()};
var streamObject = JSON.stringify(data);
stream1.write(streamObject+'\n');
i++;
});
}, 5000);
});
}
when i tried install using npm install plotly.js, and ran my program I got :
\Users\intern\Documents\universal-ground-system\Node js\node_modules\plotly.js
rc\lib\index.js:397
var style = document.createElement('style');
^
ReferenceError: document is not defined
at Object.lib.addStyleRule (C:\Users\intern\Documents\universal-ground-syste
Node js\node_modules\plotly.js\src\lib\index.js:397:21)
at Object.<anonymous> (C:\Users\intern\Documents\universal-ground-system\Nod
js\node_modules\plotly.js\build\plotcss.js:61:16)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Users\intern\Documents\universal-ground-system\Nod
js\node_modules\plotly.js\src\plotly.js:30:1)
at Module._compile (module.js:409:26)

Plotly open source library can not be used in node js. But can be used on client side javascript.

I got past this to some degree, but ran into another error, so close now it seems.
When I make a dom with jsdom it doesn't like to see window for some sort of async issue probably.
If you go into REPL load the file or otherwise get to make the new jsdom object, then you can do this sort of thing in REPL that references window and the needed document.
let jsdom = lib.require('jsdom');
//let window = (new jsdom.JSDOM('<p>Hello</p>')).window;
let dom = new jsdom.JSDOM('<p>Hello</p>');
/* While just testing I do this in REPL after .load index.js
let window = dom.window;
let document = window.document;
*/
So then I get a new error after npm install canvas that needs
apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++
first to properly build it on Ubuntu 16
so the new error is...
> let window = dom.window;
undefined
> let document = window.document;
undefined
>
>
> let plt = lib.require('plotly.js');
ReferenceError: self is not defined
at Object.254 (./node_modules/mapbox-gl/dist/mapbox-gl.js:509:29)
at s (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:684)
at ./node_modules/mapbox-gl/dist/mapbox-gl.js:1:735
at Object.252../window (./node_modules/mapbox-gl/dist/mapbox-gl.js:505:25)
at s (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:684)
at ./node_modules/mapbox-gl/dist/mapbox-gl.js:1:735
at Object.73.../package.json (./node_modules/mapbox-gl/dist/mapbox-gl.js:146:75)
at s (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:684)
at e (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:855)
>
I tried setting 'self' to 'document' and 'this' and get another error.
> self = document;
Document { location: [Getter/Setter] }
> plt = lib.plt = lib.require('plotly.js');
TypeError: Cannot read property 'hardwareConcurrency' of undefined
at Object.252../window (./node_modules/mapbox-gl/dist/mapbox-gl.js:505:834)
at s (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:684)
at ./node_modules/mapbox-gl/dist/mapbox-gl.js:1:735
at Object.73.../package.json (./node_modules/mapbox-gl/dist/mapbox-gl.js:146:75)
at s (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:684)
at e (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:855)
at ./node_modules/mapbox-gl/dist/mapbox-gl.js:1:873
at ./node_modules/mapbox-gl/dist/mapbox-gl.js:1:150
Next I attempted to get window.navigator into self but that's apparently got nothing to do with the error. Somewhere else in mapbox-gl.js is glitching out.
I've only found a reference to harwareConcurrency in mapboxgl.js here
https://github.com/mapbox/mapbox-gl-js/issues/899
Seems misleading to say it's a nodejs library when it only works in the browser? https://plot.ly/nodejs/
Just now I'm seeing something about an API Key? I guess I'm barking down the wrong tree sideways. Big mistake here.

Related

Node Using JSON-Stat

I am trying to use JSONStat toolkit for the first time. However I keep getting this error
let method = init.method || input.method || 'GET';
^
TypeError: Cannot read property 'method' of null
at new Request (/app/node_modules/node-fetch/lib/index.js:1217:21)
at /app/node_modules/node-fetch/lib/index.js:1439:19
at new Promise (<anonymous>)
at fetch (/app/node_modules/node-fetch/lib/index.js:1437:9)
at module.exports (/app/node_modules/jsonstat-toolkit/main.cjs:2:15410)
at Object.<anonymous> (/app/index.js:2:1)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
code when running:
const JSONstat = require("jsonstat-toolkit");
JSONstat( "https://json-stat.org/samples/oecd-canada-col.json" , function() {
if( this.class==="collection" ){
var ds1=this.Dataset( 0 );
}
} );
You are trying to use the syntax for the v0.x of JSONstat. The example clearly shows the correct use.
Change this to be a parameter that you accept as an argument to the function:
const JSONstat = require("jsonstat-toolkit");
JSONstat( "https://json-stat.org/samples/oecd-canada-col.json" , function(j) {
if( j.class==="collection" ){
var ds1=j.Dataset( 0 );
}
} );
Even with the correct code, version 1.4.1 seems to fail. Downgrading the package to 1.3.2 seems to fix it: npm i jsonstat-toolkit#1.3. UPDATE This has been fixed in 1.4.2 thanks to OP's bug report.
Alternatively, downgrade to version 0 and keep your same syntax (using this): npm un jsonstat-toolkit && npm i jsonstat.
Issued fixed in verison 1.4.2;
https://github.com/jsonstat/toolkit/issues/2#issuecomment-1079877683

Node.js - Import Package Issue - Not a Function

Using node v12.9.0 on macOS Mojave, have the following project structure (with app being the root folder):
app
\
utils.js
app.js
utils.js
console.log('utils.js')
const add = function(a ,b) {
return a + b
}
module.exports = add
app.js
const add = require('./utils.js')
const sum = add(4, -2)
console.log(sum)
Tried running this:
node app.js
Error:
app/app.js:3
const sum = add(4, -2)
^
TypeError: add is not a function
at Object.<anonymous> (/Users/devuser/app/app.js:3:13)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
at internal/main/run_main_module.js:17:11
Can you try this:
utils.js
const add = function(a ,b) {
return a + b
}
module.exports = {
add : add
}
app.js
const utils = require('./utils.js')
const sum = utils.add(4, -2)
console.log(sum)
It actually works now using my original code inside both the Integrated Terminal that Visual Studio Code provides and the native macOS Terminal.
Now it just prints:
2
Don't know why it was had a TypeError earlier?
Am new to node.js so I can't comment on what changed?

Node's Commander can't prompt terminal

I'm using the popular Commander npm module in a command-line program i'm building. It works great except that all of the functions it provides that solicit user input -- aka, choose, prompt, and password -- fail to work.
As an example I'm using:
program
.command('test')
.action(function(param) {
program.prompt('Username: ', function(name){
console.log('hi %s', name);
});
program.prompt('Description:', function(desc){
console.log('description was "%s"', desc.trim());
});
}
);
This results in the following error (yet it is copy and pasted directly out of the documentation/examples):
TypeError: Object # has no method 'prompt'
at Command. (lib/tg.js:780:11)
at Command.listener (node_modules/commander/index.js:249:8)
at Command.emit (events.js:98:17)
at Command.parseArgs (/node_modules/commander/index.js:480:12)
at Command.parse (/node_modules/commander/index.js:372:21)
at Object. (/lib/tg.js:806:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
Try using the Node Prompt module. Install it from npm with this command:
npm install prompt --save
The docs can be found here: https://github.com/flatiron/prompt
Make sure you also require it in your code, usually at the top.
var prompt = require('prompt');
Remember, Node is non-blocking. Multiple prompts will attempt to get user input at the same time. To get around this, split your prompts up into functions and call the next prompt in the callback.
Example:
var first_prompt = function() {
var schema = {
// Read the docs on creating a prompt schema
};
prompt.start();
prompt.get(schema, function(err, result) {
// Do stuff with the input
// Call the next prompt
next_prompt();
});
};

nodejs error when i try to run a file with jquery in it

also when i run
node script.js page.html i have this error, i dont know why, i did put jquery in my dependencies and it installed it
C:\Users\NEBELYN\Desktop\projects1\paragraph\node_modules\jquery\dist\jquery.js:29
throw new Error( "jQuery requires a window with a document" );
^ Error: jQuery requires a window with a document
at module.exports (C:\Users\NEBELYN\Desktop\projects1\paragraph\node_modules\jquery\dist\jquery.js:29:1
at Object.<anonymous> (C:\Users\NEBELYN\Desktop\projects1\paragraph\script.js:9:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
here is the code am running
var argv = require('optimist').argv,
$ = require('jquery'),
fs = require('fs');
var file = argv._[0];
var html = fs.readFileSync(file, 'UTF-8');
$(html).find('p').each(function(index) {
var content = $(this).html();
console.log('Paragraph ' + (index + 1) + ': ' + content);
});
and here is the page.html
<html>
<body>
<p>Apple</p>
<span>Unrelated</span>
<p>Orange</p>
<div>
Steak
</div>
<p>Banana</p>
</body>
</html>
jQuery requires a full DOM implementation, which you don't have on the server.
To parse HTML text, you should use a simpler library called Cheerio, which has a jQuery-like API but does not use a DOM.
Alternatively, you could use JSDOM to fake a browser DOM on the server (this is slower).
I had code that worked fine a month ago, but started to display this error when I upgraded to the latest jquery via npm. If I only knew how to get back that older version, then the problem would go away.
This seems to have fixed it:
npm install jquery
npm install jsdom
// Define window
var jsdom = require("jsdom");
var window = jsdom.jsdom().createWindow();
var $ = require("jquery")( window );

mongodb with nodejs

this is th code I am using inserting a document to mongodb.
var client = new Db('test', new Server("127.0.0.1", 27017, {}), {w: 1}),
test = function (err, collection) {
collection.insert({a:2}, function(err, docs) {
collection.count(function(err, count) {
test.assertEquals(1, count);
});
// Locate all the entries using find
collection.find().toArray(function(err, results) {
test.assertEquals(1, results.length);
test.assertTrue(results[0].a === 2);
// Let's close the db
client.close();
});
});
};
client.open(function(err, p_client) {
client.collection('test_insert', test);
});
but while running I am getting error
xports, require, module, __filename, __dirname) { var client = new Db('test',
^
ReferenceError: Db is not defined
at Object. (C:\Users\Basic node\cheerio\mongonode.js:1:81
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
can you suggest me how to solve this problem
thanks in advance
Please import all the required modules, which you are using. Db is not defined points out that Db is defined in some other module or you have forgot to declare it.
You'll notice this exact code block posted in a number of different stackoverflow questions. The problem here is that this is a copy and pasted code block from mongodb's documentation, as is in fact the first example of a mongodb nodejs program.
https://npmjs.org/package/mongodb
You'll find this under "Introduction" as "A simple example of inserting a document."
It's clearly an incomplete example, but a lot of people are just trying it out to see if they've got everything installed correctly and immediately run into a wall.
Most people will have installed the mongodb driver, but will be missing something at the top like this:
var mongodb = require('mongodb');
var Db = mongodb.Db;
var Server = mongodb.Server;
I also fell into the copy-paste trap here and ran into another issue with the "assertEquals" method not existing. I've seen other people reference that function in other places on the web, but not really sure how it works.
In any case, to make it work for me, I required the assert module:
var assert = require('assert');
And then I replaced the assertEquals lines with something like this:
assert.equal(1, count, "Unexpected result");
Note that you're going to run into an issue if you've run this a couple of times; it's going to count the number of things in that table, and there is going to be more than one.
You'll have to figure out how to get into mongo's CLI and remove them to get it to run successfully.
Try to install mongodb native driver
npm install mongodb

Resources