简体   繁体   English

将commonjs模块与ES6模块混合以导出两个功能

[英]mixing commonjs module with ES6 module to export two functions

I have this file: commonutils.js 我有这个文件:commonutils.js

import { isBoolean, isNil  } from 'lodash'; // isNil , check undefined or null
import moment from 'moment';

let dateToISO = function (dateString) {
  if (!dateString) {
    return null;
  }
  let p = dateString.split(/\D/g);
  return [p[2], p[1], p[0]].join('-');
}
let ISOtoDate = function (dateString) {
  if ( isNil(dateString) || dateString === '') {
    return  '';
  }
  return moment(dateString).format('DD-MM-YYYY');
}

module.exports.dateToISO = dateToISO;
module.exports.ISOtoDate = ISOtoDate;

when I try to import on webstorm , webstorm complete the names to imports when I type: 当我尝试在webstorm上导入时,webstorm在键入时完成要导入的名称:

import { dateToISO,  ISOtoDate } from './commonutils';

but when i execute, i get this error: 但是当我执行时,出现此错误:

./src/utils/validators.js
8:10-19 './commonutils' does not contain an export named 'dateToISO'.

what are I'm doing wrong? 我在做什么错?

update: 更新:

importing with this: 与此一起导入:

const { dateToISO,  ISOtoDate } = require('./commonutils');

I get : 我得到:

TypeError: Cannot set property 'dateToISO' of undefined
> module.exports.dateToISO = dateToISO;

You are using a CommonJS module export syntax with an ES6 import. 您正在使用带有ES6导入的CommonJS模块导出语法。 For import, you need to do something like: 对于导入,您需要执行以下操作:

const { dateToISO,  ISOtoDate } = require('./commonutils');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM