How to add a dev dependency with angular schematics extending from ng-new

Discussão em 'Angular' iniciado por Iván Jiménez Luque, Abril 9, 2025.

  1. I need to add dev dependencies when i use ng-new external schematic

    I tried many things but right now the most successful one is the this:

    context.addTask(
    new NodePackageInstallTask({
    workingDirectory: `./${options.name}`,
    packageName: "lodash-es",
    })
    );


    This adds a dependency the problem is i cannot specify or i dont now how to specify that this dependency is a dev dependency. The other way i tried is using this in my schematic

    [
    {
    type: NodeDependencyType.Dev,
    name: "husky",
    version: "*",
    },
    ].forEach((dependency) => addPackageJsonDependency(tree, dependency));
    };


    This works fine when the project is already created but my main schematic pretends to create a new project with my custom files and configuration. The main schematic looks like this:

    import {
    apply,
    chain,
    empty,
    externalSchematic,
    mergeWith,
    Rule,
    schematic,
    SchematicContext,
    Tree,
    } from "@angular-devkit/schematics";

    import {
    Schema as NgNewOptions,
    Style,
    } from "@schematics/angular/ng-new/schema";

    export function newProject(options: NgNewOptions): Rule {
    return async (_tree: Tree, _context: SchematicContext) => {
    options.style = Style.Scss;

    const ngNewOriginal = externalSchematic(
    "@schematics/angular",
    "ng-new",
    options
    );
    const customStarter = schematic("angular-starter", options);

    return chain([mergeWith(apply(empty(), [ngNewOriginal, customStarter]))]);
    };
    }



    Inside the angular-starter i call my custom files. What i read is that problem with the addPackageJsonDependency cannot make its thing because the creation of the files its done after everything else so it doesnt find a package.json. I also tried to add my custom package but i think thats not the way because i want to be updated with anything that angular does in every new version

    Continue reading...

Compartilhe esta Página