0

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?

Harrison Cramer
  • 3,792
  • 9
  • 33
  • 61
  • Possible duplicate of [ES6/ES2015 object destructuring and changing target variable](https://stackoverflow.com/questions/34904523/es6-es2015-object-destructuring-and-changing-target-variable) – jonrsharpe Oct 06 '18 at 20:34

2 Answers2

4

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;

dvsoukup
  • 1,586
  • 15
  • 31
0

You can use curly braces and import the exact name of the module

const {join, dirname} = require("path")