Unexpected token = - node.js

I'm unsure as to what could cause this error in Node.js, as I've never seen it before and cannot find another issue online.
Message:
Unexpected token =
Stack:
SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:404:25)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Projects/api/test/integration/models/article.js:3:15)
The file that is causing the error has the following contents:
'use strict';
var Article = require('../../../models/article')
Why in the world would = cause an error?
Edit 1 - adding the article.js that is being required:
'use strict';
class ArticleModel {
constructor(options = {}) {
this.options = options
}
}
module.exports = ArticleModel

node.js 5.0 does not support all ES6 features yet. In particular, it does not yet support default parameters.
So this line:
constructor(options = {}) {
is what is causing the error with the = assignment.
See this table for which features are supported in node.js 5.0.
You can replace the default parameter assignment with the old fashioned method:
constructor(options) {
this.options = options || {};
}

I think, your current Node.js distribution doesn't support default parameter values.
You should remove it:
constructor(options) {
this.options = options || {};
}
Or, try to play with --harmony runtime flag.
According to this link --harmony can not to help, this feature doesn't implemented in node5.0 at all.

I am using Node v5.7.0 and can enable default parameters using this option:
--harmony-default-parameters

The error is on the 3rd line of article.js.

Related

googleapis-common throwing error in UUID dependency

I'm trying to get a very basic oauth example to work in a node.js app with express and googleapis. Upon running the application it throws a TypeError inside the UUID dependency which is included with the googleapis-common module. I'm getting a bit frustrated at this point because I have not been able to find any additional information about this to allow me to resolve it myself.
Take a look at the screenshot below for the specifics:
Here it is in text if that makes things easier:
Exception has occurred: TypeError: Cannot assign to read only property 'name' of function 'function generateUUID(value, namespace, buf, offset) {
if (typeof value === 'string') {
value = strin...<omitted>... }'
at _default (C:\Users\ficar\OneDrive\Desktop\Frontend\node_modules\googleapis-common\node_modules\uuid\dist\v35.js:71:23)
at Object.<anonymous> (C:\Users\ficar\OneDrive\Desktop\Frontend\node_modules\googleapis-common\node_modules\uuid\dist\v3.js:14:27)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (C:\Users\ficar\OneDrive\Desktop\Frontend\node_modules\googleapis-common\node_modules\uuid\dist\index.js:63:34)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
The file this is being thrown in is called "v35.js".
My initial thought is that I must be missing some additional library that interprets the logic throwing the error differently. Eager to learn more about this and find a resolution.
Looks like this is how the uuid module works
node_modules/uuid/dist/v35.js
function _default(name, version, hashfunc) {
function generateUUID(value, namespace, buf, offset) {
...
} // Function#name is not settable on some platforms (#270)
try {
generateUUID.name = name; // eslint-disable-next-line no-empty
} catch (err) {} // For CommonJS default export support
...
Authors warn (comment on line 4), that name property may not be settable and bypass it with empty catch

Gulp-Sourcemaps, SyntaxError: Unexpected token >

Gulp / npm noobie here.
I'm trying to use gulp-sourcemaps, and for some reason, it crashes on var sourcemaps = require('sourcemaps').(It crash only when this line's in the file)
gulpfile:
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
gulp.task('generateApp', function () {
return gulp.src([some paths...])
.pipe(sourcemaps.init())
.pipe(concat('app.min.js'))
.pipe(uglify())
.pipe(sourcemaps.write())
.pipe(gulp.dest(path...));
});
Error :
C:\Projets\node_modules\strip-bom\index.js:2
module.exports = x => {
^
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)
at Object.<anonymous> (C:\Projets\node_modules\gulp-sourcemaps\src\init.js:10:14)
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)
Has anyone encounter this type of error?
I tried to google it, without any success.
I just started getting the same error and fixed it by replacing the code in C:\Projects\node_modules\strip-bom\index.js with this:
'use strict';
module.exports = function (x) {
if (typeof x !== 'string') {
throw new TypeError('Expected a string, got ' + typeof x);
}
// Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
// conversion translates it to FEFF (UTF-16 BOM)
if (x.charCodeAt(0) === 0xFEFF) {
return x.slice(1);
}
return x;
};
Then, I had to run npm rebuild node-sass to get it to work again. It seems to be an issue with an older version of the Strip-bom node module.
For more info, check this out: https://github.com/sindresorhus/strip-bom/commit/e2a3c3b83706ee5baac284f3862d3f6b9e1564e5
UPDATED ANSWER:
This error is caused by using an older version of Node. The Strip-bom module is now using ES2015 (ES6) syntax which requires Node 5.0+. (See Node's ES2015 support list here)
To test your version of Node, run:
node -v
If it's less than 5.0, you'll need to update it. You can download the newest version of Node here:
https://nodejs.org/en/
After installing the new version of Node, I still needed to run npm rebuild node-sass to get Gulp up and running again.
The former answer will still work if you don't want to update your Node version, however, as Louis pointed out, manually editing node module files is not a best-practice.

'use-strict' enabled but not working in node

I have enabled use-strict mode in my .js file but when I run it, node keeps telling me that I don't have it enabled. PLEASE don't tell me to write "use-strict"; at the top of my file because I already tried that.
Here is my server.js file. I have been trying to see what is wrong but so far stack overflow has not been much help since most people seem to get this working on their first try.
require('use-strict')
'use-strict';
let util = require('util');
let http = require('http');
let Bot = require('#kikinteractive/kik');
var kik_username = process.env.KIK_USERNAME;
var kik_api_key = process.env.KIK_API_KEY;
var kik_baseUrl = process.env.KIK_BASEURL;
// Configure the bot API endpoint, details for your bot
let bot = new Bot({
username: kik_username,
apiKey: kik_api_key,
baseUrl: kik_baseUrl
});
bot.updateBotConfiguration();
bot.onTextMessage((message) => {
message.reply(message.body);
});
// Set up your server and start listening
let server = http.createServer(bot.incoming()).listen(8085);
Everything seems fine but when I run
$ node server.js
I keep getting this error
let util = require('util');
^^^
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:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:148:18)
at node.js:405:3
It tells me to enable strict mode BUT I ALREADY DID THAT. I even required an npm package to make sure I was doing it right! Can anyone make sense of what is happening?
No dash in 'use strict'
'use strict' // not 'use-strict'
Check out the documentation for further reference
You don't need to require an npm package. just put "use strict"; at the top of the js file.

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;

Node.js with ExpressJS error: Cannot read property 'prototype' of undefined

Running node.js v0.10.2 and express v3.1.1 (latest at this time) and getting this error:
/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:10
window.XMLHttpRequest.prototype.withCredentials = false;
^
TypeError: Cannot read property 'prototype' of undefined
at create (/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:10:26)
at /root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:9503:18
at Object.<anonymous> (/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:9505:2)
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> (/root/dmr-addresses/address/log.js:1:71)
line 1 of log.js is:
var $ = require('jquery');
I've tried running npm install jquery but it has not fixed the problem.
Check this:
Same error here...
I don't know what I'm doing, but I changed the node-jquery.js fourth-fifth row's and it's start working :)
old:
if(window == null ) {
window = require('jsdom').jsdom().createWindow();
new:
if(!window || !window.document) {
window = require('jsdom').createWindow();
window.document = require('jsdom').jsdom();
You don't actually have a prototype object in Node server code, it's all stored in the much nicer __proto__ object and you should be using Object.create/defineProperty.
What exactly are you trying to do? Run an ajax query with Node? If so, you should be using Nodes http.request
An example could be:
require('request').post({
"uri" : "http://example.com/",
"headers" : {
'content-type': 'application/json'
},
"body" : "hello=world"
},
function(e,r,b){
// e = errors, r = response and b = returned body
console.log(b,r.statusCode));
});
Looks like this is an issue with the jsdom module that node-jquery depends on. It appears that this is a known issue, and that it has been fixed, but not published to npm yet.
Check it out: https://github.com/coolaj86/node-jquery/issues/52

Resources