error while connecting sqlite database file using node js - node.js

I am new to Node.Js and have written the below code to connect to the SQLite db file. But unfortunately getting error-ed out. please help
Node - 4.6.0:
const sqlite3 = require('sqlite3').verbose();
let db =new sqlite3.Database('./db/program', sqlite3.OPEN_READWRITE,(err)
if (err) { console.error(err.message); }
console.log('Connected to the database.');});
Error message:
let db = new sqlite3.Database('./db/program', sqlite3.OPEN_READWRITE, (err) => {
^^^
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: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 Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3

As you're using Node 4.6.0 it doesn't support ES6 syntax out of the box. You need to use the strict mode and write code.let is supported out of the box after Node v6.0.0.
So, maybe you can upgrade Node.JS or you can just add,
"use strict";
in the 1st line of the code. It should work.
Check ES6 compatibility here: https://node.green/#ES2015-bindings-let
Learn more about Strict mode: https://www.w3schools.com/js/js_strict.asp

Related

Primordials is not defined

I'm trying to execute this code but I don't know why it shows error. I'm new at Nodejs, so i attach the code and ss of error please help how to fix this
var webshot = require('webshot');
var flatiron = require('flatiron');
var app = flatiron.app;
app.use(flatiron.plugins.http);
app.router.get('/getImage', function() {
var self = this;
var requestUrl = this.req.headers['head'];
console.log(requestUrl);
webshot(requestUrl, function(err, renderStream) {
renderStream.pipe(self.res);
});
});
app.start(3000,"IP Address");
console.log('Starting Node Server');
error =
Debugger attached.
Waiting for the debugger to disconnect...
fs.js:45
} = primordials;
^
ReferenceError: primordials is not defined
at fs.js:45:5
at req_ (c:\Users\HOME\Desktop\NodeScripts\node_modules\natives\index.js:143:24)
at Object.req [as require] (c:\Users\HOME\Desktop\NodeScripts\node_modules\natives\index.js:55:10)
at Object.<anonymous> (c:\Users\HOME\Desktop\NodeScripts\node_modules\graceful-fs\fs.js:1:37)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
Process exited with code 1
First note encountering this kind of issue is not common when using Node.js. The packages webshot and flatiron haven't been maintained for 5 years at the time of writing, which is the primary reason they're incompatible with the current version of Node.js.
Typically when this error is encountered, you either need to use different packages, or downgrade to an older version of Node.js. According to the comments here, you should be able to downgrade to Node.js v11, but I highly suggest exploring more recently maintained packages like capture-website and express in order to stick with the latest LTS version of Node.js.

Error in p-limit lib when importing #google-cloud/storage to deploy firebase function

I am trying to deploy a firebase function with the command firebase deploy, but when I import #google-cloud/storage
const { Storage } = require('#google-cloud/storage');
I get this error
Detailed stack trace: /srv/node_modules/p-limit/index.js:30
} catch {}
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/node_modules/#google-cloud/storage/build/src/bucket.js:25:16)
The problem is that I found this error in p-limit/index.js in the following code
const run = async (fn, resolve, ...args) => {
activeCount++;
// TODO: Get rid of `pTry`. It's not needed anymore.
const result = pTry(fn, ...args);
resolve(result);
try {
await result;
} catch {}
next();
};
But if I edit this part of the code and deploy again, I get the same error, so it seems that it's not reading this file.. it's getting this error in some other place that I cant find..
Someone can help?
What version of node are you running? I had the same exact same message when I tried using it today and has been driving me crazy. I was using Node 8.12. Try upgrading to the latest version of node 12.18.1. It resolved that error for me.
I had this error with a node app (not firebase related though). Looks #google-cloud/storage starting from its version 5.0 dropped support for Node 8.
One option is to upgrade to Node versions >=10. My project though required to use Node 8. So another option is to use #google-cloud/storage prior to 5.0. For example, this worked for me in package.json with Node 8
...
"#google-cloud/storage": "5.0.0",
...

PM2 - SyntaxError: Block-scoped declarations not yet supported outside strict mode

I'm currently installing a NodeJS service on an Ubuntu server. The application is written in coffeescript and runs fine on it's own. This is how I setup the project:
cd ~/test-project
nvm use v4.9.1
npm install
pm2 start index.js
However when I try to start it using pm2 i get the error below:
/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/node.js:132
let val = process.env[key];
^^^
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: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 Object.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/index.js:9:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
And after doing some testing it seems that this error occurs no matter what script I start with pm2 so there must be something that isn't configured correctly..
I don't know if it matters but the script I am trying to run needs node v4.9.1 so I am using NVM to use that version, it seems to me that it is trying to run pm2 with that same node version somehow?
Any input is greatly appreciated as I've been stuck on this for way too long!!
I had the same issue on node version 4.x,
Downgraded the pm2 and it worked.
npm uninstall -g pm2
npm install -g pm2#3.0.4
'use strict';
Add above line of code to first line of the script file. let is used when you want a variable to be scoped to a block which is supported in strict mode in node v4.
See the documentation for more details.
#Vishnu Sing this is the complete output. The index.js file looks like this:
console.log('Hello World');
ubuntu#ip-172-31-32-5:~/test-project$ nano index.js
ubuntu#ip-172-31-32-5:~/test-project$ node -v
v10.15.3
ubuntu#ip-172-31-32-5:~/test-project$ sudo pm2 start index.js
/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/node.js:132
let val = process.env[key];
^^^
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: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 Object.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/index.js:9:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
ubuntu#ip-172-31-32-5:~/test-project$

Adding gulp-sass breaks by task runner

I have a basic gulp setup in VS2017 to minify my Javascript. I decided to add gulp-sass (my package.json says I'm on gulp-sass v4.0.1) but it throws this error:
C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:66
let sassMap;
^^^
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:373:25)
at Object.Module._extensions..js (module.js:404: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 Object.<anonymous> (C:\Work\MyProject\MyProject\gulpfile.js:11:12)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
My gulpfile looks like this:
var gulp = require('gulp');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var watch = require('gulp-watch');
var uglify = require('gulp-uglify');
var sass = require('gulp-sass');
gulp.task('minify', function () {
gulp.src('src/**/*.js')
.pipe(uglify({ mangle: false }))
.pipe(concat('scripts.min.js'))
.pipe(gulp.dest('Content'));
});
gulp.task('sass', function () {
gulp.src('src/css/**/*.scss')
.pipe(sass({ outputStyle: 'compressed' }))
.pipe(gulp.dest('Content'));
});
gulp.task('watch', function () {
gulp.watch('src/**/*.js', ['minify']);
});
I did some Googling and a simple fix suggested was to add "use strict" to the top of the offending file, in this case index.js:66. However, after doing that I get:
Failed to run "C:\Work\MyProject\MyProject\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
C:\Work\MyProject\MyProject\node_modules\node-sass\lib\binding.js:15
throw new Error(errors.missingBinary());
^
Error: Missing binding C:\Work\MyProject\MyProject\node_modules\node-sass\vendor\win32-x64-47\binding.node
Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 5.x
Found bindings for the following environments:
- Windows 64-bit with Node.js 6.x
This usually happens because your environment has changed since running `npm install`.
Run `npm rebuild node-sass --force` to build the binding for your current environment.
at module.exports (C:\Work\MyProject\MyProject\node_modules\node-sass\lib\binding.js:15:13)
at Object.<anonymous> (C:\Work\MyProject\MyProject\node_modules\node-sass\lib\index.js:14:35)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404: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 Object.<anonymous> (C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:163:21)
at Module._compile (module.js:397:26)
I am running Node.js v6. I'm lost as to why what should be a simple process is giving me these errors. What am I doing wrong?
Update:
I ran the following commands suggested in the comments:
npm install node-sass -f
npm rebuild nose-sass
Both ran successfully. However, I'm still getting this error:
Failed to run "C:\Work\MyProject\MyProject\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:66
let sassMap;
^^^
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:373:25)
at Object.Module._extensions..js (module.js:404: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 Object.<anonymous> (C:\Work\MyProject\MyProject\gulpfile.js:8:12)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
Update 2:
I was advised to add "use strict"; to the top of my gulpfile.js, but the same error occurs. Here's the file contents:
"use strict";
var gulp = require('gulp');
var sass = require('gulp-sass'); // If I comment this out, I can build
gulp.task('sass', function () {
gulp.src('src/css/**/*.scss')
.pipe(sass({ outputStyle: 'compressed' }))
.pipe(gulp.dest('Content'));
});
Most common issue online appears to be Node.js version of < 6.0, but I'm running v6.11.1.
Update 3: (solved)
I finally found the cause & solution; I've added it as an answer down below for any future readers. Enjoy.
Managed to find the problem so I'm answering my own question for future readers.
Whilst I have node.js v6.11.1 installed, Visual Studio 2017 comes bundled with it's own version of node that it uses by default. Even if you run node -v in the VS2017 shell and it tells you it's running v6.11.1, it's actually - by default - running whatever it finds in .\node_mobules\.bin.
The solution is this:
In VS2017, go "Tools > Options > Projects and Solutions > Web Package Management > External Web Tools".
You'll probably see this:
Add the path to your standalone installation of node (default C:\Program Files\nodejs) and, using the arrows, position it above the .\node_modules\bin version, like this:
Hit OK and either refresh the Task Runner Explorer or restart VS2017. Your gulpfile should now build.
In my case, I had to move $(PATH) above $(VSINSTALLDIR)/Web/External to fix the problem.

nodejs SyntaxError: Unexpected token

I am using elasticsearch-exporter to export data from Elasticsearch.
The tool initially is a nodejs application.
When I try to use the following command node exporter.js to make the tool list all the available options, it crashes with the following exception
/home/me/storage/Elasticsearch-Exporter/log.js:54
exports.error = (...args) => !capture("ERROR", args) && console.log(timestamp() + util.format(...args).red);
^^^
SyntaxError: Unexpected token ...
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/anas/storage/Elasticsearch-Exporter/exporter.js:9:11)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
here is the line where the exception is thrown
exports.error = (...args) => !capture("ERROR", args) && console.log(timestamp() + util.format(...args).red);
I think the error is related to a different version of nodejs but I am not sure.
Here is the output of the node --version command v4.2.6
Here is the output of the npm --version command 3.10.6
Yes, indeed, ... is called the spread operator and is only available since Node.js 6
The elasticsearch-exporter project declares in its package.json file that it only works with node version > 6
So since you're running Node.js 4.2.6, you either need to upgrade your Node.js installation or fork the elasticsearch-exporter project and modify it to work with Node.js 4.2.6.

Resources