return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - CommonJS alternative to ES6's importing as alias - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CommonJS alternative to ES6's importing as alias - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

CommonJS 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')
发布评论

评论列表(0)

  1. 暂无评论