admin管理员组

文章数量:1313386

I am testing some nodejs code, and this is how my directory looks like:

-> source  //NODE PATH=./source ...
-> plugs
   -myPlug.js
   -test.js

In test.js I try to require myPlug.js like this:

function(){
     var myRequiredPlug = require('./myPlug.js') //this works
}

Since the NODE PATH is source, I have also tried:

function(){
     var myRequiredPlug = require('./../plugs/myPlug') //also works
}

But I will have to require a different plug every time for my app, so I would very much like to create the path this way:

 function(nameOfPlug){  // nameOfPlug := myPlug
     var myPath = './../plugs/' + nameOfPlug;
     console.log(myPath === './../plugs/myPlug') // true, so same string
     var myRequiredPlug = require(myPath);  
}

When I try it his way, I get the error: Error: Cannot find module './../plugs/myPlug'

I have already tried path.normalize, and even to join the paths with path.join, but get the same results. Any ideas?

Update: Answer

This answer can be solved using RequireJS, Dynamic require in RequireJS, getting "Module name has not been loaded yet for context" error?

I am testing some nodejs code, and this is how my directory looks like:

-> source  //NODE PATH=./source ...
-> plugs
   -myPlug.js
   -test.js

In test.js I try to require myPlug.js like this:

function(){
     var myRequiredPlug = require('./myPlug.js') //this works
}

Since the NODE PATH is source, I have also tried:

function(){
     var myRequiredPlug = require('./../plugs/myPlug') //also works
}

But I will have to require a different plug every time for my app, so I would very much like to create the path this way:

 function(nameOfPlug){  // nameOfPlug := myPlug
     var myPath = './../plugs/' + nameOfPlug;
     console.log(myPath === './../plugs/myPlug') // true, so same string
     var myRequiredPlug = require(myPath);  
}

When I try it his way, I get the error: Error: Cannot find module './../plugs/myPlug'

I have already tried path.normalize, and even to join the paths with path.join, but get the same results. Any ideas?

Update: Answer

This answer can be solved using RequireJS, Dynamic require in RequireJS, getting "Module name has not been loaded yet for context" error?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jun 10, 2015 at 9:27 regina_fallangiregina_fallangi 2,1982 gold badges21 silver badges42 bronze badges 4
  • 3 Do you mean console.log(myPath === './../plugs/nameOfPlug') (with quotes around the string)? – jfriend00 Commented Jun 10, 2015 at 9:36
  • jfriend00, yes, I meat it with '...', thanks. And nameOfPlugs is just the name of the argument. When testing I pass myPlug as nameOfPlugs. – regina_fallangi Commented Jun 10, 2015 at 10:19
  • Try require('../plugs/myPlug') instead of require('./../plugs/myPlug') – Viktor Kireev Commented Jun 10, 2015 at 10:26
  • @victork, it does not work, as I already said in the question. ('../plugs/myPlug' is the return value of path.normalize('./../plugs/myPlug')) – regina_fallangi Commented Jun 10, 2015 at 11:38
Add a ment  | 

2 Answers 2

Reset to default 2

I use pound lines, but not pletely.

Wrong:

const path = './some/path.file';
const data = require(`${path}`);

Right:

const path = 'file';
const data = require(`./some/${path}.file`);
function(nameOfPlug){  // nameOfPlug := myPlug
     var myPath = './../plugs/' + nameOfPlug;
     console.log(myPath === './../plugs/myPlug') // true, so same string
     var myRequiredPlug = require('' + myPath);  
}

本文标签: javascriptNodejs require path stored in a variableStack Overflow