[英]Using NODE_PATH in electron to map folders inside src/ as modules
I'm trying to set NODE_PATH
on my electron project to NODE_PATH=src
but it is not working. 我正在尝试设置
NODE_PATH
我的电子项目NODE_PATH=src
,但它无法正常工作。
The idea is to be able to require any folder inside src
by it's name. 我们的想法是能够通过它的名称来要求
src
的任何文件夹。
So in the case of the folder structure 所以在文件夹结构的情况下
src/
--main.js
--components/
----index.js
from main.js
I should be able to use 从
main.js
我应该可以使用
import components from "components"
instead of 代替
import components from "./components"
I've already tried setting it in start script
: 我已经尝试在
start script
设置它:
"start": "cross-env NODE_PATH=src electron-forge start"
as well as in a .env
file to no avail. 以及
.env
文件无济于事。
I'm sharing here what has worked for me:- 我在这里分享对我有用的东西: -
At the entry point of your application add this line: 在您的应用程序的入口点添加以下行:
global.require.main.paths.push('./src');
The above is equivalent to to setting the NODE_PATH variable. 以上相当于设置NODE_PATH变量。 Make sure this line is added before any of your code is executed.
确保在执行任何代码之前添加此行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.