我的 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
intocontrollers
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/