javascript在我的客户端和服务器文件夹之间共享代码 [英] javascript Share code between my client and my server folders
问题描述
我有一个包含两个文件夹的目录:我的服务器应用程序( NodeJS
)和我的客户端应用程序( ReactJS
)。
I have a directory that contains two folder : my server app (NodeJS
) and my client app (ReactJS
).
我的目录结构如下:
root/
|- client/
|- package.json
|- src/
|- ...
|- server/
|- package.json
|- src/
|- ...
我如何在目录的根目录中添加 common
文件夹,以便与服务器和客户端共享枚举和实用程序?
How can I add a common
folder at the root of my directory to share enums and utils with both my server and my client ?
谢谢!
推荐答案
您有多种解决方案:
-
在根目录下创建一个
常用
文件夹,只需要从文件中获取所需的文件即可。但是您最终可能会遇到长要求,例如require( ../../../../../ common / file)
create a folder
common
under root and just require the files you need from your files. But you could end up with "long" require such as require("../../../../../common/file")
使用module-alias避免该问题: https://github.com/ilearnio/module-别名
use module-alias to avoid that problem: https://github.com/ilearnio/module-alias
您可以将 common
设为本地模块(使用文件:
)并将其安装在package.json
https://docs.npmjs.com/files/package.json#local-paths
you could make common
a local module (using file:
) and install it in package.json
https://docs.npmjs.com/files/package.json#local-paths
{
"name": "common",
"dependencies": {
"common": "file:../common"
}
}
然后,您只需轻松地需要您所需要的
Then you can just require what you need easily
这篇关于javascript在我的客户端和服务器文件夹之间共享代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!