I would like to import the following modules in NodeJS
const path = require("path");
const { join } = require("path");
const getDirName = require("path").dirname;
How can I consolidate these imports into a single require statement?
I would like to import the following modules in NodeJS
const path = require("path");
const { join } = require("path");
const getDirName = require("path").dirname;
How can I consolidate these imports into a single require statement?
Any reason why you're importing path three times? Just import it once and use it from there. If you need to use the join
function, just invoke it via path.join()
, or use path.dirname()
Otherwise, do it like so:
const path = require('path');
const { join, dirname } = path;
You can use curly braces and import the exact name of the module
const {join, dirname} = require("path")