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:
Modern.js provides inspect command to view the final Modern.js configuration and webpack / Rspack configuration generated by the project.
If the following error occurs when compiling, it is usually caused by installing the wrong version of webpack in the project, or installing multiple versions of webpack:
The webpack version problem has the following situations:
In the first case, it is recommended to remove the webpack dependency from the project's package.json. Because Modern.js encapsulates webpack-related capabilities by default, and will pass in the webpack object in the tools.webpack configuration option. Therefore, in most cases, it is not recommended to install additional webpack dependencies in the project.
In the second case, it is recommended to see if you can upgrade an npm package so that its dependent webpack version range is consistent with the Modern.js. It is also possible to manually unify versions through the ability of the package manager, e.g. using yarn resolutions or [pnpm overrides](https ://pnpm.io/package_json#pnpmoverrides).
If it is the third case, you can use the two methods mentioned in the second case, or you can try to delete the lock file and reinstall it to solve it.
Deleting the lock file will automatically upgrade the dependency version in the project to the latest version under the specified scope, please test it thoroughly.
Due 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: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.
For 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:
In addition to the reasons mentioned above, there is another possibility that can cause Babel compilation to hang, which is when Babel compiles a large JS file exceeding 10,000 lines (usually a large file in the node_modules directory that is compiled using source.include
).
When Babel compiles large files, the built-in babel-plugin-styled-components in Modern.js can cause the compilation to hang. There is already a relevant issue in the community .
In the future, Modern.js will consider removing the built-in babel-plugin-styled-components. In the current version, you can set tools.styledComponents to false
to remove this plugin.
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:
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:
Modern.js will throw an error after compiling the above code:
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:
In any case, it is recommended to use import type
to import types, this will help the compiler to identify the type.