简体   繁体   English

生产构建后如何在反应应用程序中使用节点模块?

[英]How can I use node module in react app after production build?

I have a project which is created in Electron.js.我有一个在 Electron.js 中创建的项目。 It uses simple HTML files.它使用简单的 HTML 文件。 But now I want to convert this app into a React app.但是现在我想把这个应用程序转换成一个 React 应用程序。 While using HTML files directly, I am able to use node modules like electron , requiring main app.js file and all.在直接使用 HTML 文件时,我可以使用像electron这样的节点模块,需要主app.js文件和所有文件。 After building the project with webpack, the React app will get converted into static files.使用 webpack 构建项目后,React 应用程序将转换为 static 文件。 So how can I access the node modules after the build?那么构建后如何访问节点模块呢?

I suggest you to use Electron-React boilerplate for your project.我建议你为你的项目使用Electron-React 样板 As this contains all the basic configurations for building electron-react project.因为这包含构建电子反应项目的所有基本配置。 Go through the electron-react docs and setup an initial electron-react app. Go 通过电子反应文档并设置初始电子反应应用程序。

 # First, clone the repo via git: git clone --depth=1 https://github.com/electron-react-boilerplate/electron-react-boilerplate.git your-project-name cd your-project-name # And then install dependencies with yarn: yarn

All the modules listed on dependencies on package.json will be bundled in the production stage. package.jsondependencies中列出的所有模块将在生产阶段捆绑。 So the relevant modules can be accessed on the build stage too.因此也可以在构建阶段访问相关模块。 You can create components with your html files.您可以使用 html 文件创建组件。

Make sure enable nodeIntegration on your BrowserWindow , so the nodemodules can be accessed by the renderer process.确保在BrowserWindow上启用nodeIntegration ,以便渲染器进程可以访问节点模块。

webPreferences: {
            nodeIntegration: true
}

you can use the electron-jsx package to work (I'm the author of the package), is simple and easy你可以使用 electron-jsx package 来工作(我是包的作者),简单易行

Only you need that in you index.html file:只有你需要在 index.html 文件中:

<script>
require("electron-jsx")(__dirname, {
    reactDir: "./react-sources",
})
</script>
<script react-src="./react-sources/index.jsx">

And you can work with react normal (without webpack), and, additional, you can call any node package with ES6 import for react而且您可以使用 react normal(没有 webpack),此外,您可以使用 ES6 导入调用任何节点 package 进行反应

More info and docs for the package: package 的更多信息和文档:

https://www.npmjs.com/package/electron-jsx https://www.npmjs.com/package/electron-jsx

https://github.com/mdjfs/electron-jsx https://github.com/mdjfs/electron-jsx

PD: You also need set nodeIntegration to true PD:您还需要将 nodeIntegration 设置为 true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在反应应用程序中使用 node.js v8 模块? - How can I use the node.js v8 module in a react app? 当我尝试使用 Parcel 构建在开发模式下运行良好的 React 应用程序的生产版本时,我得到“找不到模块 'sass'” - When I try to use Parcel to build a production version of a React app that works fine in development mode, I get “Cannot find module 'sass'” 如何在 Node 16 应用程序中使用命名模块导出? - How Can I Use Named Module Exports in Node 16 App? 我如何更改 React 查找生产构建的目录? - How can I change the directory React looks for the production build? 我如何将全栈应用程序部署到 firebase,它建立在 React Node 上并使用 mysql 数据库 - How can i deploy full stack app to the firebase which is build on React Node and using mysql database 如何在 React Native 项目中使用 Node.JS 的 Crypto 内置模块? - How can I use Crypto built-in module from Node.JS in React Native Project? 节点JS模块,如何在模块中使用本地功能 - Node JS Module, How can I use local function in module 我可以在 React 应用程序中使用节点“child_process”吗? - Can I use node 'child_process' in a React App? 如何在生产中构建 Node js 应用程序以在 IIS 中托管 - How to build Node js app in production for hosting in IIS Node/WebpackDevServer/react-scripts,删除“To create a production build, use npm run build”消息 - Node/WebpackDevServer/react-scripts, remove “To create a production build, use npm run build” message on start
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM