Running a JavaScript program requiring jsfeat - javascript

I have downloaded jsfeat from here, and unzipped the downloaded file.
Inside the directory, I wrote the following:
require("./build/jsfeat.js");
var matrix = new jsfeat.matrix_t(2,2, jsfeat.U8_t | jsfeat.C1_t);
matrix.data[1] = 4;
console.log(matrix.data[1]);
I ran the program as follows:
$ node matrix.js
But, got the following error:
/Users/abc/Desktop/JavaScript/inspirit-jsfeat-59cc928/matrix.js:2
var matrix = new jsfeat.matrix_t(2,2, jsfeat.U8_t | jsfeat.C1_t);
^
ReferenceError: jsfeat is not defined
at Object.<anonymous> (/Users/abc/Desktop/JavaScript/inspirit-jsfeat-59cc928/matrix.js:2:19)
at Module._compile (module.js:425:26)
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 Function.Module.runMain (module.js:457:10)
at startup (node.js:136:18)
at node.js:972:3
Why is that? How can I solve the issue?
Thanks.

You need to assign the imported module to a variable:
var jsfeat = require("./build/jsfeat.js");
If you prefer you could install the module with NPM to save downloading it manually:
$ npm install jsfeat
The module will be saved to ./node_modules/jsfeat. You can then require it with var jsfeat = require('jsfeat').

Related

Is there a way to get a variable or function from one file to another file in javascript?

I have a file for my Blockchain called gale.js, and I need the Blockchain variable to get into my miner.js file:
let galeCoin = new Blockchain(); // My Blockchain
Can I access this variable from my miner.js file so I can run the minePendingTransactions() function from the Blockchain class
var galeCoin = /* the coin imported from the gale.js file */
var minerAdress = "0xOTUYboIUYEO5C274OUIYGo8isYOCYW6o87TO"; // SHA256 Hash for the miner
while (true) {
minePendingTransactions(minerAdress); // mine (forever)
}
I would also like to access this variable in my user.js file:
var galeCoin = /* the coin imported from the gale.js file */
var userAdress = galeCoin.newAdress(); // A new adress for the user
// I will add the code here later...
And many other files!
But I can't seen to access the variable from the other files!
I tried this:
let galeCoin = new Blockchain();
export { galeCoin };
import { galeCoin } from './gale.js'
but that gives me this error when I run miner.js using node miner.js:
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module 'gale.js'
Require stack:
- /Users/Movies/Desktop/devProjects/Blockchain/SecondTry/miner.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/Movies/Desktop/devProjects/Blockchain/SecondTry/miner.js:1:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/Movies/Desktop/devProjects/Blockchain/SecondTry/miner.js' ]
}
I dont understand the error message
I need help as soon as posible!
NOTE: My files are in /Users/Movies/Desktop/devProjects/Blockchain/SecondTry/
Try using the following to import galeCoin from gale.js:
var {galeCoin} = require('./gale.js');
You'll also need to export the galeCoin object from gale.js. You can use the following (append somewhere at the end of gale.js):
module.exports = {galeCoin};
Then you're going to want to run npm install gale.js to ensure that module is added to the node_modules directory. For this you're going to want to make sure the node_modules is in the same director as miner.js. If it is not, npm -g install gale.js.
I should note, I'm assuming you're using node.js, which from the error you threw, seems to be the case.
I hope this was helpful:)
Try :
export let galeCoin = new Blockchain();
//add export statement at the beginning of the line where you declare your variable
You can read about it here: https://javascript.info/import-export

DateFormat.js SyntaxError: Unexpected token export

Using this with some Kubernetes PODS and one of the pods out of the blue gives me this error. It seems to come from the call on line 6. Have not changed anything was just doing a deployment on gitlab and noticed this POD said CrashLoopBackOff.
/app/config/auth.js line 6
var dateFormat = require('dateformat');
My nodemodules folder has the latest dateFormat.js from package.json
So not sure what the problem is. Does someone know how to fix it?
var token=/d{1,4}|D{3,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|W{1,2}|[LlopSZN]|"[^"]*"|'[^']*'/g;var timezone=/\b(?:[A-Z]{1,3}[A-Z][TC])(?:[-+]\d{4})?|((?:Australian )?(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time)\b/g;var timezoneClip=/[^-+\dA-Z]/g;export default function dateFormat(date,mask,utc,gmt){if(arguments.length===1&&typeof date==="string"&&!/\d/.test(date)){mask=date;date=undefined}date=date||date===0?date:new Date;if(!(date instanceof Date)){date=new Date(date)}if(isNaN(date)){throw TypeError("Invalid date")}mask=String(masks[mask]||mask||masks["default"]);var maskSlice=mask.slice(0,4);if(maskSlice==="UTC:"||maskSlice==="GMT:"){mask=mask.slice(4);utc=true;if(maskSlice==="GMT:"){gmt=true}}var _=function _(){return utc?"getUTC":"get"};var _d=function d(){return date[_()+"Date"]()};var D=function D(){return date[_()+"Day"]()};var _m=function m(){return date[_()+"Month"]()};var y=function y(){return date[_()+"FullYear"]
SyntaxError: Unexpected token export
at Module._compile (internal/modules/cjs/loader.js:760:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Module.require (internal/modules/cjs/loader.js:723:19)
at require (internal/modules/cjs/helpers.js:14:16)
at Object.<anonymous> (/app/config/auth.js:6:18)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Module.require (internal/modules/cjs/loader.js:723:19)
at require (internal/modules/cjs/helpers.js:14:16)
at Object.<anonymous> (/app/config/passport.js:8:18)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)```
Was able to fix it by downloading an older version of dateformat. Nothing wrong with my code on the question its a simple problem with the library not the code come on.
Here I could make it work by using another version of this package,was working in the version 4.6.3, so I did:
npm i dateformat#4.6.3

Cannot access child_process API

I have the following tiny program.js which tries to execute a binary file:
var childProcess = require('child_process');
var path2Binary = '/home/myuser/myproj/bins/mybin';
var par = '--file=' + '/home/myuser/myproj/files/myfile.txt';
var ret = childProcess.execFileSync(path2Binary, [par]);
if (!ret) throw 'Error invoking process!';
var cnt = ret.stdout;
if (!cnt) throw 'Error retrieving output!';
console.log(cnt);
The program tries to execute a binary file and passes it a parameter (a file). The output of this process will be then displayed.
I try to run this: node program.js, but get the following
var ret = childProcess.execFileSync(path2Binary, [par]);
^
TypeError: Object #<Object> has no method 'execFileSync'
at Object.<anonymous> (/home/myuser/myproj/program.js:6:24)
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:929:3
More information
I am running on CentOS, Node version is v0.10.36.
I tried running sudo yum install nodejs, but it tells me it is already installed so Node installation looks kinda good.
What's the problem?
On a side note...
If I replace childProcess.execFileSync with childProcess.spawn I get the same.
If I change the first line into the following:
var exec = require('child_process').execFileSync;
Then I get an undefined exception on exec.
Synchronous child processes aren't supported in node v0.10.36 - https://nodejs.org/docs/v0.10.36/api/child_process.html
Looks like it may have been introduced in 0.12.

Reading files name that begin with DOT "."

i have this simple nodeJS script for reading files from a PATH and check if they are directory or not :
var fs = require("fs");
var allFiles = fs.readdirSync(__dirname + '/bb');
allFiles.forEach(function(name){
if(fs.lstatSync(name).isDirectory())
{
console.log(name);
}
});
The problem is when I check if the file is DIRECTORY .isDirectory() and the file begins with a DOT "."
fs.js:679
return binding.lstat(pathModule._makeLong(path));
^
Error: ENOENT, no such file or directory 'D:\test\.ggg'
at Object.fs.lstatSync (fs.js:679:18)
at D:\test\server.js:4:11
at Array.forEach (native)
at Object.<anonymous> (D:\test\server.js:3:10)
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)
If I don't check if DIRECTORY or the file name don't begin with a DOT "." it works just fine.
OS : Windows 7
You are calling .lstatSync with the wrong path.
Try path.join instead to create the correct path.
The snippet should look like
var fs = require("fs");
var allFiles = fs.readdirSync(__dirname + '/bb');
allFiles.forEach(function(name){
if(fs.lstatSync(path.join("bb",name)/*Correct path*/).isDirectory())
{
console.log(name);
}
});

How do I get html-snapshots to work?

I'm just trying to run html-snapshots. This should be easy, right?
This is what I started with:
npm install html-snapshots
That's all I need, right? Here's my snapshots.js file:
var htmlSnapshots = require('html-snapshots');
htmlSnapshots.run({
source: "sitemap.xml",
hostname: "localhost",
outputDir: "snapshots",
outputDirClean: true,
selector: "#results-widget"
});
And to run it:
node snapshots.js
But nooo:
module.js:340
throw err;
^
Error: Cannot find module '.\robots'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.module.exports.create (C:\webdev\node_modules\html-snapshots\lib\input-generators\index.js:38:16)
at Object.module.exports.run (C:\webdev\node_modules\html-snapshots\lib\html-snapshots.js:42:39)
at Object.<anonymous> (C:\webdev\snapshots.js:2:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
wtf?
Additional Info...
This is part of html-snapshots.js:
var inputFactory = require("./input-generators");
...
run: function(options, listener) {
...
var inputGenerator = inputFactory.create(options.input);
...
result = inputGenerator.run(options, (function(options, notifier){
Also, the html-snapshots\lib\input-generators folder contains the file robots.js
It looks like an issue inside html-snapshots\lib\input-generators\index.js file - it works fine on Linux systems but fails on Windows (path.sep has been used to build module name)
Problem is that it should load './robots' module instead of '.\robots'.
Quick fix is to update html-snapshots\lib\input-generators\index.js file - line 38.
Change line:
result = require(file);
to:
result = require(path.join(__dirname, file));
And it will work fine. I hope that will help.

Categories