Cannot call require('fs') with PhantomJS runner - require

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.

Related

How do you export an object in a module in nodejs?

There is something wrong with the way I understand how to use classes in a Javascript module and export them, or some bad assumption I made about how nodejs works. Please help me understand this better. I wanted to write a module that exposed an object that will "store things safely." I have a file ("safestore.js") with this in it:
class Safestore {
constructor() {
console.log("SUCCESS!");
}
... // I defined other methods here...
}
exports.safestore = Safestore; // I tried this with `new Safestore` and `new Safestore()` too.
I run nodejs on my command line and then:
> ss = require('./safestore');
{ safestore: [Function] }
> s = ss.safestore('pwd','./encFile.enc');
ReferenceError: Safestore is not defined...
Why is it telling me that Safestore is not defined while executing the safestore function which is defined in the same file where Safestore is, actually defined?
The question does not contain enough information, although there is a clue. node and nodejs are two different pieces of software, and I was using the wrong one. I also didn't specify what version of nodejs I ran from my command line. When I ran it with node (instead of nodejs) I got errors that made sense and I was able to fix them.
Thanks to #Ethicist for listing the version of Node he used, as this got me to double check all those things.
I just need to remember that node and nodejs each do different things. Further research shows me that nodejs is a symlink to version 8.10.0 of node.js, and node is a symlink to the version that I set with nvm. I solved the problem permanently for myself with sudo rm /user/bin/nodejs and I'll remember, if I ever see an error that says nodejs doesn't exist, that it wants the old version of node.js.

Module not found: Error: Can't resolve 'fs' when trying to use a NodeJS library

I initiated a basic ReactJS app using npx create-react-app, then I ejected using npm run eject. Now when I am trying to import the Casual library by import casual from 'casual';, I get the following error:
Compiled with problems:
ERROR in ./node_modules/casual/src/casual.js 3:13-37
Module not found: Error: Can't resolve 'fs'
in '/home/me/project/node_modules/casual/src'
And the code around line number 3 in casual.js looks like this:
var helpers = require('./helpers');
var exists = require('fs').existsSync;
var safe_require = function(filename) {
if (exists(filename + '.js')) {
return require(filename);
}
return {};
};
...
I found answers to similar questions. Those were mainly Node or Angular related. I also tried answers suggesting some changes in webpack config, but no luck.
The reason is Casual doesn't work on the front end. It runs on Node.js only.
You need to install maybe a new package to make things work.
Fs is unavailable on the browser so it won't work. Instead, you should use casual-browserify, it will work on browsers.

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?

When running mocha test I get "object is not a function" but not when i run the test directly

when i run "mocha" to run all my tests i get this error:
var should = chai.should(); ^
TypeError: Property 'should' of object #<Object> is not a function
However when I run "mocha test/filename.js" on the file in question it runs fine.
The chai.should() came from a question I asked previously.
latest node/npm/mocha/chai/should
What would cause such discrepancy in such between the different runs?
thanks
It appears since mocha was loading should in earlier tests like,
var should = require('should');
and trying to use that should globally, so when it got to my
var should = chai.should();
in later tests it would bomb because it conflicted, so when running the test alone there was no conflict.
I solved this by going through all my tests and redefining my should requirement to the one above.

Nodejs : Kineticjs in nodejs

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.

Resources