javascript - 当我有更多后端 javascript 文件时,是否应该将 node_modules 放入单独的文件夹中?

标签 javascript node.js model-view-controller

我的 Node.js 项目具有以下结构:

app
|
|-public
|  |-assets
|
|-node_modules
|
|-views
|
|-index.js

我在index.js中有很多代码。所以我决定将它们分成不同的文件并将它们放入名为 controllers 的文件夹中。 。但由于模块引用了 node_modules 文件夹,该文件夹不存在于 controllers 中。文件夹,我找不到模块

那么搬家是个好主意node_modules controllers 内的文件夹或者是否有任何更有效的方法来管理大量后端js文件,无论是否将它们放入单个文件夹中。

IF at all I move the node_modules into controllers is there any chance that I might fall into a trap while developing the app later down the road ? ( This is a huge project . So asking ).

我知道我可以通过给出require("./controllers/module.js")直接将这些模块包含在index.js中但模块本身依赖于许多 Node 模块。所以我需要访问node_modules。

最佳答案

没有。不。 您不需要移动node_modules。它将保留在您的项目根目录中。并且您没有将其添加到需求中。

您可能会做什么和不应该做什么的示例:

const module = require('./node_modules/mymodule');

不要这样做

那没有必要。如果您移动文件,您将面临现在面临的问题。

Node 如何知道在node_modules中哪里可以找到模块?这是一些解释它的文章:https://medium.freecodecamp.org/requiring-modules-in-node-js-everything-you-need-to-know-e7fbd119be8

tl;dr:更改您的需求并删除对 node_modules 的引用。 Node会自己找到它

关于javascript - 当我有更多后端 javascript 文件时,是否应该将 node_modules 放入单独的文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48169345/

相关文章:

java - MVC 与 Taglibs,意见/替代方案?

ios - 在可重用的 UIView 中,我应该将 UIButtons 的选择器放在哪里?

javascript - 在 flask 中处理 javascript 输出后重新加载页面

javascript - 如何正确运行 jumbo react Jumbo React - React Redux Material BootStrap 管理模板?

javascript - Node.js 抓取数据点击事件

javascript - promisifyAll 是如何工作的,或者它的工作要求是什么?

CakePhp 3 - 如何扩展助手

Javascript:运行函数后添加 'false'

javascript - 使用 jQuery 多次设置元素文本时,连续的 .setTimeouts 跳转 .setTimeouts 的中间实例

javascript - Socket.io 在使用命名空间时不发出/接收事件