gpt4 book ai didi

node.js - 如何在nodejs中定义文件路径需要('')和 "../"符号?

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:47 24 4
gpt4 key购买 nike

我在 Nodejs 中定义路径时遇到问题。

我需要用一些 ../../ 定义路径,但我真的不明白如何使用这个 .. 符号。

示例:

var core=require('../../app/server/controllers'); 

在 Meanjs 0.3 中效果很好。

现在我更改为 Mean 0.4,其文件夹结构略有不同,这占用了我的时间定义路径。

任何人都可以对使用单个 . 定义路径的点表示法给出一些帮助解释吗?

目标是在自定义目录 restaurants.server.controller.js 中定义我的 core/server/controller/core.server.controller.js 文件的路径

截图: enter image description here

最佳答案

您可以使用此帮助来解决路径问题 https://gist.github.com/branneman/8048520

一些解决方案

1).全局

In your app.js:

global.__base = __dirname + '/';

In your very/far/away/module.js:

var Article = require(__base + 'app/models/article');

2).模块

Install some module:

npm install app-module-path --save

In your app.js, before any require() calls:

require('app-module-path').addPath(__dirname + '/app');

In your very/far/away/module.js:

var Article = require('models/article');

关于node.js - 如何在nodejs中定义文件路径需要('')和 "../"符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31072512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com