Can you export multiple classes from a single Nodejs Module?

You can export multiple classes like this: e.g. People.js class Jack{ //Member variables, functions, etc } class John{ //Member variables, functions, etc } module.exports = { Jack : Jack, John : John } And access these classes as you have correctly mentioned: var People = require(‘./People.js’); var JackInstance = new People.Jack(); var JohnInstance = new … Read more

What is the use of module.parent in node.js? How can I refer to the require()ing module?

The “parent” is the module that caused the script to be interpreted (and cached), if any: // $ node foo.js console.log(module.parent); // `null` // require(‘./foo’) console.log(module.parent); // `{ … }` What you’re expecting is the “caller,” which Node doesn’t retain for you. For that, you’ll need the exported function you’re currently using to be a … Read more

How to use ESM tests with jest?

Here are the steps I took to run Jest with a test using ESM. The source files under test were also written using ESM. Set my node version to 14.16.0 Install Jest: npm i jest -D Add “type”: “module” to package.json Update test script in package.json: “scripts”: { “test”: “node –experimental-vm-modules ./node_modules/.bin/jest” } Create a … Read more

node: could not initialize ICU (check NODE_ICU_DATA or –icu-data-dir parameters)

You need to run npm install including the full-icu package. It’s full-icu‘s postinstall step which downloads the appropriate bits for the currently executing node. Note that multiple files may show up in the full-icu directory, that’s OK. If you already had full-icu installed, but upgraded Node.js in between: npm rebuild fixes the issue.

how to use async await with https post request

Basically, you can write a function which will return a Promise and then you can use async/await with that function. Please see below: const https = require(‘https’) const data = JSON.stringify({ todo: ‘Buy the milk’ }); const options = { hostname: ‘flaviocopes.com’, port: 443, path: ‘/todos’, method: ‘POST’, headers: { ‘Content-Type’: ‘application/json’, ‘Content-Length’: data.length }, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)