Meteor: "ReferenceError: fs is not defined" - node.js

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);

Related

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?

node app.js localhost not working

on WINDOWS ...after install express-seed and node.js for the "blog" tutorial, i get the same cmd prompt after typing node app.js.
another time i got body parser and error handling errors
i tried alot of solutions, even had a local host run with another tutorial, but i would like to run from the blog tutorial due to some slight differences of the set-up.
Of course im a newb, and i know theres tons of answers on the forum, but none are correcting my issue...please help.
and everytime i try to post my report on here it errors me saying i have to indent each line 4 spaces. im just losing in general.
Is there a step im missing? all the tut's say just do 'this' and 'this' and i have a local host running so i can make changes to views. any help?
// Module dependencies.
var express = require('express');
var app = express.createServer();
// Configuration
app.configure( function() {
});
// Routes
app.get('/', function(req, res) {
res.send('Hello World');
});
app.listen(3000);
what version of node & express are you running?
From the command line you can check with:
node --version
and
express --version
From your code, it looks like an older version of express (version 3 or less), but I'm betting you didn't specify the version on the npm install, which will give you the latest version (4+). There's a lot of breaking changes between those versions, so you can't run old code with the new framework successfully. My bet is that your blog tutorial hasn't been updated to express 4.x yet.

Error with Simple Express Server and Gulp

I'm trying to run a simple Express web server using a gulp task. I only want a static server that displays the index file. I can easily perform this by running a node module, but again, I want to do this in gulp. I plan on expanding this to allow a LiveReload server to be set up.
I have followed many tutorials on setting up LiveReload but they are failing. I'm assuming it has something to do with the versions being used with respect to when the articles are written. But I was hoping maybe somebody had an idea on how to handle this.
I have created a very small Github repo that allows you to play around with what I'm trying to accomplish: fixit
Gulpfile.js:
var gulp = require('gulp');
var EXPRESS_PORT = 4000;
var EXPRESS_ROOT = __dirname;
gulp.task('express', function () {
var express = require('express');
var app = express();
app.use(express.static(EXPRESS_ROOT));
app.listen(EXPRESS_PORT);
});
*There is an index.html in the same directory as the Gulpfile
And here is the error:
/var/www/clients/client1/web14/sendus-admin/node_modules/express/node_modules/etag/index.js:55
throw new TypeError('argument entity must be string or Buffer')
^
TypeError: argument entity must be string or Buffer
at etag (/var/www/clients/client1/web14/sendus-admin/node_modules/express/node_modules/etag/index.js:55:11)
at SendStream.setHeader (/var/www/clients/client1/web14/sendus-admin/node_modules/express/node_modules/send/index.js:724:15)
at SendStream.send (/var/www/clients/client1/web14/sendus-admin/node_modules/express/node_modules/send/index.js:500:8)
at onstat (/var/www/clients/client1/web14/sendus-admin/node_modules/express/node_modules/send/index.js:585:10)
at Object.oncomplete (fs.js:97:15)
I had the same problem and after a week without Gulp and BrowserSync working (the combination giving me the same error), I resorted to more severe options like reinstalling Node.js. In the end, what worked for me was to use nvm to downgrade to Node.js version 10 (I was using 11 before).
nvm install 0.10
nvm use 0.10
Then just updated and used Gulp:
npm update
gulp
Sure hope that helps you too.
I had the same problem with Express.static since a week. Disabling ETAG for Express.static solves this problem for me untill there is a better fix:
app.use(express.static(path.join(__dirname, '/static'), {etag: false}));

PDF generation with PhantomJS in node-Webkit

I found a comment in this subject node wkhtmltopdf create corrupted PDF in node webkit which indicates that it's possible to generate pdf from html in node-webkit by using PhantomJS and especially with this script: https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js
However I don't understand how to use this script without command line call...
It's not possible to use the script as-is directly in node.js. You would either use the child_process module to call phantomjs essentially as a commandline script with the rasterize.js script and options.
The other possibility is to use a phantom wrapper for node.js to directly include the code of rasterize.js. You would need to make only small adjustments like the page argument is passed from the wrapper and does not need to be created. Possible wrappers are node-phantom or phantomjs-node. If you package your app with node-webkit, then you will probably run into problems with the path to the phantomjs executable.
Phantomjs' Rasterize.js worked well to generate clean multi-page editable pdf's conserving all the tricky CSS. I got a little confused when trying to use it within a nodejs environment but its pretty straight forward.
As per NPM Phantomjs readme: (I've stated it a little more explicitly)
var path = require('path')
var childProcess = require('child_process')
var phantomjs = require('phantomjs')
var binPath = phantomjs.path
//Args for rasterize.js: [ rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]') ]
var childArgs = [ path.join(__dirname, 'rasterize.js'),'url','docname.pdf','A4',1.00]
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
// handle results
})

Socket.io.js not found on Ubuntu but is on Windows

I've been pulling out my hair trying to figure this problem out. I have a node.js app that works fine in windows. I zipped everything up and put it on my linux box (Ubuntu 12) and installed all of the libraries through npm, and yet I still get a 404 message saying my socket.io.js file cannot be found. I've tried various solutions such as linked to the cdn.socket.io script but that just throws a "require not found" error. My code in my html is as follows:
<script src="/socket.io/socket.io.js"></script>
I've even tried <script src="localhost:4000/socket.io/socket.io.js"></script>
and here is my server side:
var express = require('express');
var app = express.createServer();
var io = require('socket.io').listen(app);
...
app.listen(4000);
console.log('server started');
This question is similar, however the answer for it is simply is an updated express semantic which shouldn't apply to my code: socket.io.js not found
If my understanding is correct, the script path should work because when socket.io is running, it should direct that request to the right route. I don't really know what else I should look into for a fix, could it be something with the path in Ubuntu?
Any help would be very much appreciated!
Just tested this:
var io = require('socket.io').listen(8000);
Went to localhost:8000 and the 'welcome to socket.io' message showed up so I know it is running....
paths
node_modules: /home/alex/node_modules/socket.io
my node app: /home/alex/documents/project/app.js
I had the same problem and it seemed that the reason was that something went wrong during the installation of socket.io.
Actually the only workaround I found was to make a new directory, copy all your code there and do a fresh install of the Express and socket.io modules:
npm install express socket.io
Maybe I was lucky, but it worked!

Resources