Open-source project
bcherny/ngimport avatar
bcherny/ngimport

ngimport: Bringing ES6 Imports to Angular 1 Services

Easy to use ES6 imports for $http, $log, and other Angular 1 services

100 stars5 forksTypeScriptLicense varies

At a glance

What is it?
ngimport is a small TypeScript library that lets you import Angular 1 services like $http and $log directly, bypassing Angular's dependency injection. It aims to make Angular 1 code more portable and easier to migrate to modern frameworks.
Who is it for?
Adopt ngimport if you are maintaining a legacy Angular 1 codebase and want to gradually reduce its reliance on Angular's DI, especially if you are already using TypeScript. Avoid it if you are starting a new project or if you rely heavily on Angular 1 built-ins at module load time, since those remain undefined until bootstrap.
Can I use it commercially?
Not without permission. GitHub finds no licence file in the repository, and without a licence all rights are reserved by default: you may read the code but not reuse it. Check the README, or ask the authors, before using it.
Is it still maintained?
Probably not. The repository last received commits 107 months ago, on January 29, 2018.
What is it written in?
Mainly TypeScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 15, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • bcherny/ngimport100 ★ · +0
Star history
Repository2026-08-182026-11-15
bcherny/ngimport100100

0 Stars in the last 89 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

The Problem: Angular 1 DI in a Module World

Angular 1's dependency injection (DI) was designed before JavaScript had a standard module system. It works, but it couples your code to Angular's container. With CommonJS and ES Modules now standard, DI forces you to write factory functions and closure-based injection, which makes your code less portable. If you use TypeScript, you often have to duplicate interface definitions: a service class exists, but because its dependencies are injected via a closure, you cannot export the class directly. Instead, you create a second interface just to type the factory output. ngimport directly addresses this by letting you import Angular services as if they were ordinary ES6 modules.

How ngimport Works: Injector at Runtime

ngimport exports named constants like $http and $log that are initially undefined. When your Angular app bootstraps, ngimport uses Angular's $injector to fetch the actual service instances and assigns them to those exported variables. The README shows a pattern: you import { $http, $log } from 'ngimport', and then use them directly in your functions. The underlying mechanism is not fully documented, but it likely uses a run block or a module initialization that hooks into Angular's injector. The key point is that the exported references are mutable: they start as undefined and only become valid after bootstrap. This is why the README warns that Angular builtins will be undefined at the top level of your module.

Getting Started: Installation and a Concrete Example

To install, use either Yarn or npm: `yarn add ngimport angular` or `npm install ngimport angular --save`. If you use ngResource, you also need to install `ngimport-ngresource`. The README provides a full example. Before ngimport, you might write a factory like `angular.factory('Get', function($http, $log) { ... })`. After ngimport, you write a plain exported function: `export function Get(url) { return $http.get(url).then(...) }`. You also need to add the module dependency: `angular.module('myModule', ['bcherny/ngimport'])`. The example is in TypeScript, but it works in plain JavaScript as well. The library is typed, so you get type safety if you use TypeScript.

The Portability Payoff: Mixing Frameworks

The biggest benefit claimed in the README is portability. With ngimport, your service functions are no longer tied to Angular's DI. You can export a function that uses $http, and then use that same function in an Angular 2 component or a React component, as long as you have a way to provide the service (e.g., by bootstrapping Angular 1 alongside). This also means you can pass constructor arguments to your services, which is impossible when the constructor is used for DI. The README lists several upsides: no proprietary DI, no lock-in, easier migration path to Angular 2 or React, no duplicated TypeScript interfaces, and you can still mock dependencies with $provide and assert HTTP requests with $httpBackend in tests. This makes ngimport an adapter for gradually migrating a codebase.

Wrapping Your Own Legacy Modules

The README shows how to apply the same technique to your own Angular 1 services. For example, if you have a `fooService` registered with `angular.module('myModule').service('fooService', ...)`, you can expose it via an ES6 import by creating a TypeScript file that declares an exported variable and populates it in a run block: `export let fooService = undefined; module('myModule').run(function($injector) { fooService = $injector.get('fooService') })`. This lets you import `fooService` directly, and you can then migrate the service itself to TypeScript at your own pace. This is a practical pattern for incrementally reducing DI usage across a legacy codebase.

Limitations: Bootstrap Timing and CommonJS Pitfalls

The README clearly states two limitations. First, Angular builtins like $http and $rootScope are undefined until you bootstrap the app. This is because Angular creates injectors lazily. So you cannot use these services at the top level of your module; you must bootstrap first or avoid top-level usage. Second, if you transpile to CommonJS, you must destructure the import rather than importing a default value. The reason is that the exported reference is updated after bootstrap, and if you import a default, your consumer will hold a pointer to the old, undefined reference. This is a subtle but critical gotcha for anyone using Babel or TypeScript with CommonJS output. These limitations are inherent to the approach and not bugs, but they require discipline in how you structure your code.

Alternatives and Trade-offs

The main alternative is to stick with Angular 1's DI and use TypeScript's `angular-typings` or `@types/angular` to get type safety. That approach keeps the DI container but forces you to write factories and duplicate interfaces. Another alternative is to migrate to Angular 2+ or another framework entirely, which is a much larger effort. ngimport sits in between: it gives you the portability of ES6 imports while keeping your Angular 1 runtime. The trade-off is that you still need Angular 1 to bootstrap and provide the services, so you are not eliminating Angular 1, just reducing its coupling. If you are planning a full rewrite, ngimport might be unnecessary, but if you need a gradual migration, it offers a low-risk bridge.

Maintenance, License, and Adoption Considerations

The repository is archived? No, the repository metadata says 'Archived: no', but it has no published GitHub releases. This could mean the project is stable or stale. The README lists a todo that includes adding support for many Angular builtins like $animate, $cookies, $route, and $sanitize, which suggests the library is not complete for all services. The license is listed as MIT in the README, though the repository metadata says 'unknown'. The README also mentions a companion package, `ngimport-ngresource`, for ngResource. Before adopting, you should check the npm page for the latest version and see if the package is still maintained. If you rely on services not yet supported, you may need to wrap them yourself using the technique shown in the README.

Editorial conclusion

Adopt ngimport if you are maintaining a legacy Angular 1 codebase and want to gradually reduce its reliance on Angular's DI, especially if you are already using TypeScript. Avoid it if you are starting a new project or if you rely heavily on Angular 1 built-ins at module load time, since those remain undefined until bootstrap. Before adopting, verify that your build tooling supports ES module imports and that you can destructure imports when transpiling to CommonJS. Also, check the current npm package version and whether the repository is actively maintained, as it has no published GitHub releases.

Frequently asked questions

What is the difference between imports and providers in Angular?

Angular 1 dependency injection makes sense when there was no JavaScript module standard, but with CommonJS and ES Modules it only makes code less portable. ngimport replaces injection with standard imports, and the only application-level cost is listing bcherny/ngimport in your module's dependency array.

How do I install ngimport?

With Yarn, yarn add ngimport angular, or with NPM, npm install ngimport angular --save. If you use ngResource, also install the separate ngimport-ngresource package.

What are the limitations of ngimport?

Angular builtins such as $http and $rootScope are undefined until you bootstrap your app, because of the way Angular creates injectors. And if you transpile to CommonJS, destructure the import rather than importing a default value, or your consumer keeps a pointer to the old undefined reference.

Which Angular services does ngimport not cover?

The Todo list names $animate, $animateCss, $aria, $cookies, $provide, $resource, $rootRouter, $route, $routeParams, $routerRootComponent, $sanitize, $swipe, and $touch. For ngResource there is a separate package, ngimport-ngresource.

How do I run the ngimport tests?

npm test, which runs karma start --single-run. A pretest hook runs the build first, so the suite tests compiled output, and prepublishOnly runs the same command before publishing.

Official sources

  1. Official README
  2. Project repository
  3. README
  4. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/bcherny-ngimport.svg)](https://hysenlabs.com/projects/bcherny-ngimport)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/bcherny-ngimport.svg)](https://hysenlabs.com/projects/bcherny-ngimport)

Was this useful?