Node.js Just starting - node.js

i'm following the lynda.com tutorials, love this site, i got my hello world with console.log()
my next task is to try this script
var os = required('os');
var message = 'This script is running on Node.js ' + process.version + ' on a '
+ os.type() + '-based operating system.';
console.log(message);
my error in the terminal
ReferenceError: required is not defined
at Object.<anonymous> (/Users/ArtSir/nodeJS/script.js:1:72)
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:901:3
I double checked my spelling and check my semicolons (that doesn't really matter in node.js like javascript right) but yea any help? i just started. I don't understand why required is not defined, i installed node. was i suppose to install 'os'?

It's require, withoud d:
var os = require('os');

Related

unable to install meanjs on linux mint

it has been one week now that i was trying to install meanjs stack and had no success.
Everytime I follow some tutorial and get errors. Now I end up with many issues, too much nodejs version, i dont know which one should be with what and why do karma not working ...etc
is there anyway to remove everything and install meanjs correctly and get it running successfully
+ WARNING: It is strongly recommended that you change sessionSecret config while running in production!
Please add `sessionSecret: process.env.SESSION_SECRET || 'super amazing secret'` to
`config/env/production.js` or `config/env/local.js`
/opt/mean/node_modules/connect-mongo/src/index.js:3
const Promise = require('bluebird');
^^^^^
error: uncaughtException: Use of const in strict mode. date=Mon Jul 18 2016 19:15:59 GMT+0400 (GST), pid=29602, uid=1000, gid=1000, cwd=/opt/mean, execPath=/usr/local/bin/node, version=v0.10.33, argv=[node, /opt/mean/server], rss=47960064, heapTotal=35267072, heapUsed=18951416, loadavg=[0.5966796875, 0.453125, 0.66748046875], uptime=4058.491096953
SyntaxError: Use of const in strict mode.
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (/opt/mean/node_modules/connect-mongo/index.js:1:80)
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. (/opt/mean/config/lib/express.js:12:16)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
below is my system config
Linux Mint 17.2 64-bit
NodeJS v0.10.33
npm 1.4.28
bower 1.7.9
mongodb v3.0.12
Can anyone please help me, I want to get started.
I haven't dug into it, but I'm guessing the version of node you have doesn't support the 'const' keyword. 0.10.x is a (fairly) old version of node; have you considered upgrading to one of the versions that has ES 6 native support?

Some problems about the raw-socket in Node.js

My operating system is Windows 10 Version 10586 and node.js is Version 4.4.5.
When I use the module raw-socket follow the example in this page
https://github.com/stephenwvickers/node-raw-socket
When running code like this:
var raw = require ("raw-socket");
var socket = raw.createSocket ({protocol: raw.Protocol.None});
I got following error:
C:\Users\KEEY\node_modules\raw-socket\index.js:47
this.wrap = new raw.SocketWrap (
^
Error: ��һ�ַ���Ȩ�޲������ķ�ʽ����һ�������׽��ֵij��ԡ�
at Error (native)
at new Socket (C:\Users\KEEY\node_modules\raw-socket\index.js:47:14)
at Object.exports.createSocket (C:\Users\KEEY\node_modules\raw-socket\index.js:202:9)
at Object.<anonymous> (C:\Users\KEEY\Desktop\node\raw-socket\demo.js:2:18)
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 Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
[Finished in 0.2s with exit code 1]
How to solve it?
Thanks for everyone's help.
I email Stephen Vickers,the author of the module raw-socket,the source of problem is that
Stephen Vickers:
It looks like you don't have permissions to open raw sockets on that platform.
Then I run it as administrator, it works!
Thank you,Stephen Vickers
try this
var socket = raw.createSocket ({
protocol: raw.Protocol.None,
socketOption: raw.SocketOption.IPV6_HDRINCL
});

Using the Forever Node Module on multiple projects

I am in the process of setting up two different web application projects on my server. I am attempting to get Node, MongoDB, and the Forever Node module set up correctly so that I can use Forever on my scripts to keep them running 24/7.
The main issue I am running into is the local vs. global installations for Node and the two modules. I have attempted this installation process five times now using a few resources online as well as a bash script that I found here (https://github.com/punkave/stagecoach/blob/master/sc-proxy/install-node-and-mongo-on-ubuntu.bash).
Nothing I do seems to work correctly. Either the Forever command doesn't work from my application's folder or when Forever does work, the process gets stopped immediately with a log file created. Here is the contents of the log file:
module.js:340
throw err;
^
Error: Cannot find module 'mongodb'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/www/html/test/requestHandlers.js:6:13)
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)
error: Forever detected script exited with code: 8
So obviously I am not installing Node, MongoDB, and Forever correctly. What is the best way to go about this process?
I am running on an Ubuntu 14.04 instance hosted on an Amazon AWS EC2 server.
Here is my require() for mongodb:
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db,
host = "localhost";
Latest error log that Forever creates:
/var/www/html/test/node_modules/mongodb/lib/mongodb/db.js:403
if(connection && connection.serverCapabilities.hasListCollectionsCommand) {
^
TypeError: Cannot read property 'hasListCollectionsCommand' of undefined
at Db.listCollections (/var/www/html/test/node_modules/mongodb/lib/mongodb/db.js:403:49)
at Db.createCollection (/var/www/html/test/node_modules/mongodb/lib/mongodb/db.js:1026:8)
at Object.<anonymous> (/var/www/html/test/requestHandlers.js:15:4)
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> (/var/www/html/test/indextest.js:3:23)
error: Forever detected script exited with code: 8

AssertionError: path must be a string is thrown when requiring own module

I wanted to create a very minified version of hapi-ninja and came across following problem:
var settings = require('./app/server/config/settings');
var routes = require('./app/server/config/rout');
The first line works as it should an returns my modules. But the second line throws following Exception
AssertionError: path must be a string
at Module.require (module.js:362:3)
at require (module.js:380:17)
at Object.<anonymous> (/home/hknlof/development/mygit/todos/app/server/config/rout.js:8: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/hknlof/development/mygit/todos/tryout.js:1:75)
I am running on Node v0.10.25 and hapi-ninja is working fine. When I don't require the rout module it does work. My rout and settings modules look very much the same as in hapi-ninja. I isolated the two require statements in one file. Tried both on their own, still the same result. Can't get my head around this. Tried debugging but the value of the node internal path does never change to the above string. Encoding ist always utf-8.
Thank you vkuchartkin and Tracker1.
So my mistake was that I forgot to require a certain directory in my rout.js
var getController = require(module, '../controller');
was my code. Should have been:
var requireDir = require('require-directory');
var get Controller = requireDir(module, '../controller');
Now I feel a bit stupid

nodeJS ReferenceError : navigator is not defined.

Hello I'm trying to make some Javascript files working on nodeJS everything is well set however here is the error I get :
C:\>node myServer.js
C:\myFunctionsCallTest.js:750
if( (j_lm && (navigator.appName == "Microsoft Internet Explorer")) || navigato
^
ReferenceError: navigator is not defined
at Object.<anonymous> (C:\myFunctionsCallTest.js:750:15)
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> (C:\myServer.js:1:79)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
C:\>
The files I'm using are :
myServer.js
var my_module = require("./myFunctionsCallTest.js");
console.log("RSA encrypted message :"+ my_module.Encrypt("myKey","myMessage"));
myFunctionsCallTest.js //This file contains some call of prototypes like :
.....................................
RSAKey.prototype.setPublic = RSASetPublic;
RSAKey.prototype._short_encrypt = RSAEncrypt;
RSAKey.prototype.encrypt = RSAEncryptLong;
RSAKey.prototype.encrypt_b64 = RSAEncryptB64;
..............................................
Any idea from where this issue could come?
Thanks.
It looks like you're using Tom Wu's BitInteger and RSA JavaScript library. This library was designed for use in the browser and doesn't work quite right in Node.js.
Luckily, someone has already gone through the work of packaging this library in a Node.js compatible format; it's on GitHub at https://github.com/eschnou/node-bignumber and available via npm as the package "bignumber", and comes with a nice example.

Resources