我有这个文件: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;
当我尝试在webstorm上导入时,当键入以下内容时,webstorm完成要导入的名称:
import { dateToISO, ISOtoDate } from './commonutils';
但是当我执行时,出现此错误:
./src/utils/validators.js
8:10-19 './commonutils' does not contain an export named 'dateToISO'.
我在做什么错?
更新:
与此导入:
const { dateToISO, ISOtoDate } = require('./commonutils');
我得到:
TypeError: Cannot set property 'dateToISO' of undefined
> module.exports.dateToISO = dateToISO;
最佳答案
您正在使用带有ES6导入的CommonJS模块导出语法。对于导入,您需要执行以下操作:
const { dateToISO, ISOtoDate } = require('./commonutils');