Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
Make use of the TextEncoder class in Node and Browser environments

I want to create a library using Typescript. This library can be used inside Node and browser environments, so the configuration supplies support for both

( tsconfig.json )

{
  "compilerOptions": {
    "baseUrl": ".",
    "declaration": true,
    "esModuleInterop": true,
    "lib": [ "esnext", "dom" ],
    "module": "commonjs",
    "outDir": "dist",
    "resolveJsonModule": true,
    "strict": true,
    "target": "es2019",
  },
  "include": [
    "./**/*.ts"
  ],
  "exclude": [
    "./dist"
  ]
}

I'm using esbuild as a bundler. The package.json contains

{
  "name": "my-lib",
  "version": "1.0.0",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "build": "tsc && esbuild ./dist/index.js --bundle --minify --sourcemap --outfile=./bundle/index.js"
  },
  "dependencies": {
    "esbuild": "0.14.36"
  },
  "devDependencies": {
    "typescript": "4.6.3"
  }
}

The library makes use of some "private" helper functions, I'm using the following sample code

import { TextEncoder } from 'util';

const encodeInput = function (input: string): Uint8Array {
  const textEncoder = new TextEncoder();

  return textEncoder.encode(input);
}

Running the build script command throws an esbuild error telling me that this only works for Node environments, not in the browser, which makes sense because

  • this class comes from the util package https://nodejs.org/api/util.html#class-utiltextencoder
  • this class does not require any imports inside browser environments https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder

How can I make sure this library works in "both" worlds?

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

TextEncoder is a global in Node, so you don't need to import it. Use it directly, without the import statement, just like in the browser.

You also don't need to instantiate a new TextEncoder every time the function is invoked: it is a small performance optimization to instantiate it once, and then just alias the encode method. See below.

const encoder = new TextEncoder();
const encode = encoder.encode.bind(encoder);

console.log(encode('hello world'));

about 4 years ago · Santiago Gelvez Denunciar

0

You can make use of the UMD (Universal Module Definition) tool to get around this problem.

In your tsconfig.json, just change module: "commonjs", to module: "umd", and it should build properly from there to work in all environments.

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda