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 ofpath.normalize('./../plugs/myPlug')
) – regina_fallangi Commented Jun 10, 2015 at 11:38
2 Answers
Reset to default 2I 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
版权声明:本文标题:javascript - Node.js require path stored in a variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741930440a2405543.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论