English 中文(简体)
如何在javascript中积极使用“要求”?
原标题:How to use "require" dynamically in javascript?

在“mple.js”档案中,我有一张 j印功能。 情况如下:

varmapDict ={100 : test_100.js , 200 : test_200_API.js , 300 : test_300_API.js }

function mapAPI()
{
    this.version = 0.1;
}

mapAPI.prototype.getFileName = function( id ) {
   return mapDict[id]
}

module.exports = mapAPI;

在另一个名为“机构......js”的档案中,我想动态地要求上述“测试——xxx_API”档案。 我有以下法典:

const mapAPI  = require( ../../sample.js );
const map     = new mapAPI();
const mapFile = map.getFileName("100");
var insAPI    = require(mapFile);

当我由“Nodeinstitut.js”指挥实施该守则时,我发现以下错误:

Error: Cannot find module  ./test_100_API.js .

But the "test_100_API.js" file exists and is located in the current folder besides "institute.js". When I changed var insAPI = require(mapFile); to var insAPI = require("./test_100_API.js"); and give it the exact path instead of dynamic path, it works fine. Can anyone help me?

提前

问题回答

你再次碰到的问题,因为Node.js要求根据目前的工作名录(在你管理着 no)而不是包含所需说明的档案,对单元采取相对途径。

In your case, mapFile contains just the filename ( test_100_API.js ), but Node.js is trying to find it in the current directory where you are running node institute.js. To fix this and allow for dynamic requires based on filenames stored in mapDict, you can modify your institute.js code as follows:

const path = require( path );
const mapAPI = require( ../../sample.js );

const map = new mapAPI();
const mapFile = map.getFileName("100");

// Construct the full path to the module using path.join
const modulePath = path.join(__dirname, mapFile);

// Now require the module using the constructed path
const insAPI = require(modulePath);

// Now you can use insAPI as needed




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签