0

I recently started to updgrade my repository to babel 7 & webpack 4 and i'm getting the following error on a function of mine:

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)

Also when building with babel I get the following warnings:

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

here's my code:

    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
}

How on earth do I export this? before I had:

exports.lineLogger = createLogger({

however this does not work anymore, then I get the error like: can't export readonly bla bla bla. I would really appreciate some help. when should I use imports & when should I use require, also when should I do module.exports or export default.

Joelgullander
  • 1,376
  • 1
  • 13
  • 30
  • It appears that you are a) either not configuring webpack correctly to apply Babel to your source files or b) your accessing the original source files when starting your program or c) both. The syntax itself is correct. – Felix Kling Dec 13 '18 at 21:08
  • import { lineLogger as logger } from './logger'; – Joelgullander Dec 13 '18 at 21:13
  • Should I be able to import it like such? – Joelgullander Dec 13 '18 at 21:13
  • No, that's not correct since you have a default export. So either export it with `export const lineLogger = createLogger(...);` *or* import it as `import from './logger'; const logger = .logger;`. And that is why you get the warning. However, that is unrelated to the error you are getting, you need to fix the syntax error first. Since Babel seems to process your files, I can only assume that you are starting you program in such a way that it uses the original source files, not the transpiled files. – Felix Kling Dec 13 '18 at 21:15
  • ok I exported the function aswell above export default and now I get: 2018-12-13T21:17:05.974Z [error] uncaughtException: Unexpected token export /Users/XXX/server/lib/logger.js:11 export const lineLogger = createLogger({ – Joelgullander Dec 13 '18 at 21:18
  • Thanks, ill keep trying. – Joelgullander Dec 13 '18 at 21:18

0 Answers0