Angular 7项目执行ng serve报错,npm install无效求助
Hey Sarah, sorry to hear your Angular 7 project started throwing those weird type errors out of nowhere! Those TS2304 errors about missing ArrayBuffer and Object are almost always related to missing library configurations in your tsconfig.json file, not issues with your dependencies (which explains why reinstalling node_modules didn't help). Let's walk through the fixes step by step:
1. Check and Update Your tsconfig.json Lib Settings
The most common culprit here is that your TypeScript compiler isn't including the necessary standard library definitions to recognize global types like ArrayBuffer and Object.
Head to your project root's tsconfig.json file, look for the compilerOptions section, and make sure the lib array includes these entries:
{ "compilerOptions": { // ... other existing options "lib": [ "es2017", "dom", "es2015.core" ] // ... other existing options } }
es2015.corecovers basic JavaScript types likeObjectes2017includes definitions forArrayBufferand other ES2017 featuresdomadds browser-specific type definitions that Angular relies on
If your lib array was missing any of these, adding them should resolve most of those type errors.
2. Clear Angular's Build Cache
Sometimes cached build artifacts can cause unexpected issues, even after reinstalling dependencies. Run these commands to clear the cache and restart your dev server:
# Delete the Angular cache folder rm -rf node_modules/.cache # Start the server with cache cleared ng serve --clearCache
3. Verify TypeScript Version Compatibility
Looking at your package.json, you're using TypeScript 3.1.1 which is fully compatible with Angular 7.2.x (Angular 7 officially supports TypeScript 3.1.x). But just to be safe, you can re-install the exact TypeScript version to rule out any corruption:
npm install typescript@3.1.1 --save-dev
4. Double-Check for Custom Type Conflicts
If the above steps don't work, take a quick look at any custom *.d.ts files in your project. Rarely, custom type definitions can override or interfere with global TypeScript types. But since you already reinstalled node_modules, this is less likely.
Give these steps a try—most folks fix this issue with the first two steps alone. Let me know if you run into any snags!
内容的提问来源于stack exchange,提问作者Sarah De Cock

