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

120
Vistas
what's the difference between import "name" from "module" and import * as "name" from "module"?

The code on my app is like this.

import { HttpExceptionFilter } from './common/exceptions/http-exception.filter';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { DocumentBuilder, OpenAPIObject, SwaggerModule } from '@nestjs/swagger';
//1.
import * as expressBasicAuth from 'express-basic-auth';
//2. 
//import expressBasicAuth from 'express-basic-auth'; => promise unhandled error

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.useGlobalPipes(new ValidationPipe()); 
  app.useGlobalFilters(new HttpExceptionFilter());

  app.use(
    ['/docs', '/docs-json'],
    expressBasicAuth({
      challenge: true,
      users: {
        [process.env.SWAGGER_USER]: process.env.SWAGGER_PASSWORD,
      },
    }),
  );
};

I want to know the difference between 1 and 2.

Because When I run my app using 2(9 lines), there was promise unhandled error.

I want to know how those two ways work. I referred to the official document of mozilla, but I couldn't understand it well. I'd appreciate it if you could answer me.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

First let's just understand how imports works

so consider i have a file with the following exports

// file name -> modules
export const add = (x, y) => x + y;
export const subtract = (x, y) => x - y;
export default (x, y) => x * y;

now i can import all these functions on another file like this

import defaultFunction, { add, subtract } from './modules.js';

console.log(add(1, 2)) // 3
console.log(subtract(2, 1)) // 1
console.log(defaultFunction(2, 2)) // 2 * 2 = 4

or i can use import * as <name> from <file/package> to import all the exports inside a file.

like this


import * as myModules from './modules.js';
// NOTE: this will only give you add and subtract functions but not the default one
// now to actually access the default function you have to use
// import * as <name> from <file/package>
// <name>.default to access the default export
// or simply
// import default, { } from <file/package>


console.log(myModules.add(1, 2)) // 3
console.log(myModules.subtract(2, 1)) // 1
console.log(myModules.default(2, 2)) // 2 * 2 = 4

// or i can destructure the "myModules" import

const { add, subtract } = myModules;

Conclusion

I think the problem is that the library that you're using is not providing a default export to actually use import expressBasicAuth from 'express-basic-auth'; or it might be exporting different functions for named exports and default exports

about 4 years ago · Juan Pablo Isaza Denunciar

0

As per your code:

  1. import * as expressBasicAuth from ...

    Imports all export const variable1...; export const variable2...; items as a single item.

  2. import expressBasicAuth from ...

    Imports a single export default item if there was one defined inside the library.

Depending on the library implementation, there might be totally different functions exported in those two ways.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Most of the time, the module exports multiple things There are two great ways to import from another module when the module exports an object with properties. This is the common case.

Import the whole module, giving it a name:

import * as child_process from "child_process";

// then later...
child_process.spawn(...);
or pick the names you want to import:

import { spawn } from "child_process";

// then later
spawn(...);

sometimes the module exports just one thing. typescript has the concept of export default If a module declares a default export, then you must bring it in like this:

import thing from "thing";

Now you have a function or a class (whatever its default export is) in the thing.

More commonly in JavaScript (CommonJS?) modules, a module author will override module.exports to a function or class instead of adding properties to the exports object like a ES-module would.

so in common js we have

const expressBasicAuth = require(express-basic-auth);

and in typescript :

import * as expressBasicAuth from 'express-basic-auth';
about 4 years ago · Juan Pablo Isaza 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