无需每次执行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-templateonce, selecting all your preferred options (routing support, stylesheet format, etc.) - Wait for all dependencies to install, then verify the project works with
ng serveandng e2eto 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 likename,version, anddescriptionto 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.jsonin your root folder. If not, runnpm init -ythen copy the Angular dependencies from an existing working project’spackage.jsonand runnpm 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.jsonfile afterward to ensure path configurations align with your setup
If you work on multiple Angular projects regularly, a monorepo can eliminate repeated dependency downloads:
- Create a root folder, add a
package.jsonwith"private": trueand"workspaces": ["projects/*"](for npm) or the equivalent yarn workspace config - Run
ng new projects/my-first-projectonce — this installs all shared dependencies in the rootnode_modulesfolder - 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 newwill 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
相关产品推荐
相关产品推荐

