且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

javascript在我的客户端和服务器文件夹之间共享代码

更新时间:2021-09-16 21:54:49

您有多种解决方案:


  • 在根目录下创建一个常用文件夹,只需要从文件中获取所需的文件即可。但是您最终可能会遇到长要求,例如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