简体   繁体   中英

how to define the file path in nodejs require('') with “../ ” Notations?

I am having trouble in defining paths in nodejs.

I need to define path with some ../../ But really I don't understand how to Use this .. notations.

Example:

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

in Meanjs 0.3 which worked good.

Now I changed to Mean 0.4 where its little different folder structure Which eating my time define path.

Could anybody give some helping explanation about this dot notation regarding path defining with a single . ?

Aim is to define the path of my core/server/controller/core.server.controller.js file in my custom directory restaurants.server.controller.js

Screenshot: 在此处输入图片说明

You Could use this Helps for Path issue https://gist.github.com/branneman/8048520

Some Solutions

1). The Global

In your app.js:

global.__base = __dirname + '/';

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

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

2). The Module

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');

Your restaurant.server.controller.js is in the modules/restaurants/server/controllers directory. ../ means go up one directory, so ../../../ (up three directories) would put you in /modules . Then you can find the core.server.controller.js by following the core/server/controllers directories.

So the final require you want for core is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM