Require dependency of another dependency in node modules

Do I just have to include mongoose as a dependency as well in the parent app or is there a way of getting access to that module by way of the child? While it’s possible for you to e.g. require(‘github/node_modules/mongoose’), the standard practice is to install all of your dependencies explicitly (i.e., you should include … Read more

How can I use paths in tsconfig.json?

This can be set up on your tsconfig.json file, as it is a TypeScript feature. You can do like this: “compilerOptions”: { “baseUrl”: “src”, // This must be specified if “paths” is. … “paths”: { “@app/*”: [“app/*”], “@config/*”: [“app/_config/*”], “@environment/*”: [“environments/*”], “@shared/*”: [“app/_shared/*”], “@helpers/*”: [“helpers/*”] }, … Have in mind that the path, where you … Read more

How can I update Node.js and NPM to their latest versions?

Use: npm update -g npm See the documentation for the update command: npm update [-g] [<pkg>…] This command will update all the packages listed to the latest version (specified by the tag config), respecting semver. Additionally, see the documentation on Node.js and NPM installation and Upgrading NPM. The following original answer is from the old … Read more

What is the purpose of the ‘node_modules’ folder?

What is the purpose of node_modules folder? You can think of the node_modules folder like a cache for the external modules that your project depends upon. When you npm install them, they are downloaded from the web and copied into the node_modules folder and Node.js is trained to look for them there when you import … Read more

Mixed default and named exports in Node with ES5 syntax

You want to assign the value of module.exports to be your default function, and then put all the named exports as properties on that function. const defaultFunction = () => { console.log(‘default!’); }; const namedFunction1 = () => { console.log(‘1!’); }; const namedFunction2 = () => { console.log(‘2!’); }; const myModule = module.exports = defaultFunction; … Read more

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