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

374
Visualizações
Typescript: import { groupBy } from 'lodash' ReferenceError: require is not defined

I'm trying to use groupBy from lodash in a .ts file like this:

import { groupBy } from 'lodash';

But after tsc .\index.ts -w the resulting index.js turns that import into var lodash_1 = require("lodash"); and the following error appears:

index.js:28 Uncaught ReferenceError: require is not defined

Here is my package.json:

{
  "name": "final_exercise",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "lodash-es": "^4.17.21",
    "require": "^0.4.4",
    "typescript-require": "^0.3.0"
  }

The error happens with and without require and typescript-require being installed. Installing require needed the npm audit fix --force, not sure if that's normal or not.

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2019",
    "module": "commonjs",
    "lib": ["ES2019", "dom", "dom.iterable"],
    "strict": true,
    "sourceMap": true,
  }
}

I also have this on the end of index.html, else the error would be ReferenceError: exports is not defined:

<script> var exports = {}; </script>
<script src="index.js"></script>

Since I'm new to Typescript, to modules and everything associated, I must be doing something wrong or I'm missing something. I've checked all similar questions with answers but nothing so far has worked.

Thanks for any help.

about 4 years ago · Santiago Trujillo
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