GatsbyJS找不到模块'fs'

时间:2020-02-18 09:05:41

标签: reactjs gatsby fs mollie

我将gatsby( v 2.18.4 )用作前端。它工作正常,但是在我安装Mollie payment package

之后

我收到错误消息:

TypeError: fs__WEBPACK_IMPORTED_MODULE_1___default.a.readFileSync is not a function

我发现将其添加到 gatsby-config.js 文件中即可解决问题

exports.onCreateWebpackConfig = ({ actions }) => {
    actions.setWebpackConfig({
        node: {
            fs: 'empty',
            net: 'empty'
          }
    })
};

但是它不起作用。我收到错误的Mollie软件包中的代码是:

import fs from 'fs';

function createHttpClient(options) {
  const axiosOptions = cloneDeep(options);
  delete axiosOptions.apiKey;
  delete axiosOptions.versionStrings;
  axiosOptions.baseURL = 'https://api.mollie.com:443/v2/';

  if (undefined == axiosOptions.headers) {
    axiosOptions.headers = {};
  }

  axiosOptions.headers['Authorization'] = `Bearer ${options.apiKey}`;
  axiosOptions.headers['Accept-Encoding'] = 'gzip';
  axiosOptions.headers['Content-Type'] = 'application/json';
  var customVersionStrings = options.versionStrings;

  if (undefined == customVersionStrings) {
    customVersionStrings = [];
  } else if (false == Array.isArray(customVersionStrings)) {
    customVersionStrings = [customVersionStrings];
  }

  axiosOptions.headers['User-Agent'] = [`Node/${process.version}`, `Mollie/${version}`, ...customVersionStrings.map(versionString => {
    //                platform /version
    const matches = /^([^\/]+)\/([^\/\s]+)$/.exec(versionString);

    if (null === matches) {
      if (-1 == versionString.indexOf('/') || versionString.indexOf('/') != versionString.lastIndexOf('/')) {
        throw new Error('Invalid version string. It needs to consist of a name and version separated by a forward slash, e.g. RockenbergCommerce/3.1.12');
      } else {
        throw new Error('Invalid version string. The version may not contain any whitespace.');
      }
    } // Replace whitespace in platform name with camelCase (first char stays untouched).


    const platform = matches[1].replace(/([^^])(\b\w)/g, (match, boundary, character) => [boundary, character.toUpperCase()].join('')).replace(/\s+/g, '');
    const version = matches[2];
    return [platform, version].join('/');
  })].join(' ');
  axiosOptions.httpsAgent = new https.Agent({
    ///////////////////////////////////////////////
    //////////////// THIS LINE ///////////////////

    cert: fs.readFileSync(path.resolve(__dirname, './cacert.pem'), 'utf8') 
  });
  return axios.create(axiosOptions);
}

下一个代码中有错误:

axiosOptions.httpsAgent = new https.Agent({   
    cert: fs.readFileSync(path.resolve(__dirname, './cacert.pem'), 'utf8') 
  });

有什么办法解决吗?

0 个答案:

没有答案
相关问题