TypeError: generateKeyPairSync not a function - javascript

I am trying to use the generateKeyPairSync() function of the crypto module of node.js. But am getting a typerError saying that there is no such function.
Its strange because another function of the same module createCipher() works perfectly!
Here is the sample code:
import * as cryp from 'crypto';
export function generate_keys() {
console.log('hey there!');
const keyPair = cryp.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret'
}
});
console.log(keyPair);
}
function encrypt(data: string) {
let cipher = cryp.createCipher('aes-256-ecb', 'pass');
cipher.update(data, 'utf8');
return cipher.final('hex');
}
// This is working
console.log(encrypt('asa'));
// Getting error - 'generateKeyPairSync is not a function'
generate_keys();
This is the complete console output:
[Running] ts-node "d:\projects\myProject\src\ply.ts"
4eb35bc94bd4d17fad4857b17558b2ad
hey there!
d:\projects\myProject\src\ply.ts:5
const keyPair = cryp.generateKeyPairSync('rsa', {
^
TypeError: cryp.generateKeyPairSync is not a function
at generate_keys (d:\projects\myProject\src\ply.ts:5:24)
at Object.<anonymous> (d:\projects\myProject\src\ply.ts:33:1)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Module.m._compile (C:\Users\anmolsingh.jaggi\AppData\Local\Yarn\Data\global\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\anmolsingh.jaggi\AppData\Local\Yarn\Data\global\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
[Done] exited with code=1 in 2.097 seconds

From the docs:
Added in: v10.12.0
I guess you are running an earlier version of node

Related

Discord.js v12: Error: The class/prototype returned from the extender function must extend the existing structure class/prototype

I am trying to set up a event handler for my bot. After I add the code and start the bot it outputs the error:
Error:
Error: The class/prototype returned from the extender function must extend the existing structure class/prototype (received function ExtendedTextChannel extends TextChannel; expected extension of ExtendedTextChannel).
at Function.extend (/home/runner/ricebot/node_modules/discord.js/src/util/Structures.js:82:13)
at module.exports (/home/runner/ricebot/node_modules/discord-buttons/src/index.js:24:16)
at Object.<anonymous> (/home/runner/ricebot/index.js:35:27)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/runner/ricebot/events/ready.js:1:16)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at /home/runner/ricebot/index.js:119:18
Code:
readdirSync("./events/").forEach((file) => {
const events = readdirSync("./events/").filter((file) =>
file.endsWith(".js")
);
for(let file of events) {
let pull = require(`./events/${file}`);
if(pull.name) {
client.events.set(pull.name, pull);
} else {
continue;
}
}
});
The error log leads me to this line 35
require('discord-buttons')(client); and to this line 119 let pull = require(`./events/${file}`)
This really seems confusing to me. Thanks in advance.

Why is console.log showing properties of error?

class AppError extends Error {
constructor(cause, name, ...args) {
const message = args.map(x => x.toString()).join(" ")
super(message)
this.name = name
Error.captureStackTrace(this, AppError)
}
}
throw new AppError(null, "name", "my", "message")
When I throw the error, the output looks like this:
throw new AppError(null, "name", "my", "message")
^
name: my message
at Object.<anonymous> (/home/xxx/test.js:21:7)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11 {
name: 'name'
}
Why is it showing {name: 'name'}?
But when I throw a regular Error object it looks like this:
throw new Error("message")
^
Error: message
at Object.<anonymous> (/home/xxx/test.js:21:7)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
It doesn't show {name: "name or something"}
I am using node ./test.js to run this file.
Your AppError assigned a name property to the instance:
this.name = name
You aren't assigning anything to the plain old Error though. If you do assign such a property, you'll be able to see it:
const err = new Error('errormessage');
err.name = 'someerrorname';
throw err;
results in
someerrorname: errormessage
at Object.<anonymous> (D:\Javascript\...\foo.js:1:13)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11 {
name: 'someerrorname'
}
(Otherwise, what comes before the : will default to Error)
The same sort of thing is true for front-end:
const err = new Error('errormessage');
err.name = 'someerrorname';
throw err;
Uncaught someerrorname: errormessage
(without assigning to err.name, you get Uncaught Error instead)
If you want to remove the {name: 'name'}, make the property non-enumerable:
Object.defineProperty(this, 'name', { value: name, enumerable: false });
When logging an object, enumerable properties will be listed on it.
(You can also omit the enumerable: false if you want, since it defaults to false anyway)

How to fix TypeError: document.getElementById is not a function when running unit tests over files that use leaflet.js objects?

I have the exact same problem that is in this post How can you unit test Leaflet JS maps?
I was unable to comment or add additional questions to this post so I apologize for the duplicate question.
GLOBAL.window = { screen: {} };
GLOBAL.document = {
documentElement: {
style: {}
},
getElementsByTagName: function () { return []; },
createElement: function () { return {}; }
};
GLOBAL.navigator = {
userAgent: 'nodejs',
platform: 'nodejs'
};
GLOBAL.L = require('leaflet');
const assert = require('assert');
const config = require('../js/config.js')
config.js has the following in it.
const openstreetmap = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
});
const map = L.map('map', {
center: [39, -105],
zoom: 4,
defaultExtentControl: true,
zoomControl: false,
layers: [openstreetmap]
});
I type mocha and get the following error:
TypeError: document.getElementById is not a function
at get (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:2247:43)
at NewClass._initContainer (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:4094:37)
at NewClass.initialize (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:3131:8)
at new NewClass (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:301:20)
at Object.createMap [as map] (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:4718:9)
at Object. (C:\dev\apps\APP\resources\js\config.js:12:15)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object. (C:\dev\apps\APP\resources\test\config_test.js:45:16)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:334:36
at Array.forEach ()
at Mocha.loadFiles (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:331:14)
at Mocha.run (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:811:10)
at Object.exports.singleRun (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\run-helpers.js:108:16)
at exports.runMocha (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\run-helpers.js:142:13)
at Object.exports.handler.argv [as handler] (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\run.js:292:3)
at Object.runCommand (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\yargs.js:1104:24)
at Object.parse (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\cli.js:68:6)
at Object. (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\bin\mocha:154:29)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

vm.runInContext: Why can't this function access a global object that should be in the context?

Using Node v10.13.0, I don't understand why this code errors out instead of printing "Bill":
const vm = require('vm');
function printName() {
console.log(defaultName);
}
const sandbox = { defaultName: 'Bill', printName: printName };
vm.createContext(sandbox);
vm.runInContext('printName()', sandbox);
When I run it, I get this:
$ node ./eval-js.js
/Users/wiseman/src/typescript-eval-test/eval-js.js:4
console.log(defaultName);
^
ReferenceError: defaultName is not defined
at printName (/Users/wiseman/src/typescript-eval-test/eval-js.js:4:17)
at evalmachine.<anonymous>:1:1
at Script.runInContext (vm.js:107:20)
at Object.runInContext (vm.js:285:6)
at Object.<anonymous> (/Users/wiseman/src/typescript-eval-test/eval-js.js:9:4)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)

Mocha can't find modules in imported code

I have a Phoenix app and am trying to get Mocha wired up for javascript unit tests. I want to test module 'Two', that imports module 'One' but I can't figure out how to configure Mocha to find module one.
Here is the test code:
web/static/js/one.js
export var One = 1;
web/static/js/two.js
import {One} from "web/static/js/one";
export var Two = function () {return One + One;}
> test/js/two_test.js
import assert from 'assert';
import {Two} from "../../web/static/js/two";
describe('Two()', function() {
it('returns value 2', function () {
assert.equal(2, Two());
});
});
Here is the output when I run npm test
home:~/elixir/optitrue$ npm test
> # test /home/jon/elixir/optitrue
> mocha --compilers js:babel-register test/js/**/*.js
module.js:457
throw err;
^
Error: Cannot find module 'web/static/js/one'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (two.js:1:1)
at Module._compile (module.js:556:32)
at loader (/home/jon/elixir/optitrue/node_modules/babel-register/lib/node.js:148:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/jon/elixir/optitrue/node_modules/babel-register/lib/node.js:158:7)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (two_test.js:2:1)
at Module._compile (module.js:556:32)
at loader (/home/jon/elixir/optitrue/node_modules/babel-register/lib/node.js:148:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/jon/elixir/optitrue/node_modules/babel-register/lib/node.js:158:7)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at /home/jon/elixir/optitrue/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (/home/jon/elixir/optitrue/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/home/jon/elixir/optitrue/node_modules/mocha/lib/mocha.js:485:10)
at Object.<anonymous> (/home/jon/elixir/optitrue/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
brunch.config
exports.config = {
// See http://brunch.io/#documentation for docs.
files: {
javascripts: {
joinTo: "js/app.js"
},
stylesheets: {
joinTo: "css/app.css"
},
templates: {
joinTo: "js/app.js"
}
},
conventions: {
assets: /^(web\/static\/assets)/
},
// Phoenix paths configuration
paths: {
watched: [
"web/static",
"test/static"
],
// Where to compile files to
public: "priv/static"
},
// Configure your plugins
plugins: {
babel: {
// Do not use ES6 compiler in vendor code
ignore: [/web\/static\/vendor/]
}
},
modules: {
autoRequire: {
"js/app.js": ["web/static/js/app"]
}
},
npm: {
enabled: true,
whitelist: ["phoenix", "phoenix_html"]
}
};
The error is quite clear:
Error: Cannot find module 'web/static/js/one'
Meaning the path you provided doesn't work, try using a relative paths:
import {One} from "./one";

Categories

Resources