proper way of using es6 classes in a nodejs project - node.js

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;

Related

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object

For
const systemRegex = /^system\./,
endOfLine = require('os').EOL,
EJSON = require('mongodb-extjson');
I got error at the line EJSON = require('mongodb-extjson'), details is as follows:
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received function hidden
at Function.from (buffer.js:331:9)
at fnv1a32 (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/fnv1a.js:21:25)
at fnv1a24 (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/fnv1a.js:39:18)
at Object.<anonymous> (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/objectid.js:14:20)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/bson.js:7:14)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19) {
code: 'ERR_INVALID_ARG_TYPE'
}
How to resolve it?
It is because of the Node.js version.
Node.js v10.15.1: Works!
Node.js v12.8.1: Error loading the module
I am not sure about the cause, but I would guess that it is because Node.js' packaging mechanism has undergone a change. And mongodb-extjson has not been modified to adhere to the new mechanism. As mentioned in their GitHub repository, it is unlikely to be fixed as it is no longer being maintained.
You have the following choices:
Downgrade to v10 of Node.js, OR
Use the recommended bson module instead:
$ npm install bson
const { EJSON } = require('bson');
const data = '{ "someId": { "$oid": "5ec7cb151a1878fbefce4119" } }'
const doc = EJSON.parse(data, { relaxed: false });
console.log(doc.someId._bsontype)
// Should print 'ObjectID'
Note the difference in the EJSON option between the two modules. Whereas in mongodb-extjson the default is strict, in bson the default is relaxed.

SyntaxError: Unexpected reserved word sql-cli

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

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

TypeScript / NodeJS: variable not defined, using internal ref paths for separate files

I have been testing TypeScript with node, all was going well until i tried splitting these out.
Am I forced to use modules ?
I have 2 files, app.ts which has a reference path to the hellofile.tst
/// <reference path="hellofile.ts" />
var testme = new Hello()
console.log(testme.testMe());
and the hellofile.ts which contains
class Hello {
testMe():string {
return "hello";
}
}
Now running the program (i am using webstorm), I get the following error.
/usr/local/bin/node app.js
/
Users/tst/WebstormProjects/NodeJsWithTypescript/app.js:2
var testme = new Hello();
^
ReferenceError: Hello is not defined
at Object.<anonymous> (/Users/tst/WebstormProjects/NodeJsWithTypescript/app.js:2:18)
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:906:3
Process finished with exit code 8
You don't have to use modules. You can compile with --out flag
But external modules with commonjs is highly recommended if targeting nodejs : http://m.youtube.com/watch?v=KDrWLMUY0R0

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