class Match {
JSDOM = jsdom;
Discord = require("discord.js");
jsdom = require("jsdom");
…
}
This is the code I use, and I get the error:
JSDOM = jsdom();
^
SyntaxError: Unexpected token =
Why? This should work right? This only happens when I try to run the code on repl.it. When I run it on my computer it works fine.
So for anyone wondering, I found the problem. I was importing the project from GitHub, when I made a new REPL and specified node.js as the language, then imported the files, everything worked as expected.
Related
I was building a website with Fastify, and recently updated it to the latest version. After that, I converted my code from CommonJS to ES6. Once I did that, I suddenly ran into an error that I did not encounter before.
TypeError: func.toString is not a function
at Object.getFuncPreview (/home/aditya/Development/launchbase/node_modules/.pnpm/fastify#4.7.0/node_modules/fastify/lib/pluginUtils.js:44:15)
at Boot.override (/home/aditya/Development/launchbase/node_modules/.pnpm/fastify#4.7.0/node_modules/fastify/lib/pluginOverride.js:52:78)
at Plugin.exec (/home/aditya/Development/launchbase/node_modules/.pnpm/avvio#8.2.0/node_modules/avvio/plugin.js:79:33)
at Boot.loadPlugin (/home/aditya/Development/launchbase/node_modules/.pnpm/avvio#8.2.0/node_modules/avvio/plugin.js:272:10)
at /home/aditya/Development/launchbase/node_modules/.pnpm/avvio#8.2.0/node_modules/avvio/plugin.js:262:18
I am not able to find the exact source of the issue, for the error message. However, I found all the instances of using `toString' in my code.
const { randomBytes, scryptSync } = await import('crypto');
const salt = randomBytes(16).toString('hex');
const hashedStr = scryptSync(str, salt, 64).toString('hex');
Can someone help me debug this issue?
You should import the functions with:
const { randomBytes, scryptSync } = require('crypto');
I was testing a very simple module to see if it works and keep on getting an error stating that node cannot find my module.
//mymodule.js
greeting = 'hello'
module.exports = greeting;
// main.js
const s = require('exportsPractice\mymodule.js');
console.log(s);
The error I get is shown here
Try using:
const s = require('./exportsPractice/mymodule.js');
And make sure the path is correct.
I'm creating a blog, using this 'Web Dev Simplified' tutorial:
https://www.youtube.com/watch?v=1NrHkjlWVhM
I've copied the code from git hub https://github.com/WebDevSimplified/Markdown-Blog, installed the node modules and linked it to my mongodb database online.
Node Modules include;
express, mongoose, ejs, --save-dev nodemon, slugify, method-override, dompurify, jsdom.
The database was working and I could save articles, until I added the last part about sanitizing HTML and converting markdown to HTML, this is when the 'TypeError: marked is not a function' comes up, and the save button ceases to work.
Seems a once understood function is now not understood because of a more recent node module dependency, either the dompurify library or jsdom. I'm really out of my depth here! please help!
From Marked Documentation:
https://marked.js.org/#demo
Node JS
import { marked } from 'marked';
// or const { marked } = require('marked');
const html = marked.parse('# Marked in Node.js\n\nRendered by **marked**.');
Your Code:
if (this.markdown) {
this.sanitizedHtml = dompurify.sanitize(marked(this.markdown))
}
try this:
if (this.markdown) {
this.sanitizedHtml = dompurify.sanitize(marked.parse(this.markdown))
}
its work for me
In my case:
const { marked } = require('marked');
instead of
const marked = require('marked')
...
this.sanitizedHTML = dompurify.sanitize(marked.parse(this.markdown))
Per node example documentation at https://marked.js.org/#demo
What I need:
Read a local JSON file inside NuxtJS as the page loads. So I can parse it into a prop within <option> tag.
What I have:
Lowdb (installed as dependency — to read the JSON file) inside a component, with this code:
computed: {
resultFetchCamera: function() {
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('db.json');
const db = low(adapter);
let value = db.get('Size').map('Name').value();
return value;
}
}
}
I got an error This dependency was not found: * fs in ./node_modules/lowdb/adapters/FileSync.js. Fixed with this solution. Which leads me to another error: TypeError: fs.existsSync is not a function. This solution helps out a bit but it also leads to other errors: TypeError: window.require is not a function and TypeError: FileSync is not a constructor. So, I undo the last solution and get back with the fs.existsSync error.
The question:
How to fix the fs.existsSync error (in a NuxtJS environment)?
Did I implement Lowdb to NuxtJS correctly?
I'm building an Electron app (with Ionic) to digitally sign PDFs with p12 certificates using node-signpdf (https://www.npmjs.com/package/node-signpdf). I've had problems which I solved by using require ('electron').remote for example for fs.
I've also installed the same node version in my OS (MacOS Catalina) as the same one that electron is using (Node v12.4.0).
The problem is that one of the NPM dependencies uses crypto and it shows as undefined with the next error:
HomePage.html:39 ERROR TypeError: _crypto.randomBytes is not a function
at Object.ctx.seedFileSync (prng.js:340)
at _reseedSync (prng.js:210)
at Object.ctx.generateSync (prng.js:163)
at Object.ctx.generate (prng.js:80)
at Object.ctx.getBytes (random.js:92)
at _modPow (rsa.js:431)
at Object.push../node_modules/node-forge/lib/rsa.js.pki.rsa.encrypt (rsa.js:501)
at Object.key.sign (rsa.js:1245)
at addSignerInfos (pkcs7.js:534)
at Object.sign (pkcs7.js:377)
What I can see is that node-signpdf uses node-forge as a dependency, and node-forge loads crypto inside prng.js this way:
var _crypto = null;
if(forge.util.isNodejs && !forge.options.usePureJavaScript &&
!process.versions['node-webkit']) {
_crypto = require('crypto');
}
I've tried changing that part of the code to use crypto-js or browserfy-crypto (this last one doesn't even build and hasn't been updated in years), but I keep getting the error shown above.
EDIT 1:
This is how I'm implementing the signature in my service:
public signFile(pathToFile: string, pathToCert: string): void {
const fs = (<any>window).require('fs');
let certBuffer = fs.readFileSync(pathToCert);
let fileBuffer = fs.readFileSync(pathToFile);
fileBuffer = plainAddPlaceholder({
pdfBuffer: fileBuffer,
reason: 'I have reviewed it.',
signatureLength: 1612,
});
const signedPdf = signer.sign(fileBuffer, certBuffer, {passphrase: 'qwertyui'});
}
The code adds the placeholder to add the signature, the problem comes in signer.sign
EDIT 2:
When I run the app if I type in console: require ('crypto') I see the methods, so it looks like it's loaded in the global scope, the problem seems to be in the NPM dependency of node-forge.
EDIT 3:
I've changed require('crypto') to window.require('crypto') and started working. But I think I'll have to make postinstall script to modify it.
Is there a better way?
-
How can I make this crypto thing work? I ran out of ideas. Maybe you can spare some?
Thanks for your time!