使用 razzle 4.x 和 webpack 5.x 在 server.js 中捆绑 node_modules

发布于 2025-01-12 21:58:28 字数 1092 浏览 4 评论 0原文

我有一个 Razzle 应用程序,最近从 Razzle 3.3 更新到 4.2。为此,我更新了 package.json 中的许多包,包括 webpack。

对于我的 Razzle 3.3 实现,我通过在 razzle.config.js 中添加以下内容,将 node_modules 与我的 server.js 捆绑在一起。

module.exports = {
    modify: (config, { target }, webpack) => {
        const appConfig = { ...config };

        if (target === 'node') {
            // some changes to appConfig

            appConfig.externals = []; // this resulted in node_modules to be bundled with my server.js
        }
    }
};

然而,随着 Razzle 4.2 的更新,这不再有效。我在 Google 上搜索了很长一段时间,找不到任何有关 Razzle 4 是如何完成此操作的知识,但有很多帖子指出这是一个 babel-loader 问题,我认为这是一个问题可以通过更新我的配置来修复。在 Razzle 文档 上有一个关于“外部模块的转译”的部分' 但是我从示例中假设,这是针对单个模块的,而不是整个 node_modules 库。

每当我尝试在 CI 环境中运行应用程序时,都会收到以下错误:

Error: Cannot find module 'react'

如果将我的 node_modules 文件夹复制到构建中,我可以解决此错误,但这对我来说不是一个选项,并且我希望将 node_modules 与我的申请。

我想知道是否有人知道 Razzle 4.x/Webpack 5.x 是如何实现这一点的,无论哪一个是罪魁祸首?

I have a Razzle application which I've recently updated from Razzle 3.3 to 4.2. In doing so I've updated a lot of packages in my package.json, including webpack.

For my Razzle 3.3 implementation I bundled the node_modules with my server.js by having the following in my razzle.config.js.

module.exports = {
    modify: (config, { target }, webpack) => {
        const appConfig = { ...config };

        if (target === 'node') {
            // some changes to appConfig

            appConfig.externals = []; // this resulted in node_modules to be bundled with my server.js
        }
    }
};

However with the update to Razzle 4.2 this no longer works. I've Googled around for quite some time unable to find any knowledge of how this is done with Razzle 4, but there are a lot of posts pointing towards this being a babel-loader issue, which I assume is possible to fix with an update to my config. On the Razzle documentation there is a section about 'Transpilation of External modules' however I assume from the example, that this is for single modules, and not the entire node_modules library.

Whenever I try to run my application in my CI environment I get the following error:

Error: Cannot find module 'react'

I can solve this error if I copy my node_modules folder to the build, however this is not an option for me, and I want the node_modules to be bundled with my application.

I was wondering if someone out there knows how this is achieved for Razzle 4.x/Webpack 5.x, whichever is the culprit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花似海 2025-01-19 21:58:28

更新配置是我的解决方案。

当 Razzle 更新到版本 3.3 时,他们弃用了 razzle.config.js 中旧的 modify 函数。他们用一种新方法取代了它,称为modifyWebpackConfig用于编写 razzle 配置。

所以我可以将我的代码更改为

module.exports = {
    modifyWebpackConfig: ({ env: { target }, webpackConfig, webpackObject }) => {
        const appConfig = { ...webpackConfig };

        if (target === 'node') {
            // some changes to appConfig

            appConfig.externals = [];
        }
    
        return appConfig;
    }
}

Updating the configuration was my solution.

When Razzle was updated to version 3.3 they deprecated the old modify function in the razzle.config.js. They replaced it by a new method, known as modifyWebpackConfig for writing the razzle config.

So I could change my code to

module.exports = {
    modifyWebpackConfig: ({ env: { target }, webpackConfig, webpackObject }) => {
        const appConfig = { ...webpackConfig };

        if (target === 'node') {
            // some changes to appConfig

            appConfig.externals = [];
        }
    
        return appConfig;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文