Error importing cesium in ES6 mode

I imported the Cesium module using ES6, as follows:
import * as Cesium from ‘cesium/Cesium’;

The error message is as follows:
TS2307: Cannot find module ‘cesium/Cesium’ or its corresponding type declarations.

Project dependencies are as follows:
cesium: 1.75.0
webpack: 5.39.1
react:17.0.2
typescript: 4.3.5

The configuration of WebPack is as follows:
const cesiumSource = ‘node_modules/cesium/Source’;
const cesiumWorkers = ‘…/Build/Cesium/Workers’;
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: ‘./src/index.html’
}),
new CopyWebpackPlugin({
patterns: [
{ from: path.join(cesiumSource, cesiumWorkers), to: ‘Workers’ },
{ from: path.join(cesiumSource, ‘Assets’), to: ‘Assets’ },
{ from: path.join(cesiumSource, ‘Widgets’), to: ‘Widgets’ },
]
}),
new webpack.DefinePlugin({
CESIUM_BASE_URL: JSON.stringify(’’)
})
],
output: {
filename: ‘[name].bundle.js’,
path: path.resolve(__dirname, ‘…/’, ‘build’),
sourcePrefix: ‘’,
},
amd: {
toUrlUndefined: true,
},
resolve: {
extensions: [’.js’, ‘.json’, ‘.sass’, ‘.scss’, ‘.less’, ‘jsx’, ‘.ts’, ‘.tsx’],
alias: {‘cesium’: path.resolve(cesiumSource)},
},

The configuration of TS is as follows
{
“compilerOptions”: {
“outDir”: “./dist/”,
“noImplicitAny”: true,
“module”: “commonjs”,
“target”: “esnext”,
“jsx”: “react”,
“allowJs”: true,
“sourceMap”: true,
“strict”: true,
“forceConsistentCasingInFileNames”: true,
“allowSyntheticDefaultImports”: true,
“moduleResolution”: “node”,
“types”: [
“node”,
“cesium”
],
},
“include”: [“src”],
“exclude”: [
“node_modules”
]
}

@ningya

Thank you for your question! In general, it is best practice to import only the classes that you need from a given API. This often looks like this:

import { Ion, Viewer, createWorldTerrain, createOsmBuildings, Cartesian3, Math } from "../node_modules/cesium"

More information on your package.json and node_modules files/directories would help me debug this issue. However, I think it might be more effective for you to use the following repository as a working reference for using CesiumJS with webpack.

Please look over this repository and ensure that your JavaScript, json, and webpack files are set up correctly. Please let me know if you have any other questions or concerns! Looking forward to learning more about your project :grinning:

-Sam

@sam

I have solved the problem where modules could not be found

I changed the alias in webpack
from alias: {‘cesium’: path.resolve(‘node_modules/cesium/Source’)}
to alias: {‘cesium’: path.resolve(‘node_modules/cesium/Source/Cesium’)}

The error message that the module could not find disappeared.

Unfortunately, new bugs have emerged,many TS error messages are displayed,like this:

TS2339: Property ‘TOP’ does not exist on type ‘typeof HorizontalOrigin’.
TS7053: Element implicitly has an ‘any’ type because expression of type ‘any’ can’t be used to index type ‘typeof Color’.
TS7015: Element implicitly has an ‘any’ type because index expression is not of type ‘number’.
TS2322: Type ‘string | Color’ is not assignable to type ‘string’.
Type ‘Color’ is not assignable to type ‘string’.
TS2345: Argument of type ‘{ url: string; maximumLevel: number; defaultAlpha: number; }’ is not assignable to parameter of type ‘ConstructorOptions’.
Object literal may only specify known properties, and ‘defaultAlpha’ does not exist in type ‘ConstructorOptions’.

My tsconfig.json configuration is as follows:

I cannot find a solution at present. I hope to get your help and look forward to your reply.