简体   繁体   English

使用require时如何找到.js文件的正确路径

[英]How to locate the right path for a .js file when using require

I have an ExpressJS project where I want to use .js module inside another. 我有一个ExpressJS项目,我想在另一个项目中使用.js模块。 I use require to get the .js module but it can't find the module because the path is not correct. 我使用require获取.js模块,但由于路径不正确而无法找到该模块。 How can I see or find what is the correct path for a local module. 如何查看或找到本地模块的正确路径。

Here is my project hierarchy/structure 这是我的项目层次结构 在此处输入图片说明

This is what I have tried inside programController.js - another thing is I don't quite understand using "." 这是我在programController.js内部尝试过的-另一件事是我不太了解使用“。” in path string. 在路径字符串中。

var Program = require('.././program.js');
var FinishProgram = require('.././finishProgram');

The . . and .. notation in paths represent a path relative to the file they're being required from. 路径中的..表示 对于它们所需要的文件的路径。

Meaning of . 的含义. and .. Wildcards ..通配符

. - inside the same directory this file is in (also referred to as the current working directory) -该文件位于同一目录内(也称为当前工作目录)

.. - inside the parent directory of the current directory ..当前目录的父目录中

When you use multiples of these together it creates a relative path pattern. 当您将它们的倍数一起使用时,它将创建一个相对路径模式。 So for programController.js to access program.js your path should be 因此,为了使programController.js访问program.js您的路径应为

var Program = require('../../models/program');

This path means, go up 2 folders to the App directory and locate the models folder, then load the file program.js . 此路径意味着将两个文件夹上移到App目录并找到models文件夹,然后加载文件program.js

Following the same rules, you can also access finishProgram.js 按照相同的规则,您还可以访问finishProgram.js

var FinishProgram = require('../../models/finishProgram');

. represents the current directory and is redundant in your case. 代表当前目录,在您的情况下是多余的。

The paths are simple relative paths that are used to determine how to navigate to the required files. 这些路径是简单的相对路径,用于确定如何导航到所需文件。

This should work for you: 这应该为您工作:

var Program = require('../../models/program');
var FinishProgram = require('../../models/finishProgram');

Note that the extension .js is not required. 请注意,不需要扩展名.js

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

相关问题 如何使用JavaScript在codeigniter中定位/重定向控制器文件的路径? - How to locate/redirect the path of controllers file in codeigniter using javascript? 覆盖要求在Intern JS中具有文件扩展名的路径 - Overwriting require path with file extension in intern js 使用mongo nodejs require('file.js') module.exports时如何处理结果 - how to process the result when using mongo nodejs require('file.js') module.exports Angular js:与require js一起使用时如何使用resolve - Angular js : How to use resolve when using with require js 使用Require JS加载js文件 - Loading js file using Require JS Require JS with Knockout components 正在寻找不正确路径中的 js 文件 - Require JS with Knockout components is looking for js file in incorrect path 需要JS-r.js优化-如何正确执行此操作? - Require JS - r.js optimization - how to do this right? 如何从npm包中请求替代js文件而不指定完整的node_modules路径 - How to require an alternative js file from a npm package without specifiying full node_modules path 如何在没有路径问题的情况下要求来自外部源/目录的 JS 文件? - How to require a JS file from an external source/directory without path issues? 如何在 react naitve 中从一个 index.js 文件中使用 require() 导出图像路径? - How can I export images path with require() from one index.js file in react naitve?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM