Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从文件中调用和重用函数?_Javascript_Node.js_Ecmascript 6 - Fatal编程技术网

Javascript 如何从文件中调用和重用函数?

Javascript 如何从文件中调用和重用函数?,javascript,node.js,ecmascript-6,Javascript,Node.js,Ecmascript 6,在下面的脚本中,我想阅读config.toml的内容,并在我想做的地方做了标记。让我困惑的是如何包含包含toml reader函数的toml.js文件 在整个脚本中,我需要读取3个不同的toml文件 问题 我应该如何包含toml.js函数,以及如何重用该函数来读取3个不同的文件 免责声明:我很抱歉这个超级noob的问题,但这是我的第一个项目,与,我发现它非常混乱 index.js 'use strict' var minimist = require('minimist') const toml

在下面的脚本中,我想阅读
config.toml
的内容,并在我想做的地方做了标记。让我困惑的是如何包含包含toml reader函数的
toml.js
文件

在整个脚本中,我需要读取3个不同的toml文件

问题

我应该如何包含
toml.js
函数,以及如何重用该函数来读取3个不同的文件

免责声明:我很抱歉这个超级noob的问题,但这是我的第一个项目,与,我发现它非常混乱

index.js

'use strict'
var minimist = require('minimist')
const toml = require('./src/toml')

module.exports = () => {
var argv = minimist(process.argv.slice(2), {
  string: 'input',
  string: 'project',
  boolean: ['validate'],
  boolean: ['help'],
  alias: { i: 'input', v: 'validate', h: 'help', p: 'project' },
  unknown: function () { console.log('Unkown argument') }
})

  if (argv.help || argv.h) {
    // help output goes here
  }

  // read config.toml

}
module.exports = (filename) => {
  const TOML = require('@iarna/toml')
  const fs = require('fs');

  return TOML.parse(fs.readFileSync(filename, 'utf-8'));
}
src/toml.js

'use strict'
var minimist = require('minimist')
const toml = require('./src/toml')

module.exports = () => {
var argv = minimist(process.argv.slice(2), {
  string: 'input',
  string: 'project',
  boolean: ['validate'],
  boolean: ['help'],
  alias: { i: 'input', v: 'validate', h: 'help', p: 'project' },
  unknown: function () { console.log('Unkown argument') }
})

  if (argv.help || argv.h) {
    // help output goes here
  }

  // read config.toml

}
module.exports = (filename) => {
  const TOML = require('@iarna/toml')
  const fs = require('fs');

  return TOML.parse(fs.readFileSync(filename, 'utf-8'));
}

您应该能够调用
toml('path/to/config/that/You/want/to/read.toml')

您需要一个模块src/toml.js。此模块导出一个函数-在本例中,如何声明此函数并不重要。无论何时导入该模块,都会获得此函数

因此:

const icallthisreferencehoweveriwant=require('./src/toml');
我将此引用称为howeveriwant('path/to/a/toml/file.toml');
我认为您需要在函数定义之外的
toml.js
中要求您的依赖关系-我想它可能会对您大喊大叫,但我对它没有超级自信:)

命题+一些重构:

src/toml.js

'use strict'
var minimist = require('minimist')
const toml = require('./src/toml')

module.exports = () => {
var argv = minimist(process.argv.slice(2), {
  string: 'input',
  string: 'project',
  boolean: ['validate'],
  boolean: ['help'],
  alias: { i: 'input', v: 'validate', h: 'help', p: 'project' },
  unknown: function () { console.log('Unkown argument') }
})

  if (argv.help || argv.h) {
    // help output goes here
  }

  // read config.toml

}
module.exports = (filename) => {
  const TOML = require('@iarna/toml')
  const fs = require('fs');

  return TOML.parse(fs.readFileSync(filename, 'utf-8'));
}
const TOML=require(“@iarna/TOML”)
常数fs=要求('fs');
const readTOML=(filename)=>TOML.parse(fs.readFileSync(filename,'utf-8');
module.exports=readTOML;

您应该能够调用
toml('path/to/config/that/You/want/to/read.toml')

您需要一个模块src/toml.js。此模块导出一个函数-在本例中,如何声明此函数并不重要。无论何时导入该模块,都会获得此函数

因此:

const icallthisreferencehoweveriwant=require('./src/toml');
我将此引用称为howeveriwant('path/to/a/toml/file.toml');
我认为您需要在函数定义之外的
toml.js
中要求您的依赖关系-我想它可能会对您大喊大叫,但我对它没有超级自信:)

命题+一些重构:

src/toml.js

'use strict'
var minimist = require('minimist')
const toml = require('./src/toml')

module.exports = () => {
var argv = minimist(process.argv.slice(2), {
  string: 'input',
  string: 'project',
  boolean: ['validate'],
  boolean: ['help'],
  alias: { i: 'input', v: 'validate', h: 'help', p: 'project' },
  unknown: function () { console.log('Unkown argument') }
})

  if (argv.help || argv.h) {
    // help output goes here
  }

  // read config.toml

}
module.exports = (filename) => {
  const TOML = require('@iarna/toml')
  const fs = require('fs');

  return TOML.parse(fs.readFileSync(filename, 'utf-8'));
}
const TOML=require(“@iarna/TOML”)
常数fs=要求('fs');
const readTOML=(filename)=>TOML.parse(fs.readFileSync(filename,'utf-8');
module.exports=readTOML;

看起来您已正确设置了所有内容。在index.js内部,您应该能够使用
toml('filename.toml')
。此函数将filename.toml的内容作为对象返回

src/toml.js导出解析.toml文件的函数。当您在index.js中使用
const toml=require('./src/toml')
时,您将
toml
分配给./src/toml.js的导出(这是解析函数)。这意味着在index.js中,
toml
表示./src/toml.js中的函数

然后,您可以在index.js中任意多次使用
toml('filename.toml')

下面是修改后的index.js代码,用于读取config.toml文件并将对象存储在
config

'use strict'
var minimist = require('minimist')
const toml = require('./src/toml')

module.exports = () => {
  var argv = minimist(process.argv.slice(2), {
    string: 'input',
    string: 'project',
    boolean: ['validate'],
    boolean: ['help'],
    alias: { i: 'input', v: 'validate', h: 'help', p: 'project' },
    unknown: function () { console.log('Unkown argument') }
  })

  if (argv.help || argv.h) {
    // help output goes here
  }

  // read config.toml
  const config = toml('config.toml')

}

看起来你把一切都安排好了。在index.js内部,您应该能够使用
toml('filename.toml')
。此函数将filename.toml的内容作为对象返回

src/toml.js导出解析.toml文件的函数。当您在index.js中使用
const toml=require('./src/toml')
时,您将
toml
分配给./src/toml.js的导出(这是解析函数)。这意味着在index.js中,
toml
表示./src/toml.js中的函数

然后,您可以在index.js中任意多次使用
toml('filename.toml')

下面是修改后的index.js代码,用于读取config.toml文件并将对象存储在
config

'use strict'
var minimist = require('minimist')
const toml = require('./src/toml')

module.exports = () => {
  var argv = minimist(process.argv.slice(2), {
    string: 'input',
    string: 'project',
    boolean: ['validate'],
    boolean: ['help'],
    alias: { i: 'input', v: 'validate', h: 'help', p: 'project' },
    unknown: function () { console.log('Unkown argument') }
  })

  if (argv.help || argv.h) {
    // help output goes here
  }

  // read config.toml
  const config = toml('config.toml')

}