在javascript中的require中传递变量

Passing a variable in the require in javascript

本文关键字:变量 require javascript 中的      更新时间:2023-09-26

我使用的是javascript和node.js。我的代码中有以下几行:

var countryName = "Turkey";
var jsonPath = '../'+countryName+'/PersonalDetails.json';
console.log(jsonPath);
var PersonalDetails = require(jsonPath);

这会出现错误:找不到模块".."/土耳其/PersonalDetails.json'

但是当我使用时

var PersonalDetails = require('../Turkey/PersonalDetails.json'); 

它运行良好。我错过了什么?

还附上了我的json文件以供参考。

{
    "fields":[
                {
                    "fieldType":"Radio",
                    "fieldLabel":"Relevance to the transaction :",
                    "disclosureField":""    ,
                    "fieldList":["One carrying out the transaction","One on behalf of him/her the transaction is carried out","Indirect Relevance"]
                },
                {
                    "fieldType":"Radio",
                    "fieldLabel":"Is ID Card False",
                    "disclosureField":"",
                    "fieldList":["Yes","No"]
                },
                {
                    "fieldType":"Radio",
                    "fieldLabel":"Sex :",
                    "disclosureField":"gender",
                    "fieldList":["Male","Female","Unknown"]
                },
                {
                    "fieldType":"TextInput",
                    "fieldLabel":"First Name",
                    "disclosureField":"identity.first_name",
                    "fieldList":[]
                },
                {
                    "fieldType":"TextInput",
                    "fieldLabel":"SurName",
                    "disclosureField":"identity.last_name",
                    "fieldList":[]
                },
                {
                    "fieldType":"NumberInput",
                    "fieldLabel":"TR ID Number :",
                    "disclosureField":"",
                    "fieldList":[]
                },
                {
                    "fieldType":"NumberInput",
                    "fieldLabel":"Tax Number :",
                    "disclosureField":"",
                    "fieldList":[]
                },
                {
                    "fieldType":"TextInput",
                    "fieldLabel":"Mother's Name :",
                    "disclosureField":"",
                    "fieldList":[]
                },
                {
                    "fieldType":"TextInput",
                    "fieldLabel":"Father's Name :",
                    "disclosureField":"",
                    "fieldList":[]
                },
                {
                    "fieldType":"TextInput",
                    "fieldLabel":"Nationality :",
                    "disclosureField":"nationality",
                    "fieldList":[]
                },
                {
                    "fieldType":"TextInput",
                    "fieldLabel":"Occupation :",
                    "disclosureField":"occupation",
                    "fieldList":[]
                },
                {
                    "fieldType":"TextInput",
                    "fieldLabel":"Additional Information :",
                    "disclosureField":"",
                    "fieldList":[]
                }
                ]
}

您可以定义一个路径链接如下:

var path = require(__dirname + "yourpath");

让我知道这是否有助于

编辑:

var jsonPath = require(__dirname + '/../' + countryName + '/PersonalDetails.json');