我试图实现的是创建一个包含多个功能的模块。

module.js:

module.exports = function(firstParam) { console.log("You did it"); },
module.exports = function(secondParam) { console.log("Yes you did it"); }, 
// This may contain more functions

main.js:

var foo = require('module.js')(firstParam);
var bar = require('module.js')(secondParam);

我的问题是,firstParam是一个对象类型和secondParam是一个URL字符串,但当我有它总是抱怨类型是错误的。

我如何声明多个模块。在这种情况下出口?


当前回答

在这里加上某人的帮助:

这个代码块将帮助您在cypress index.js中添加多个插件 插件-> cypress-ntlm-auth和cypress env文件选择

const ntlmAuth = require('cypress-ntlm-auth/dist/plugin');
const fs = require('fs-extra');
const path = require('path');

const getConfigurationByFile = async (config) => {
  const file = config.env.configFile || 'dev';
  const pathToConfigFile = path.resolve(
    '../Cypress/cypress/',
    'config',
    `${file}.json`
  );
  console.log('pathToConfigFile' + pathToConfigFile);
  return fs.readJson(pathToConfigFile);
};

module.exports = async (on, config) => {
  config = await getConfigurationByFile(config);
  await ntlmAuth.initNtlmAuth(config);
  return config;
};

其他回答

你也可以像这样导出

const func1 = function (){some code here}
const func2 = function (){some code here}
exports.func1 = func1;
exports.func2 = func2;

或 对于这样的匿名函数

    const func1 = ()=>{some code here}
    const func2 = ()=>{some code here}
    exports.func1 = func1;
    exports.func2 = func2;

一种方法是在模块中创建一个新对象,而不是替换它。

例如:

var testone = function () {
    console.log('test one');
};
var testTwo = function () {
    console.log('test two');
};
module.exports.testOne = testOne;
module.exports.testTwo = testTwo;

然后打电话

var test = require('path_to_file').testOne:
testOne();

你可以写一个函数来手动委托其他函数:

module.exports = function(arg) {
    if(arg instanceof String) {
         return doStringThing.apply(this, arguments);
    }else{
         return doObjectThing.apply(this, arguments);
    }
};

你可以这样做:

module.exports = {
    method: function() {},
    otherMethod: function() {},
};

或者是:

exports.method = function() {};
exports.otherMethod = function() {};

然后在调用脚本中:

const myModule = require('./myModule.js');
const method = myModule.method;
const otherMethod = myModule.otherMethod;
// OR:
const {method, otherMethod} = require('./myModule.js');

你可以像我下面做的那样…对于函数和箭头函数:

greet.js:

函数greetFromGreet() { Console.log ("hello from greet module…"); } const greetVar = () => { Console.log("将var视为箭头fn/…"); }; 模块。exports = {greetVar, greetFromGreet};// ----多个模块导出…

// -----------------------------------------------

app.js:

const greetFromGreets = require("./greet");

greetFromGreets.greetFromGreet();
greetFromGreets.greetVar();

// -----------------------------------------------