导出为ES6模块

时间:2018-12-13 21:05:06

标签: node.js babel es6-modules babel-loader

我最近开始将存储库升级到babel 7和webpack 4,并且在我的函数中遇到以下错误:

SyntaxError: Unexpected token export
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)

在使用babel进行构建时,我还会收到以下警告:

WARNING in ./server/lib/loader.js 226:16-22
"export 'lineLogger' (imported as 'logger') was not found in './logger'
 @ multi ./server/lib/loader.js

这是我的代码:

    const { createLogger, format, transports }  = require('winston');
const { LOG_LEVEL: level } = require('../config')

const consoleTransport = [
  new transports.Console({
    level,
    colorize: true
  })
];

const lineLogger = createLogger({
  format: format.combine(
    format.colorize(),
    format.timestamp(),
    format.align(),
    format.printf(info => {
      const { timestamp, level, message, ...rest} = info;

      return `${timestamp} [${level}] ${message}\n ${Object.keys(rest).length ? JSON.stringify(rest, null, 2) : ''}`
    })
  ),
  transports: consoleTransport,
  exceptionHandlers: [
    new transports.Console({ colorize: true })
  ]
});

export default {
  lineLogger
}

我到底该如何出口?在我之前:

exports.lineLogger = createLogger({

但是,这不再起作用了,那么我得到如下错误:无法导出只读bla bla bla。我真的很感谢您的帮助。什么时候应该使用import和什么时候需要require,什么时候应该使用module.exports或导出默认值。

0 个答案:

没有答案