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

352
Visualizações
How to load JSON Web Tokens (JWTs) in my javascript file

I'm in custom module for Drupal, and i need to use JWT in javascript.

So i tried npm i jsonwebtoken and import jwt from "../node_modules/jsonwebtoken/index.js"; on my js/index.js

But i have the following error : Uncaught SyntaxError: The requested module '../node_modules/jsonwebtoken/index.js' does not provide an export named 'default'

I dont understand what to do and how i can load my library jsonwebtoken.

Sorry i'm really a beginner in javascript.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you import node modules you don't enter the relative path. You only put the name of the module your trying to import. Also the module dosnt have a default module which means you need to import everything into one variable or import specific functions.

Javascript

If you use javascript you should use require instead of import. The example below is taken from their docs.

// reference https://www.npmjs.com/package/jsonwebtoken

var jwt = require('jsonwebtoken');
var token = jwt.sign('payload', 'secret');

Typescript

// quick and dirty

import * as jwt from 'jsonwebtoken';

// This is the way you should do it

import {sign, decode, JsonWebTokenError, TokenExpiredError, verify, NotBeforeError} from 'jsonwebtoken'

I created an example.ts file and run the following commands.

npm i -s jsonwebtoken && npm i -D @types/jsonwebtoken

So my structure is somthing like this

  • project
    • node_modules
    • package.json
    • package-lock.json
    • example.ts

Then I added those 2 lines to get the result below

import {sign} from 'jsonwebtoken';

sign('foo-bar', 'secret', undefined, ((err, res) => console.log(res)));

Console: eyJhbGciOiJIUzI1NiJ9.Zm9vLWJhcg.o2WiQrAIplX3gvloUOTaxCTlvmsWhFwxk9rh6xHoFs8

about 4 years ago · Juan Pablo Isaza 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