Angular 5.2升级@ngx-translate/core至9.0.0后导入模块失败求助
Hey there, let's work through this import issue you're hitting after upgrading @ngx-translate/core from 7.x to 9.0.0 in your Angular 5.2 project. The error you're seeing is totally expected—here's why, and how to fix it:
Why This Happens
Starting with version 8.x, the @ngx-translate/core team restructured how the package is bundled and what paths are exposed publicly. The internal paths like @ngx-translate/core/src/translate.service and @ngx-translate/core/index are no longer part of the package's public API, so your build tool can't resolve them anymore. In 7.x, these paths worked because the older bundling setup didn't restrict access to internal files, but that's been tightened up in newer versions to follow best practices.
Step-by-Step Fixes
1. Use the Official Public Import Paths
Instead of trying to import from internal files or index paths, use the main package entry point to import everything you need. Here's how to adjust your imports:
- If you need the entire module namespace (though this is rarely necessary):
import * as ngxTranslateCore from '@ngx-translate/core'; - For specific services like TranslateService (this is the recommended approach for tree-shaking and clarity):
import { TranslateService } from '@ngx-translate/core';
All public APIs (services, pipes, directives, etc.) are now exported directly from the main @ngx-translate/core entry, so you don't need to dig into subpaths anymore.
2. Verify Dependencies Compatibility
Make sure any related @ngx-translate packages (like @ngx-translate/http-loader if you're using it) are compatible with v9.0.0 and Angular 5.2. For Angular 5.x, you should use @ngx-translate/http-loader@3.x—this avoids version mismatches that could cause additional build issues.
3. Clean Build Caches
Sometimes old cached files can hang around and cause path resolution problems. Run these commands to clear things out:
# Delete node_modules and lock file rm -rf node_modules package-lock.json # Clear npm cache npm cache clean --force # Reinstall dependencies npm install # Do a clean Angular build ng build --clean
If you're on Windows, replace the first command with:
rmdir /s /q node_modules && del package-lock.json
Final Notes
Sticking to the public API imports will not only fix your current error but also make future upgrades smoother, since the internal package structure is likely to keep evolving while the main entry point remains stable.
内容的提问来源于stack exchange,提问作者Stas.dp

