Nodejs : Kineticjs in nodejs - node.js

I install the package Kineticjs in nodejs and have the following error message:
Kinetic.window = Kinetic.document.createWindow();
^
TypeError: undefined is not a function
at /kinetic/kinetic.js:608:47

document.createWindowwas removed from jsdom 1.0.0-pre.1. Use jsdom 0.11.1 with kinetic

If you have problems like that, you can always read on documentation. also
console.log(typeof Kinetic.document.createWindow);
Also if u do not have access to source code or u can not simply find it. u can always output javascript code via simply reading it with simple I/O
http://serebrov.github.io/html/2013-12-02-node-core-module-source.html
here is example
fs = require('fs');
fs.writeFileSync('fs.js', fs.toString());
fs.writeFileSync('fs.readFileSync.js', fs.readFileSync.toString());
this will copy the source code of fs to new js file that u can read.

Related

crypto-js module is imported but does not work as expected

I work on a project which use typescript and es6 syntax. I have installed the module crypto-js npm install crypto-js and his typescript type npm install #types/crypto-js.
I import it then into my file like this:
import * as CryptoJS from 'crypto-js';
But when I tried to use it like in the documentation:
console.log(CryptoJS.MD5('my message'));
It show me an Object structure instead of an unreadable string:
WordArray.init {words: Array(4), sigBytes: 16}
sigBytes: 16
words: Array(4)
0: -1952005731
1: -1042352784
2: 804629695
3: 720283050
length: 4
__proto__: Array(0)
__proto__: Object
What am I forgetting?
In your code, you reference the output from calling the MD5 function which, when passed to typeof returns its type as 'object'.
Though it seems poorly documented, you can reach the string representation of the MD5 value using:
console.log(CryptoJS.MD5('my message').toString())
which produces: "8ba6c19dc1def5702ff5acbf2aeea5aa"
If you plan to run your code using NodeJS, you might consider its native crypto module rather than crypto-js.
const crypto = require('crypto')
const h = crypto.createHash('md5')
h.update('my message')
console.log(h.digest('hex'))
which also prints: "8ba6c19dc1def5702ff5acbf2aeea5aa"
The benefit of using NodeJS' native crypto module here is that, like all native modules, it is bundled into the NodeJS runtime and so doesn't need to be loaded in from an external module.

npm fast-lorem-ipsum not working in nodejs

`var fastLoremIpsum = require('fast-lorem-ipsum').fastLoremIpsum;
console.log(fastLoremIpsum(10,'c'));`
I was trying to use fast-lorem-ipsum npm package but on executing above code in nodejs file,instead of getting lorem-ipsum text i am getting 'undefined' as console output.
What should i do ?
If you look at the documentation for the package, the function you're looking for should be accessed like this —
var fastLoremIpsum = require('fast-lorem-ipsum').fastLoremIpsum;
console.log(fastLoremIpsum(10,'c'));

nodeJS:fs.write callback and fs.writeFile not working

I knew nothing about fs until I was learning to use casperjs to scrape some content from a website and save them to a file. Following some examples on the web, I write this file scrape.js (The json data has been tested so it has nothing to do with the issue):
var fs = require('fs');
var url = "http://w.nycweb.io/index.php?option=com_k2&view=itemlist&id=4&Itemid=209&format=json";
var casper = require('casper').create();
casper.start(url,function(){
var json = JSON.parse(this.fetchText('pre'));
var jsonOfItems={},items = json.items;
items.forEach(function(item){
jsonOfItems[item.id] = item.introtext.split('\n');
})
fs.write('videoLinks.json',JSON.stringify(jsonOfItems),function(err){
if (err) console.log(err);
console.log('videoLinks.json saved');
})
});
casper.run();
When I do casperjs scrape.js in command line of my Ubuntu 14.04 server, I won't see the file saved message as expected, although the file is properly saved. So this is the first question: why the callback isn't running at all?
Secondly, I also tried fs.writeFile, but when I replace fs.write with it, the file isn't saved at all, nor is there any error information.
I do notice that in casper documentation it's said that casper is not a node.js module and some module of node.js won't be available, but I doubt it has anything to do with my issues. And I think it worths to mention that previously when I run this script I only get a respond like
I'm 'fs' module.
I had to follow this question to reinstall fs module globally to get it working.
fs.write expects a file descriptor where you are trying to give it a filename. Try fs.writeFile. https://nodejs.org/dist/latest-v4.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback
Edit: Oh you tried that. Are you sure it didn't write it somewhere like the root directory? Tried a full path in there?
And what version of node are you running?

Cannot call require('fs') with PhantomJS runner

I've been trying to use the FS API in PhantomJS, but I get an error I'm not able to understand when I run the following code
private[scalajssupport] object PhantomFile {
val fs: PhantomFS = js.Dynamic.global.require("fs").asInstanceOf[PhantomFS]
}
The error I get is:
TypeError: undefined is not a constructor (evaluating '$g["require"]("fs")')
However, when I run
var fs = global["require"]("fs")
directly in the PhantomJS REPL, it's working fine.
It turns out that when using PhantomJS to run scala.js code, it is ran in a sandbox with the "webpage" module, which does not have access to require.
The only way to write to the filesystem was to define a callback in onCallback, as seen in the answer to this StackOverflow question.

Meteor: "ReferenceError: fs is not defined"

Losing my mind with this one..
Getting "fs is not defined" on meteor when trying to read a file:
var data = fs.readFileSync(filepathHidden);
I have this package: cfs:filesystem 0.1.2 on Meteor 1.1.0.2
Funny thing here is that if I write in meteor shell fs it prints object and it seems to have lot of functions etc stuff. But the thing here is that after writing fs in meteor shell my code starts to work!? And if I close meteor server and then start it again my server code keeps nagging until I run fs in meteor shell...
Can someone please explain what happens in this case? And how to achieve same thing in my code..
You just need to load it in via npm. In meteor that looks like:
var fs = Npm.require('fs');
var data = fs.readFileSync(filepathHidden);

Resources