nodejs deconstruction of require

Viewed 112

I'm trying to deconstruct a require assignment of express along with importing the main export itself:

const express, {static: expressStatic, json, urlencoded} = require("express");

However it fails:

const express, {static: expressStatic, json, urlencoded} = require("express");
      ^^^^^^^

SyntaxError: Missing initializer in const declaration

What's the right way to do it?

2 Answers

You cannot perform this in one line.

const express = require("express");
const {static: expressStatic, json, urlencoded} = express;

Destructure in JS

If you want to do this in a single statement, you can use

const express = require("express"), {static: expressStatic, json, urlencoded} = express;
Related