ES6 can import an export as alias, like so;
import express from 'express'
import { express as playground } from 'graphql-playground/middleware'
Is there an alternative way to do this with CommonJS require('something')
? Or something that circumvents the above declaration issue if it were done the CommonJS way?
This throws an error.
const express = require('express')
const express = require('graphql-playground/middleware')
// SyntaxError: Identifier 'express' has already been declared
ES6 can import an export as alias, like so;
import express from 'express'
import { express as playground } from 'graphql-playground/middleware'
Is there an alternative way to do this with CommonJS require('something')
? Or something that circumvents the above declaration issue if it were done the CommonJS way?
This throws an error.
const express = require('express')
const express = require('graphql-playground/middleware')
// SyntaxError: Identifier 'express' has already been declared
Share
Improve this question
edited Oct 13, 2017 at 20:51
Yliaho
asked Oct 13, 2017 at 20:34
YliahoYliaho
1275 silver badges10 bronze badges
3
- Can't you just assign it to a variable with a different name? You haven't shown a CommonJS example, so it's hard to give suggestions of what to change. – loganfsmyth Commented Oct 13, 2017 at 20:37
-
1
Like
const express = require('express'); const playground = require('graphql-playground/middleware').express;
? Not sure understand your problem. – Felix Kling Commented Oct 13, 2017 at 20:41 - Yes @FelixKling, that's exactly what I was needing. Thank you, and sorry for the poor explanation – Yliaho Commented Oct 13, 2017 at 20:43
2 Answers
Reset to default 4CommonJS is really just assigning values to variables and you can name the variables however you want:
const express = require('express');
const playground = require('graphql-playground/middleware').express;
For non-default exports (module.export = var) you can also alias with regular destructure syntax:
const {
originName: newNameInFile
} = require('foo.js')