nodejs error undefined is not a function - javascript

I am trying to create a simple webserver using node js. This is the script that I have written.
var connect = require('connect');
connect.createServer(
connect.static("../angularjs")
).listen(5000);
I place the script in the nodejs directory and then I try to run it using the command "node server.js" and I get the following error
C:\Program Files\nodejs\server.js:4
connect.static("../angularjs")
^
TypeError: undefined is not a function
at Object.<anonymous> (C:\Program Files\nodejs\server.js:4:22)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
I am new to javascript and request you to kindly guide.
Thanks & Regards
Sunil

your version of connect does not contain static in its propertys,
you either have to downgrade your version of connect (2.xx) or try this code snippet
var connect = require('connect'),
serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic("../angularjs"));
app.listen(5000);
but before run
npm install serve-static

Because static is not the property of connect.
According to the phrase
serve-static - previously static
I think you are using a newer version of connect than the one you code is amed to.
So you have two solutions:
Use an older version of connect
Use serve-static middleware instead of the static method

Related

Issues installing Sequelize

I am finishing some backend tutorials and when i tried to install Sequelize with Node this is what it displays on the screen i have tried to downgrade the versions of Node js to solve the problem and updated other dependencies but it doesnt work, i am currently using 8.4.0 Node version, the one that is used on the tutorial and the lesson but it doesnt fix the issue, hope you can help me with this so i can move on to finish my course and do the exam, here it is what i get after trying to install:
P.D. I am working on a OS Windows 10 desktop computer.
C:\Users\AIO 330-20AST\Desktop\backend\base_datos> node server.js
node-pre-gyp info This Node instance does not support builds for N-API version 3
node-pre-gyp info This Node instance does not support builds for N-API version 6
C:\Users\AIO 330-20AST\Desktop\backend\base_datos\node_modules\node-pre-gyp\lib\util\napi.js:84
throw new Error(
^
Error: The N-API version of this Node instance is 1. This module
supports N-API version(s) 3,6. This Node instance cannot run this
module.
at Object.module.exports.validate_package_json (C:\Users\AIO
330-20AST\Desktop\backend\base_datos\node_modules\node-pre-gyp\lib\util\napi.js:84:9)
at Object.validate_config (C:\Users\AIO
330-20AST\Desktop\backend\base_datos\node_modules\node-pre-gyp\lib\util\versioning.js:229:10)
at Object.exports.find (C:\Users\AIO
330-20AST\Desktop\backend\base_datos\node_modules\node-pre-gyp\lib\pre-binding.js:21:15)
at Object.<anonymous> (C:\Users\AIO
330-20AST\Desktop\backend\base_datos\node_modules\sqlite3\lib\sqlite3-binding.js:3:27)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Module.require (module.js:517:17)
C:\Users\AIO 330-20AST\Desktop\backend\base_datos>
For your help thanks in advance, good coding!

Grave (`) is marked as illegal token

I've been trying to transfer a node program I created locally to an EC2 server via Cloud9, and I have already manually replaced all of the (req,res) => {} statements because it didn't accept that. However, now it's giving me
console.log(`foo ${bar}`)
^
SyntaxError: Unexpected token ILLEGAL
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:945:3
It works fine on my local server and it runs fine here too, even without nodejs.
var bar = "bar"
console.log(`foo ${bar}`)
The version of node is v13.9.0 and it was installed via nvp.
Anyone know why this is happening? I really don't want to go through and convert all the strings with `
Thanks for the help, I figured out that cloud9 automatically installs nodejs, just not the right version. It installs 10.19.0, while the current latest version is 13.10.0. I installed the latest version, but the default was still 10.19.0. For anyone else having this problem, do
nvm ls
to list your installed node versions, then use
nvm alias default 13.10.0 (replace with whatever version you want)

Reading from an external file in node.js

I've just started using node.js, and I have been looking for ways to count the number of lines in a cpp file stored on my machine (in a different directory from that of the node.js app).
I am trying just read text from a cpp file stored inside the node.js project for now, with this function:
console.log(fs.readFileSync('code.cpp', 'utf8'));
but I get this error:
fs.js:646 return binding.open(pathModule._makeLong(path),
stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open
'C:\Users\Heba\WebstormProjects\wrfile\sever.cpp'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at Object. (C:\Users\Heba\WebstormProjects\wrfile\app.js:5:16)
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)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
Process finished with exit code 1
Is this the right approach to the problem? if so how can I fix this error?
Thanks in advance.
according to the error that you get , your script cant locate the file.
make sur that 'code.cpp' is in the same location as your script
try using path.join(__dirname, 'code.cpp');
var fs = require('fs'),
path = require('path'),
file = path.join(__dirname, 'code.cpp');
console.log(fs.readFileSync(file, 'utf8'));

Playing a local file on RPi with Node.js

I have a Raspberry Pi set up with a Node.js app that responds when it sees button push from an Amazon Dash Button. It was originally supposed to be a silent doorbell from https://github.com/initialstate/silent-doorbell, but I would like to just have it play a local sound file. Which I think should be easy enough, but my inexperience with coding leaves me just trying new stuff that I find all over the internet.
I can play the file from the terminal with the following and it plays just fine:
$ omxplayer example.mp3
But, no matter how I try to put it in the Node.js app and trigger when the Dash Button is pressed it won't work.
var dash_button = require('node-dash-button'),
dash = dash_button('XX:XX:XX:XX:XX:XX'), //REPLACE WITH YOUR ADDRESS
exec = require('child_process').exec;
Omx = require('node-omxplayer');
player = Omx('~/node_modules/node-dash-button/example.mp3');
let spawn = require('child_process').spawn;
dash.on('detected', function() {
console.log('Button pushed!');
player.play();
});
When run with my latest as above, I get this:
/home/pi/node_modules/node-dash-button/doorbell.js:7
let spawn = require('child_process').spawn;
^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
After upgrading Node.js to the newest version as suggested by #Quentin using the Major Version Upgrade directions on this site http://thisdavej.com/upgrading-to-more-recent-versions-of-node-js-on-the-raspberry-pi/ I was able to get past this. Now I can't get past how to properly use omxplayer. When running the same code as above after the Node.js upgrade I now get this error after pressing the Amazon Dash button which then crashes the app:
pi#raspberrypi:~/node_modules/node-dash-button $ sudo node doorbell.js
Button pushed!
/home/pi/node_modules/node-omxplayer/index.js:103
throw new Error('Player is closed.');
^
Error: Player is closed.
at writeStdin (/home/pi/node_modules/node-omxplayer/index.js:103:10)
at EventEmitter.Omx.omxplayer.play (/home/pi/node_modules/node-omxplayer/index.js:133:27)
at Readable.<anonymous> (/home/pi/node_modules/node-dash-button/doorbell.js:13:12)
at emitOne (events.js:96:13)
at Readable.emit (events.js:188:7)
at PcapSession.<anonymous> (/home/pi/node_modules/node-dash-button/index.js:87:28)
at emitOne (events.js:96:13)
at PcapSession.emit (events.js:188:7)
at PcapSession.on_packet_ready (/home/pi/node_modules/node-dash-button/node_modules/pcap/pcap.js:99:10)
at packet_ready (/home/pi/node_modules/node-dash-button/node_modules/pcap/pcap.js:44:14)
I tried a few different things to try and get the player to spawn with no luck. The index.js file referenced mentions to use the player.running command but I still get the player is closed error when attempting to use this.
You are using a version of Node older than the 4.x series.
Consequently it sees let as an identifier rather than the keyword, so it doesn't expect it to be immediately followed by another identifier (spawn).
Upgrade your installation of Node to a current version.
Alternatively, use a different variable declaration, such as var.

Run node.js with different file extension

Is is possible to run node.js with a different extension than .js like: node server.type instead of node server.js?
I have tried require.extensions
require.extensions['.type'] = require.extensions['.js'];
but I get this error:
Error: /root/project/server.type: invalid ELF header
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
However I think this has nothing to do with require unless I want to require these files from within node.js.
Works just fine:
$ echo 'console.log("hello world")' > server.type
$ node server.type
hello world
But I'm guessing that you want to run it as an executable. In that case, start your script with:
#!/usr/bin/env node
# followed by your actual script:
console.log("hello world");
And make your script executable:
chmod 755 server.type
After that:
$ /root/project/server.type
hello world

Categories