Registry / devops / ng-annotate-patched

ng-annotate-patched

JSON →
library1.15.0jsnpmunverified

ng-annotate-patched (v1.15.0) is a maintained fork of the now-unmaintained ng-annotate library. It adds and removes AngularJS dependency injection annotations, supporting modern JavaScript syntax (ES2020, classes, arrow functions, dynamic import, export) that the original cannot parse. Key updates include a newer acorn parser and acornOptions for customization. It is the recommended choice over ng-annotate for projects using modern JS.

npm install ng-annotate-patched
INSTALL
IMPORT
SIG · NG-ANNOTATE-PATCHE
N
ng-annotate-patched
devopsjavascriptv1.15.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ngAnnotate (default export)
const ngAnnotate = require('ng-annotate-patched');
const ngAnnotate = require('ng-annotate');
Package name changed from ng-annotate to ng-annotate-patched. Use 'ng-annotate-patched' for this fork.
transform method
const result = ngAnnotate(src, options);
const result = ngAnnotate.annotate(src, options);
The default export is a function, not an object with a method. Calling ngAnnotate() directly returns {src, errors}.
fromString export (deprecated)
const { fromString } = require('ng-annotate-patched');
const fromString = require('ng-annotate-patched').fromString;
fromString() is a legacy method equivalent to the default export. Prefer calling ngAnnotate() directly.

Demonstrates adding AngularJS DI annotations to a controller function using the ng-anotate-patched library API.

const ngAnnotate = require('ng-annotate-patched'); const fs = require('fs'); const src = ` angular.module("MyMod").controller("MyCtrl", function($scope, $timeout) { "ngInject"; // ... }); `; const result = ngAnnotate(src, { add: true }); if (result.errors && result.errors.length > 0) { console.error('Errors:', result.errors); } else { console.log(result.src); // Output: // angular.module("MyMod").controller("MyCtrl", ["$scope", "$timeout", function($scope, $timeout) { // "ngInject"; // // ... // }]); } // To remove "ngInject" strings from output: const cleaned = result.src.replace(/["']ngInject["'];*/g, ''); console.log(cleaned);
Debug
Known issues
breakingPackage renamed from ng-annotate to ng-annotate-patched. Install and require 'ng-annotate-patched' instead of 'ng-annotate'.
fix
npm install ng-annotate-patched && const ngAnnotate = require('ng-annotate-patched');
affects: >=1.0
gotchaThe default export is a function (ngAnnotate(src, options)), not an object with a method. Do NOT call ngAnnotate.annotate() or similar.
fix
Use const result = ngAnnotate(src, options);
affects: >=1.0
deprecatedThe fromString() method is deprecated. It behaves identically to the default export but is provided for backward compatibility.
fix
Prefer calling ngAnnotate(src, options) directly.
affects: >=1.0
gotchaThe 'ngInject' pragma is not automatically removed from output. You must strip it manually (e.g., via sed or regex) for production code.
fix
Add a post-processing step: result.src.replace(/["']ngInject["'];*/g, '');
affects: >=1.0
gotchaImplicit matching of AngularJS patterns (e.g., controller function parameters) may fail for uncommon code forms. Use 'ngInject' or 'ng-strict-di' for debugging.
fix
Use explicit 'ngInject' pragma or enable 'ng-strict-di' in AngularJS.
affects: >=1.0
Errors
Common errors & fixes
Error: Cannot find module 'ng-annotate'
Attempted to require the old package name 'ng-annotate' which is unmaintained and not installed.
fix
npm install ng-annotate-patched && const ngAnnotate = require('ng-annotate-patched');
TypeError: ngAnnotate.annotate is not a function
Incorrectly assumed the default export is an object with an 'annotate' method.
fix
Use ngAnnotate(src, options) directly instead of ngAnnotate.annotate(src, options).
SyntaxError: Unexpected token
Using ng-annotate (original) on modern JavaScript code (ES6+, import/export, arrow functions).
fix
Switch to ng-annotate-patched which supports ES2020 syntax via updated acorn parser.
Error: No input file specified
Running the CLI tool 'ng-annotate' without providing a source file argument.
fix
ng-annotate -a path/to/source.js
Upgrade
Version history
1.15.0latest on npm
Audit
Dependencies
acornrequiredJavaScript parser used to transform source code
Agent activity
2 hits · last 30 days
node
2
Resources
ng-annotate-patched — npm install ng-annotate-patched · libregistry