You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需每次执行ng new project_name,Angular 2项目结构获取替代方案

Great question! I totally get the frustration of running ng new project_name every time when you just want a quick, reusable Angular project structure without waiting for dependencies to install all over again. Here are a few solid solutions to solve this:

1. Create a reusable base template project

This is the simplest, most straightforward approach:

  • Run ng new my-angular-base-template once, selecting all your preferred options (routing support, stylesheet format, etc.)
  • Wait for all dependencies to install, then verify the project works with ng serve and ng e2e to make sure everything is set up correctly
  • Once it’s perfect, save this entire folder as your go-to template. Whenever you need a new project, just copy the folder, rename it, and update the package.json (adjust fields like name, version, and description to match your new project)
  • Pro tip: Pre-configure common tools like lint rules, environment variables, or shared components in the template to save even more time later
2. Rebuild project structure/e2e with Angular CLI generate commands

If you already have a folder with node_modules but lack the full project structure or e2e setup, you can manually scaffold the missing parts:

  • First, ensure you have a package.json in your root folder. If not, run npm init -y then copy the Angular dependencies from an existing working project’s package.json and run npm install
  • Use these CLI commands to build out the structure:
    • ng generate application my-new-app (creates the core src folder, config files, and basic app structure)
    • ng generate e2e my-new-app-e2e (sets up the e2e testing framework — Cypress or Protractor, depending on your Angular version)
  • Note: You may need to tweak the angular.json file afterward to ensure path configurations align with your setup
3. Use a monorepo setup for shared dependencies

If you work on multiple Angular projects regularly, a monorepo can eliminate repeated dependency downloads:

  • Create a root folder, add a package.json with "private": true and "workspaces": ["projects/*"] (for npm) or the equivalent yarn workspace config
  • Run ng new projects/my-first-project once — this installs all shared dependencies in the root node_modules folder
  • For new projects, run ng generate application projects/my-next-project — this reuses the root dependencies instead of downloading everything from scratch
  • Add e2e support to any app with ng generate e2e projects/my-next-project-e2e
4. Speed up ng new with dependency caching

If you still prefer using ng new but want to cut down on wait time, leverage dependency caching:

  • For npm: The built-in cache is usually enabled by default, but you can confirm with npm config get cache. It stores downloaded packages locally, so subsequent installs will skip re-downloading existing packages
  • For yarn: Yarn automatically caches packages, so ng new will be significantly faster after the first run
  • For even better efficiency, try pnpm — it uses a shared store for dependencies, reducing disk usage and install times drastically

内容的提问来源于stack exchange,提问作者Nikhil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:32:50