[英]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.json
的dependencies
中列出的所有模块将在生产阶段捆绑。 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.