Issue with node.js require.paths.unshift() - node.js

I'm new to node.js world, I'm using node.js v0.8.4. I googled for this issue. Yes, i got solutions for this, but still could not understand as a beginner. So please help me......
i was going through a node.js tutorial. In that tutorial node.js v0.1.103 is used and he writes
require.paths.unshift(__dirname+'/vendor');
var http = require('http'),
sys = require('sys'),
nodeStatic = require('node-static/lib/node-static');
var server = http.createServer(function(request, response) {
var file = new nodeStatic.Server('./public', function() {
cache: false
});
request.addListener('end', function() {
file.server(request, response);
});
}).listen(8000);
And the script above works for him. When i write the sample script and run, i get this error.
C:\Documents and Settings\local>node C:\xampp\htdocs\rt\node\livestats\
server.js
Error: require.paths is removed. Use node_modules folders, or the NODE_PATH envi
ronment variable instead.
at Function.Module._compile.Object.defineProperty.get (module.js:386:11)
at Object.<anonymous> (C:\xampp\htdocs\rt\node\livestats\server.js:2:8)
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)
How do i make node to load files from custome directory and How to i solve this issue? My OS is windows
Thank you......

require.paths is deprecated a long time ago. The tutorial for node v0.1 is pretty much useless now, as nearly everything has changed since then.
If you installed node-static with npm install, you can just use require("node-static") without specifying the entire path (and, of course, without using require.paths).

Related

Socket.io Syntax not Recognized on Server

Node server not able to understand socket.io syntax, even with all packages installed.
When I run my server locally with nodejs server.js it works fine is working. But, when I try to run it on my Ubuntu server, it does not seem to understand this line:
socket.on( 'client-data', ( serverpackage ) => {
^
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
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
The directory on my Ubuntu server is user/server, which contains all my server modules. Socket.io is installed, and I even checked to make sure all dependencies for socket.io are there too.
NodeJS-Socket-server-with-DB#1.0.0 /home/<user>/server
└── socket.io#2.1.1
I am also running this version of nodejs:
<user>#host*****:~/server$ nodejs -v
v0.10.25
You can't use arrow functions. You need at minimum version 4 of node however version 6 offers full compatability with arrow functions.
To fix your issue simply update node or change to a regular function like:
socket.on( 'client-data', function (serverpackage) {
Your nodejs version don't understand arrow function, check update if you want use it.
If not, please change arrow function to normal function.
Check Node.js ES2015 Support.

Node.js Express JS - Cannot find module './config/express'

Im Learning to make a web application with Node.js Express JS.
When I run my server.js
$ node server
I get this
Error: Cannot find module './config/express'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/david/Desktop/Node/ejemplo/server.js:2:18)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
my server.js file is like this...
server.js:
var express = require('./config/express');
var app = express();
app.listen(3000);
module.exports = app;
console.log('Server running at http://localhost:3000/');
before doing this I run this command in the root of my app
npm install
Here is a like to a picture to show you the project folder structure I have.
I dont understand why cannot find module './config/express' while apparently the folder structure is fine.
Any idea??
I dont understand why cannot find module './config/express' while apparently the folder structure is fine.
after installing npm, you should try express installation into your project directory as
npm install express
it will create node_modules of express.
then you should use
var express = require('express');
Your server.js doesn't work because express.js file into config folder create confusion.
Change the name from express.js to init.express.js
and then it will work.

Gulp Server throw err

I am trying to run gulp serve to start my live load that was previously working. After I updated node and npm I am getting the following:
$ gulp serve
module.js:515
throw err;
^
Error: Cannot find module 'internal/util/types'
at Function.Module._resolveFilename (module.js:513:15)
at Function.Module._load (module.js:463:25)
at Module.require (module.js:556:17)
at require (internal/module.js:11:18)
at evalmachine.<anonymous>:31:26
at Object.<anonymous> (c:\Development\Template\studio_billing\node_modules\gulp\node_modules\vinyl-fs\node_modules\graceful-fs\fs.js:11:
1)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
Here is my gulpfile:
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
// Save a reference to the `reload` method
// Watch scss AND html files, doing different things with each.
gulp.task('serve', function () {
// Serve files from the root of this project
browserSync.init({
server: {
baseDir: "./"
}
});
gulp.watch("*.html").on("change", browserSync.reload);
gulp.watch("assets/css/*.css").on("change", browserSync.reload);
gulp.watch("assets/js/*.js").on("change", browserSync.reload);
});
I am running Node 8.8.1 and NPM 5.2.0. I am honestly just looking for something to watch changes to my files I am working on and refresh the browser. This solution use to work great but with updating NODE and NPM it seems to have broken it. Any type of solution would be awesome!
Everything use to work just fine before the update. I had updated NPM and Node because of a Laravel project in a different folder.

Firebase NodeJs ReferenceError: Promise is not defined

I came across this error message when trying to deploy a firebase node application to a virtual private server:
/home/.../Backend/node_modules/firebase-admin/lib/firebase-namespace.js:195
this.Promise = Promise;
^
ReferenceError: Promise is not defined
at new FirebaseNamespace (/home/.../Backend/node_modules/firebase-admin/lib/firebase-namespace.js:195:24)
at Object.<anonymous> (/home/.../Backend/node_modules/firebase-admin/lib/default-namespace.js:5:21)
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/.../Backend/node_modules/firebase-admin/lib/index.js:4:16)
at Module._compile (module.js:456:26)
On my local environment, this node application runs without any problem. Both environments are having the same node, npm, and "firebase-admin" module version.
So, I followed the suggestion from here and modified the "firebase-admin" module files on the virtual server. By adding
var Promise = require('es6-promise').Promise;
to some of the module source files manually, I can get rid of the error messages. After that, nothing can be read from the firebase database.
My code section
firebaseDatabase.ref("...").once('value').then(function(snapshot){
....
});
which reads the contents of firebase with no problem on my local environment, never have its "then" called on the virtual server.
What am I doing wrong? Any suggestion is appreciated.
I manage to solved the problem. Just in case if anyone run into the same issue, here are the steps how I fixed it:
For my case, I removed all the modifications I made to the firebase-admin module.
install "es6-promise" if you haven't. (npm install es6-promise --save)
Add the following line to your "server.js" file:
require('es6-promise').polyfill();
Notice that we don't assign the result of polyfill() to any variable. The polyfill() method will patch the global environment (in this case to the Promise name) when called.
I encountered this issue as soon as I firebase init. I did not change or added any codes in the generated scripts. I did solve the issue and was able to deploy by:
Going to the functions folder "cd functions"
sudo npm install es6-promise --save
Browse to the functions/node_modules/firebase-admin/lib/firebase-namespace.js
Add this on top
var Promise = require('es6-promise').Promise;
Your header should look like this:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var deep_copy_1 = require("./utils/deep-copy");
var error_1 = require("./utils/error");
var firebase_app_1 = require("./firebase-app");
var credential_1 = require("./auth/credential");
var DEFAULT_APP_NAME = '[DEFAULT]';
var globalAppDefaultCred;
var globalCertCreds = {};
var globalRefreshTokenCreds = {};
var Promise = require('es6-promise').Promise;

Node.js TypeError: object is not a function

I'm trying to run Mike Wilson's book sample app, but receiving the following error:
pcassiano#...:~/socialnet$ sudo node app.js
/home/pcassiano/socialnet/app.js:60
require('./routes/' + routeName)(app, models);
^
TypeError: object is not a function
at /home/pcassiano/socialnet/app.js:60:35
at Array.forEach (native)
at Object.<anonymous> (/home/pcassiano/socialnet/app.js:57:26)
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)
i'm running the latest code from the book's repo.
what should i do in order to run this sample app properly?
thanks in advance.
You likely have .js files in the routes directory that do not export a function.
app.js is calling require on all files in the routes directory and then calling them as a function. So if any of those files do not follow the general pattern below you'll get the error you're seeing:
module.exports = function(app, models) {
// Add this file's routes using app.get, etc. calls.
...
};
The error reports that the value of require('./routes/' + routeName) is not a function. So you have only one real possible source of the problem.
If './routes/'+routeName doesn't exist, node should throw an error (in v0.8.16 at least), so that isn't it. So the most obvious one is that the module being loaded doesn't export a function (as the error suggests).
You should run console.log('routeName: ', routeName) right before the require statement that is on line 60 of app.js and try running it again. Then, after finding the value of routeName, look in the file it's trying to open. Odds are either module.exports is either exporting an object (or an array, string etc), or is not set at all.
If you're adding routes in the routes folder, take care to follow the same exporting style as the author, namely module.exports = function(app, models) {...}

Resources