[英]CommonJS alternative to ES6's importing as alias
ES6 can import an export as alias , like so; ES6 可以像这样导入导出为 alias ;
import express from 'express'
import { express as playground } from 'graphql-playground/middleware'
Is there an alternative way to do this with CommonJS require('something')
?有没有其他方法可以用 CommonJS
require('something')
做到这require('something')
? Or something that circumvents the above declaration issue if it were done the CommonJS way?或者,如果以 CommonJS 的方式完成,可以绕过上述声明问题?
This throws an error.这会引发错误。
const express = require('express')
const express = require('graphql-playground/middleware')
// SyntaxError: Identifier 'express' has already been declared
CommonJS is really just assigning values to variables and you can name the variables however you want: CommonJS 实际上只是给变量赋值,你可以随意命名变量:
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:对于非默认导出(module.export = var),您还可以使用常规解构语法进行别名:
const {
originName: newNameInFile
} = require('foo.js')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.