简体   繁体   English

将 node_modules 文件夹用于自定义模块

[英]Using node_modules folder for custom modules

recently I got to project where is this structure:最近我开始计划这个结构在哪里:

  • app.js <- init script, which will start lib/index.js app.js <- init 脚本,它将启动 lib/index.js
  • node_modules <- Modules fetched from npm node_modules <- 从 npm 获取的模块
  • lib
    • index.js索引.js
    • resources - some code for entities in system资源 - 系统中实体的一些代码
    • node_modules - local modules used in system (not from npm) node_modules - 系统中使用的本地模块(不是来自 npm)

What do you think about using folder named node_modules for local code which is not fetched from npm?您如何看待将名为 node_modules 的文件夹用于未从 npm 获取的本地代码?

The only plus is, that you don't have to work with relative paths when requesting module from resource..唯一的优点是,从资源请求模块时,您不必使用相对路径。

Negative part is, that there are many scripts (jshint, nodemon, ..) which are ignoring these folders and also you have to permit this folder also in .gitignore不利的部分是,有许多脚本(jshint、nodemon 等)忽略了这些文件夹,而且您还必须在 .gitignore 中允许此文件夹

I would call it bad practice for exactly the reason you described.正是出于您所描述的原因,我将其称为不好的做法。 Eventually something will stop working and it will take a long time figure out why.最终有些东西会停止工作,需要很长时间才能找出原因。

If you have to work around the relative path problem in node I would go with the supported solution, even if its not ideal, ie the NODE_PATH environment variable如果您必须解决节点中的相对路径问题,我会使用支持的解决方案,即使它不理想,即NODE_PATH 环境变量

NODE_PATH=path\\to\\program node myprogram.js

There is a good discussion about the alternative options here on github关于 github 上的替代选项有一个很好的讨论

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM