Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!