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

467
Vistas
Property 'subtle' does not exist on type 'typeof webcrypto'

I'm using Node v17.4 and want to consume the webcrypto API. Based on this example I'm trying to import subtle into my project but TypeScript comes up with the error

Property 'subtle' does not exist on type 'typeof webcrypto'.

The only thing the namespace webcrypto offers is this.

import crypto from 'crypto';
const { subtle } = crypto.webcrypto; // subtle doesn't exist

How do I get access to subtle ?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

As you noted, @types/node includes only a stub for the webcrypto interface. One workaround is to extend the crypto module declaration to declare webcrypto.subtle as SubtleCrypto from the DOM type definitions:

// src/types/index.d.ts

declare module "crypto" {
  namespace webcrypto {
    const subtle: SubtleCrypto;
  }
}

This allows writing a module like:

// src/digest.ts:

import * as crypto from "crypto";

// subtle has type SubtleCrypto, from the DOM type definitions
const { subtle } = crypto.webcrypto;

// Generate a cryptographic digest of the given message
export async function digest(message?: string, algorithm = "SHA-512") {
  const encoder = new TextEncoder();
  const data = encoder.encode(message);
  const hash = await subtle.digest(algorithm, data);
  return hash;
}

To use SubtleCrypto, the project must enable the DOM type definitions by adding dom to the lib array in tsconfig.json. For example, with the following packages installed:

$ npm install -D typescript@4.6 @types/node@17 @tsconfig/node17

and a tsconfig.json containing:

{
  "extends": "@tsconfig/node17/tsconfig.json",
  "compilerOptions": {
    "lib": ["dom"],
    "outDir": "dist",
    "typeRoots": ["./node_modules/@types", "./src/types"]
  },
  "include": ["src/**/*"],
  "exclude": ["dist", "node_modules"]
}

you can write an entrypoint that calls digest and prints the result:

// src/index.ts

import { digest } from "./digest";

const message = "Hello, World!";

(async () => {
  const digestBuffer = await digest(message);
  console.log(Buffer.from(new Uint8Array(digestBuffer)).toString("hex"));
})();

This builds and runs, like:

$ npx tsc && node dist/index.js
374d794a95cdcfd8b35993185fef9ba368f160d8daf432d08ba9f1ed1e5abe6cc69291e0fa2fe0006a52570ef18c19def4e617c33ce52ef0a6e5fbe318cb0387
over 4 years ago · Santiago Trujillo 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