SyntaxError: Unexpected reserved word sql-cli - node.js

I'm trying to follow this guide to use mssql with docker in my Mac https://database.guide/how-to-install-sql-server-on-a-mac/ but when trying to connect to SQL server I get a SyntaxError: Unexpected reserved word error in /usr/local/lib/node_modules/sql-cli/lib/cli.js:21
Error:
/usr/local/lib/node_modules/sql-cli/lib/cli.js:21
class SqlCli {
^^^^^
SyntaxError: Unexpected reserved word
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.<anonymous> (/usr/local/lib/node_modules/sql-cli/bin/mssql.js:1:76)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
Code: (Not mine)
...
class SqlCli {
constructor() {
this.db = new MSSQLDbService();
this.messages = new Messages();
this.options = new Options();
this.buffer = new Buffer();
}
...
I'm using node version 0.10.35 since this is the one my project is using and I can't change it.

I took a look at the tutorial you posted and it downloads SqlCli with npm.
When you name a class or file the same as a package in node_modules it will conflict with the node package and ultimately give you this fatal error,
Try changing the class and filename to something different and check again.
class DBCli {
constructor() {
this.db = new MSSQLDbService();
this.messages = new Messages();
this.options = new Options();
this.buffer = new Buffer();
}
}

Related

Main Bower files

In my angularjs app, i try to concat bower file because i have to many lib. I make a task for concat file, here is code
var filter = require('gulp-filter');
var mainBowerFiles = require('gulp-main-bower-files');
var dest = 'dist/scripts';
gulp.task('main-bower-files', function() {
return gulp.src('./bower.json')
.pipe(mainBowerFiles([[filter, ]options][, callback]))
.pipe(gulp.dest('dist/scripts'));
});
After i call this task in nodejs cmd, i get this error in chrome console
.pipe(mainBowerFiles([[filter, ]options][, callback]))
This is error in node cmd
.pipe(mainBowerFiles([[filter, ]options][, callback]))
^^^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
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 Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Liftoff.handleArguments (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:116:3)
at Liftoff.<anonymous> (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\index.js:198:16)
at module.exports (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\node_modules\flagged-respawn\index.js:17:3)
If someone know solution??? Thnx
i fix this with another library, called BOWER-FILES.
var lib = require('bower-files')();
gulp.task('bower_js', function () {
gulp.src(jsBowerFile)
.pipe(concat('bower_js.js'))
.pipe(gulp.dest('dist/scripts'))
.pipe(ngmin())
.pipe(uglify({mangle: false}))
.pipe(gulp.dest('dist/scripts'));
});

proper way of using es6 classes in a nodejs project

I'd like to be able to use the cool es6 classes feature of nodejs 4.1.2
I created the following project:
a.js:
class a {
constructor(test) {
a.test=test;
}
}
index.js:
require('./a.js');
var b = new a(5);
as you can see I create a simple class that it's constructor gets a parameter. and in my include i require that class and create a new object based on that class. pretty simple.. but still i'm getting the following error:
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/ufk/work-projects/bingo/server/bingo-tiny/index.js:1:63)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
any ideas why ?
Or you can run like this:
node --use_strict index.js
i'm still confused about why 'use strict' is needed, but this is the code that works:
index.js:
"use strict";
var a = require('./a.js');
var b = new a(5);
a.js:
"use strict";
class a {
constructor(test) {
a.test=test;
}
}
module.exports=a;

Can't update elements which are inside the array

Let's say I have this
userinfo={
userDetails:
{
username:"",
password:"",
cookie:"",
firstname:"",
lastname:"",
phonenumber:"",
postalcode:"",
country:""
},
applicationsInfo:[
{
application:"",
consumerKey:"",
accessToken:""
}
]
}
First I created user and latter I am to update applicationsInfo section when user creates an application. First I tried this way and It works
var consumerKey="asdyfsatfdtyafydsahyadsy";
var findCon={"userDetails.username":"someName"};
db.find(findCon,function(err,docs){
if(err){
console.log(err);
}else{
var updateCon={$set:{"applicationsInfo.0.consumerKey":consumerKey}};
db.update(findCon,updateCon,{},function(err,docs){
console.log(docs);
});
}
});
But actually what I want is update some selected one I tried that in this way.
........
var appNum=0;
var updateCon={$set:{"applicationsInfo."+appNum+".consumerKey":consumerKey}};
then I start my node server then I got error like this.
/home/jobs/nodeserver/routes/initusers.js:180
"applicationsInfo."+appNum+
^
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 Module.require (module.js:364:17)
at require (module.js:380:17)
You need to set it the below way:
var appNum = 0;
var updateCon = {$set:{}};
updateCon.$set["applicationsInfo."+appNum+".consumerKey"] = 1;
Setting an expression ("applicationsInfo."+appNum+".consumerKey") as key of an object during initialization is not allowed in java script.

Node.js crashing when connecting to AWS S3

After migrating my application from IntoVPS to Digital Ocean, my application crashes when it tries to upload photos to AWS S3.
The differences between the servers are: IntoVPS was running Ubuntu 10.10, and Digital Ocean is running Debian; IntoVPS had node 0.8.x, and Digital Ocean has 0.10.26.
There are no firewalls on the new server that could be causing this issue (I've checked).
The crash errors are:
Error: read ECONNRESET
at errnoException (net.js:904:11)
at Pipe.onread (net.js:558:19)
Error: spawn ENOENT
at errnoException (child_process.js:988:11)
at Process.ChildProcess._handle.onexit (child_process.js:779:34)
Error: write EPIPE
at errnoException (net.js:904:11)
at Object.afterWrite (net.js:720:19)
Some of these might be related to forever trying to restart the process (I figured that's what spawn might mean). The one I'd focus on is the ECONNRESET one.
So, I'm running my process using forever, and I'm using the knox module to connect to S3.
After googling I found: https://github.com/LearnBoost/knox/issues/198
I tried adding res.resume() to the callback of putFile like it says in the, but nothing changes; I still get the ECONNRESET error.
I've spent an entire day (yesterday) trying to fix this issue, and I can't continue having my production application be broken, so I decided to try switching to an older version of Node in order to temporarily (but quickly) fix this issue. So, I installed the n module to try and get 0.8.26 as the node version installed. Unfortunately, n didn't work, and here's the issue I created on that: https://github.com/visionmedia/n/issues/170
EDIT:
After exiting my ssh session and opening a new one, n is working. However, switching to 0.8.26 version of node causes another errors:
/apps/Foobar/node_modules/dnode/node_modules/weak/node_modules/bindings/bindings.js:83
throw e
^
Error: Module version mismatch, refusing to load.
at Object.Module._extensions..node (module.js:485:11)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at bindings (/apps/Foobar/node_modules/dnode/node_modules/weak/node_modules/bindings/bindings.js:76:44)
at Object.<anonymous> (/apps/Foobar/node_modules/dnode/node_modules/weak/lib/weak.js:1:97)
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.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/apps/Foobar/node_modules/dnode/index.js:5:12)
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.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/apps/Foobar/controllers/sock.js:2:13)
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.require (module.js:362:17)
at require (module.js:378:17)
at global.Controller (/apps/Foobar/globals.js:2:9)
at Object.<anonymous> (/apps/Foobar/controllers/generosity.js:209:12)
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.require (module.js:362:17)
at require (module.js:378:17)
at global.Controller (/apps/Foobar/globals.js:2:9)
at Object.<anonymous> (/apps/Foobar/app.js:282:1)
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:245:9)
As you can see I'm having the darnedest time trying to fix this issue with S3, and I'm to the point of crying (metaphorically). I can't keep wasting time on this issue, but I can't seem to solve it. It's almost like Node itself is broken.
Can anyone provide any insight on what's going on? Any help with this is appreciated.
Why isn't knox working anymore?
EDIT
Here's more details.
Code that errors:
// S3
var endpoint = "https://s3.amazonaws.com/"+global.config.aws.s3.bucket+"/";
var knox = require('knox');
var s3 = knox.createClient(global.config.aws.s3);
var photosPrefix = 'photos/';
var model = module.exports;
model.emitter = new events.EventEmitter;
// ::Photos
model.createPhoto = function(info, cb){
if (!info.file || path.resolve(info.file) === path.resolve(global.__tempdir))
return cb(new Error('missing info.file'));
var db = info.db;
var photoId;
if (typeof info.public === 'undefined')
info.public = false;
// Move file to a specific location
var basename = path.basename(info.file);
var destination = photosPrefix+basename+'/original';
yarn
(function(){
// Upload photo to AWS
s3.putFile(
info.file,
destination,
{
'Content-Type': 'image/png'
},
this());
})
(function(err, res){
if (err) return this.error(new Error(err));
// Remove temporary file
fs.unlink(info.file, this());
})
(function(err){
if (err) return this.error(new Error(err));
// Retrieve photo stream from AWS
s3.getFile(destination, this());
})
(function(err, res){
if (err) return this.error(new Error(err));
// Get the dimensions of the photo using the stream (res)
gm(res).size(this());
})
(function(err, size){
if (err) return this.error(new Error(err));
// Insert record into photos table (include the dimensions, width and height)
db.query("INSERT photos (resource, width, height, public) VALUES (?, ?, ?, ?)", [basename, size.width, size.height, info.public], this());
})
(function(err, results){
if (err) return this.error(new Error(err));
photoId = results.insertId;
cb(null, photoId);
})
.error(function(err){
cb(err);
});
}
All photos no matter what the size or format cause the same error.
I found that the issue wasn't with AWS and knox, but rather with gm and the fact that GraphicsMagick wasn't installed on my system.
For reference for others, if you're experiencing similar errors, remember that gm doesn't report that GraphicsMagick or ImageMagick are absent when they are absent. I reported this error on the gm module here: https://github.com/aheckmann/gm/issues/273

Function and module.exports error issue NODE.js

:)
I have a somewhat easy answer for you guys to answer as you always do.
Im new at functions and whatnot, iv watched some tutorials about exporting your functions to another node.js application,
Im attempting to generate some random numbers for a external module.
this is what i have setup.
(index.js file)
function randNumb(topnumber) {
var randnumber=Math.floor(Math.random()*topnumber)
}
module.exports.randNumb();
(run.js)
var index = require("./run.js");
console.log(randnumber);
Well My Issue is when i run the index.js file, i get this error from the console.
TypeError: Object #<Object> has no method 'randNumb'
at Object.<anonymous> (C:\Users\Christopher Allen\Desktop\Node Dev Essential
s - Random Number\index.js:8:16)
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 ran the run.js in the beginning, this is what i got.
ReferenceError: randNumb is not defined
at Object.<anonymous> (C:\Users\Christopher Allen\Desktop\Node Dev Essential
s - Random Number\run.js:3:1)
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)
In the randNum function, don't forget to:
return randnumber;
Also in index.js, export the function like so:
exports.randNumb = randNumb;
Invoke it like this in run.js:
console.log(randNumber(10));
You didn't export the var at all.
Your index.js should be :
function randNumb(topnumber) {
return Math.floor(Math.random()*topnumber)
}
module.exports.randnumber = randNumb(10); //replace 10 with any other number...
run.js should be :
var index = require("./run.js");
console.log(index.randnumber);

Resources