使用ng2-daterangepicker构建报错:导入时未从node_modules读取包
I’ve run into this exact issue before with ng2-daterangepicker—let’s walk through the fixes that worked for me:
1. Verify the package is properly installed
First, double-check that the package exists in your node_modules directory. If it’s missing, reinstall it with:
npm install ng2-daterangepicker --save # Or if using yarn yarn add ng2-daterangepicker
If it’s already present, try a clean reinstall to fix any corrupted or incomplete files:
- Delete your
node_modulesfolder - Delete
package-lock.json(oryarn.lock) - Run
npm installagain
2. Check TypeScript module resolution settings
Make sure your tsconfig.json is configured to resolve modules from node_modules correctly. In the compilerOptions section, confirm these settings:
{ "compilerOptions": { "moduleResolution": "node", "baseUrl": "./", // Other existing options... } }
The moduleResolution: "node" flag tells TypeScript to use Node.js-style module resolution, which prioritizes looking in node_modules over your project root.
3. Confirm the correct import path
Sometimes the package’s entry point isn’t at the root of the npm package. Check the main or module field in node_modules/ng2-daterangepicker/package.json—if it points to a subdirectory like lib/index.js, adjust your import to match:
import { Daterangepicker } from 'ng2-daterangepicker/lib';
4. Ensure Angular version compatibility
Older versions of ng2-daterangepicker might not work with newer Angular releases. Check the package’s documentation to confirm it supports your Angular version, and upgrade/downgrade the package accordingly if needed.
5. Clear the Angular build cache
If you’re using Angular CLI, outdated cache can cause path resolution issues. Clear it with:
ng cache clean
内容的提问来源于stack exchange,提问作者ahsan ahmad

