将 node_modules 文件夹用于自定义模块 [英] Using node_modules folder for custom modules
问题描述
最近我开始计划这个结构在哪里:
recently I got to project where is this structure:
- app.js <- init 脚本,它将启动 lib/index.js
- node_modules <- 从 npm 获取的模块
- 库
- index.js
- 资源 - 系统中实体的一些代码
- node_modules - 系统中使用的本地模块(不是来自 npm)
您如何看待将名为 node_modules 的文件夹用于未从 npm 获取的本地代码?
What do you think about using folder named node_modules for local code which is not fetched from npm?
唯一的好处是,从资源请求模块时,您不必使用相对路径.
The only plus is, that you don't have to work with relative paths when requesting module from resource..
不利的部分是,有许多脚本(jshint、nodemon、..)忽略了这些文件夹,而且您还必须在 .gitignore 中允许此文件夹
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
推荐答案
正是因为您所描述的原因,我将其称为不好的做法.最终有些东西会停止工作,需要很长时间才能找出原因.
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.
如果您必须解决节点中的相对路径问题,我会使用支持的解决方案,即使它不理想,即 NODE_PATH 环境变量
If you have to work around the relative path problem in node I would go with the supported solution, even if its not ideal, i.e. the NODE_PATH environment variable
NODE_PATH=path\to\program node myprogram.js
关于替代选项的讨论很好在 github 上
There is a good discussion about the alternative options here on github
这篇关于将 node_modules 文件夹用于自定义模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!