If you encounter any build-related issues, you can refer to the current document for troubleshooting.
Modern.js is internally based on Rsbuild and encapsulates its own build tool, so you can directly refer to the FAQ document of Rsbuild:
By default, Modern.js's webpack cache is generated in the ./node_modules/.cache/webpack directory.
If you need to clear the local webpack cache, you can execute the following command:
rm -rf ./node_modules/.cacheModern.js provides inspect command to view the final Modern.js configuration and webpack / Rspack configuration generated by the project.
➜ npx modern inspect
Inspect config succeed, open following files to view the content:
- Builder Config: /root/my-project/dist/rsbuild.config.mjs
- Rspack Config (web): /root/my-project/dist/rspack.config.web.mjsDue to considerations of compilation performance, by default, the Modern.js does not compile files under node_modules or files outside the current project directory.
Therefore, when you reference the source code of other sub-projects, you may encounter an error similar to You may need an additional loader to handle the result of these loaders.
There are several solutions to this problem:
source.include configuration option to specify the directories or modules that need to be additionally compiled. Please refer to Usage of source.include for more information.exports is not defined runtime error?If the compilation is succeed, but the exports is not defined error appears after opening the page, it is usually because a CommonJS module is compiled by Babel.
Under normal circumstances, Modern.js will not use Babel to compile CommonJS modules. If the source.include configuration option is used in the project, some CommonJS modules may be added to the Babel compilation.
There are two workarounds for this problem:
sourceType configuration option to unambiguous, for example:export default {
tools: {
babel(config) {
config.sourceType = 'unambiguous';
},
},
};Setting sourceType to unambiguous may have some other effects, please refer to Babel official documentation.
If the following error occurs during compilation, it is usually because a CommonJS module is compiled with Babel in the project, and the solution is same as the above exports is not defined problem.
Error: ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: 581For more information, please refer to issue: babel#12731.
When the compilation progress bar is stuck, but there is no Error log on the terminal, it is usually because an exception occurred during the compilation. In some cases, when Error is caught by webpack or other modules, the error log can not be output correctly. The most common scenario is that there is an exception in the Babel config, which is caught by webpack, and webpack swallows the Error in some cases.
Solution:
If this problem occurs after you modify the Babel config, it is recommended to check for the following incorrect usages:
// wrong example
export default {
tools: {
babel(config, { addPlugins }) {
// The plugin has the wrong name or is not installed
addPlugins('babel-plugin-not-exists');
},
},
};// wrong example
export default {
tools: {
babel(config, { addPlugins }) {
addPlugins([
[
'babel-plugin-import',
{ libraryName: 'antd', libraryDirectory: 'es' },
],
[
'babel-plugin-import',
{ libraryName: 'antd-mobile', libraryDirectory: 'es' },
],
]);
},
},
};// correct example
export default {
tools: {
babel(config, { addPlugins }) {
addPlugins([
[
'babel-plugin-import',
{ libraryName: 'antd', libraryDirectory: 'es' },
'antd',
],
[
'babel-plugin-import',
{ libraryName: 'antd-mobile', libraryDirectory: 'es' },
'antd-mobile',
],
]);
},
},
};Modern.js enables webpack's persistent cache by default.
After the first compilation is completed, the cache file will be automatically generated and output to the ./node_modules/.cache/webpack directory. When the second compilation is performed, the cache is hit and the compilation speed is greatly improved.
When configuration files such as package.json are modified, the cache is automatically invalidated.
If the webpack compilation cache in the project has not taken effect, you can add the following configuration for troubleshooting:
export default {
tools: {
webpack(config) {
config.infrastructureLogging = {
...config.infrastructureLogging,
debug: /webpack\.cache/,
};
},
},
};After adding the above configuration, webpack will output logs for debugging. Please refer to the logs related to PackFileCacheStrategy to understand the cause of cache invalidation.
If the @types/lodash package is installed in your project, you may import some types from lodash, such as the DebouncedFunc type:
import { debounce, DebouncedFunc } from 'lodash';Modern.js will throw an error after compiling the above code:
Syntax error: /project/src/index.ts: The lodash method `DebouncedFunc` is not a known module.
Please report bugs to https://github.com/lodash/babel-plugin-lodash/issues.The reason is that Modern.js has enabled the babel-plugin-lodash plugin by default to optimize the bundle size of lodash, but Babel cannot distinguish between "value" and "type", which resulting in an exception in the compiled code.
The solution is to use TypeScript's import type syntax to explicitly declare the DebouncedFunc type:
import { debounce } from 'lodash';
import type { DebouncedFunc } from 'lodash';
In any case, it is recommended to use import type to import types, this will help the compiler to identify the type.