Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

386
Visualizações
How do I perform an export that is compatible with ES5 and ES6?

I'm writing a "class" in node

// mymodule/index.js

function MyClass() {}
MyClass.prototype.method1 = function() {..}

usually I do

module.exports = MyClass

but I want my class available for both syntax

var MyClass = require('mymodule')

and

import {MyClass} from 'mymodule'

Which is the correct way to do it?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

As far as writing an export that is compatible for both ES5 and ES6, Babel already takes care of that for you. (As communicated in the comments to your question. I'm only clarifying for those who got lost in the dialog.)

module.exports = MyClass

will work with both var MyClass = require('mymodule') and import MyClass from 'mymodule

However, to be clear, the actual syntax you asked about:

import {MyClass} from 'mymodule'

means something different from

import MyClass from 'mymodule'

For the latter, you would have to export it as: module.exports.MyClass = MyClass, and for ES5 modules it would have to required as var MyClass = require('mymodule').MyClass

over 4 years ago · Santiago Trujillo Relatório

0

Both ways are correct, but try to import in es6 like this without the brackets:

import MyClass from 'mymodule'

Otherwise you would have to export your function like this:

module.exports.MyClass = MyClass

and than import it like this:

import { MyClass } from 'mymodule'
over 4 years ago · Santiago Trujillo Relatório

0

From the comments, I understand you are trying to run your ES6 frontend code in some mocha unit tests in node. Yes, you can't do that until node support ES6 modules. If I were you, I would use systemjs to load code for those mocha tests. Mocha supports promises, so it should be fairly painless to load any files before tests.

Writing syntax for both will just create more problems for you.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda