How to fix Namespace must be imported or aliased using 'import as' in TypeScript

TypeScriptINTERMEDIATEMEDIUMUpdated Dec 11, 2025ยท 103 views

This TypeScript compilation error occurs when trying to use a namespace without proper import syntax. Namespaces in TypeScript require explicit import statements using 'import as' syntax or namespace qualifiers to access their contents.

Quick fix

Fixes Namespace must be imported or aliased using 'import as'

// Incorrect - will cause error
import { SomeFunction } from './my-namespace';

// Correct - use 'import as' for namespaces
import * as MyNamespace from './my-namespace';
// Then access with MyNamespace.SomeFunction

// Alternative: import specific members with alias
import { SomeFunction as MyFunction } from './my-namespace';
  1. Add proper 'import as' syntax for namespace imports
  2. Use namespace qualifiers for internal references
  3. Check your tsconfig.json module settings
  4. Convert namespaces to ES6 modules if possible
typescript โ€” error
// Incorrect - will cause error
import { SomeFunction } from './my-namespace';
// Correct - use 'import as' for namespaces
import * as MyNamespace from './my-namespace';
// Then access with MyNamespace.SomeFunction
// Alternative: import specific members with alias
import { SomeFunction as MyFunction } from './my-namespace';
Namespace must be imported or aliased using 'import as'

What this error means

This error occurs when you attempt to use a TypeScript namespace without properly importing or referencing it. TypeScript namespaces are a way to organize code and prevent naming collisions, but they require specific syntax to access their contents. When you define a namespace like `namespace MyNamespace { export const value = 1; }`, you cannot directly reference `value` without either: 1. Importing the namespace with `import as` syntax 2. Using the namespace qualifier `MyNamespace.value` The error message "Namespace must be imported or aliased using 'import as'" is TypeScript's way of telling you that you're trying to use something from a namespace without following the proper access rules. This is different from ES6 modules, which use different import/export syntax.

Typical symptoms

  • TypeScript compilation fails with error TS2693
  • Error message: 'Namespace must be imported or aliased using 'import as''
  • Code works at runtime but fails during TypeScript type checking
  • IDE shows red squiggly lines under namespace references
  • Build process fails but JavaScript execution might work
  • Error occurs when migrating from JavaScript to TypeScript

Common causes

  1. Directly referencing namespace members without proper import syntax
  2. Mixing namespace syntax with ES6 module imports incorrectly
  3. Missing 'import as' statement for namespace imports
  4. Using triple-slash references (/// <reference>) without proper configuration
  5. Trying to use global namespace declarations in module files
  6. Incorrect tsconfig.json configuration for module resolution

How to fix "Namespace must be imported or aliased using 'import as'"

1Add proper 'import as' syntax for namespace imports

When importing from a namespace, use the import as syntax:

typescript
// Incorrect - will cause error
import { SomeFunction } from './my-namespace';

// Correct - use 'import as' for namespaces
import * as MyNamespace from './my-namespace';
// Then access with MyNamespace.SomeFunction

// Alternative: import specific members with alias
import { SomeFunction as MyFunction } from './my-namespace';

This tells TypeScript you're importing a namespace module rather than an ES6 module.

2Use namespace qualifiers for internal references

If you're referencing members within the same file but different namespace, use the namespace qualifier:

typescript
namespace MyNamespace {
  export const value = 42;
}

// Incorrect - direct reference
const x = value; // Error!

// Correct - use namespace qualifier
const x = MyNamespace.value;

// Also correct - import with 'import as' even in same file
import MyAlias = MyNamespace;
const y = MyAlias.value;

This applies even when the namespace is in the same file.

3Check your tsconfig.json module settings

Ensure your TypeScript configuration supports namespace syntax:

json
{
  "compilerOptions": {
    "module": "commonjs", // or "amd", "system", "umd"
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}

Namespaces work best with CommonJS or AMD module systems. If using ES6 modules ("module": "es2015" or higher), consider converting namespaces to ES6 modules.

4Convert namespaces to ES6 modules if possible

Modern TypeScript projects should prefer ES6 modules over namespaces. Convert:

typescript
// Old namespace style
namespace Utilities {
  export function helper() { /* ... */ }
}

// New ES6 module style
// utilities.ts
export function helper() { /* ... */ }

// consumer.ts
import { helper } from './utilities';

ES6 modules are standardized, tree-shakeable, and work better with modern bundlers like Webpack and Rollup.

5Use triple-slash references for global namespaces

For global namespace declarations (like in .d.ts files), use triple-slash references:

typescript
/// <reference path="./my-globals.d.ts" />

// Now you can use the namespace
const x = MyGlobalNamespace.someValue;

Or declare them globally:

typescript
// my-globals.d.ts
declare namespace MyGlobalNamespace {
  export const someValue: string;
}

This makes the namespace available throughout your project without imports.

6Verify file extensions and module resolution

Ensure TypeScript can find your namespace files:

typescript
// Check file extensions
import * as MyNamespace from './my-namespace.ts'; // .ts extension
import * as MyNamespace from './my-namespace.js'; // .js extension in output

// Use path mapping in tsconfig.json for complex structures
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@namespaces/*": ["src/namespaces/*"]
    }
  }
}

// Then import
import * as MyNamespace from '@namespaces/my-namespace';

Proper module resolution ensures TypeScript can locate and understand namespace declarations.

Advanced notes

### Namespace vs Module History

TypeScript namespaces (originally called "internal modules") were created before ES6 modules were standardized. They provide a way to organize code and avoid global scope pollution. With the adoption of ES6 modules, namespaces are now primarily used for:

1. Declaration files (.d.ts): Organizing type definitions for libraries
2. Legacy codebases: Projects that predate ES6 module support
3. Global augmentations: Adding to existing global interfaces

### Module Resolution Strategies

TypeScript uses different resolution strategies:
- Classic: Looks for .ts/.d.ts files (used with AMD/System modules)
- Node: Mimics Node.js require() resolution (default for CommonJS)
- Bundler: For Webpack/Rollup (TypeScript 4.7+)

Namespaces work with all strategies but may require explicit configuration.

### Namespace Merging

TypeScript supports namespace merging, where multiple declarations contribute to the same namespace:

typescript
// file1.ts
namespace MyLib {
  export function func1() { /* ... */ }
}

// file2.ts
namespace MyLib {
  export function func2() { /* ... */ }
}

// Both functions are available in MyLib

This is useful for splitting large namespaces across files.

### Compatibility with Bundlers

Modern bundlers (Webpack, Rollup, Vite) primarily work with ES6 modules. When using namespaces:
- Use tsc to compile to your target module format
- Ensure output matches your bundler's expectations
- Consider using rollup-plugin-typescript2 or ts-loader for Webpack

### Migration Path

If maintaining namespaces is problematic, consider this migration path:
1. Convert one namespace at a time to ES6 modules
2. Update imports from import * as Namespace to named imports
3. Use codemods or search/replace for bulk changes
4. Test thoroughly as module resolution differs

Frequently asked questions

What does "Namespace must be imported or aliased using 'import as'" mean?
This error occurs when you attempt to use a TypeScript namespace without properly importing or referencing it. TypeScript namespaces are a way to organize code and prevent naming collisions, but they require specific syntax to access their contents. When you define a namespace like namespace MyNamespace { export const value = 1; }, you cannot directly reference value without either: 1.
How do I fix "Namespace must be imported or aliased using 'import as'"?
This TypeScript compilation error occurs when trying to use a namespace without proper import syntax. Namespaces in TypeScript require explicit import statements using 'import as' syntax or namespace qualifiers to access their contents.
What causes "Namespace must be imported or aliased using 'import as'"?
Directly referencing namespace members without proper import syntax Mixing namespace syntax with ES6 module imports incorrectly Missing 'import as' statement for namespace imports
Is the "Namespace must be imported or aliased using 'import as'" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading