standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.26k stars 146 forks source link

Customized REPL #854

Open bibendi opened 4 years ago

bibendi commented 4 years ago

Hi!

In the documentation I see:

image

But I want to customize some configuration options in REPL. So, I have console.js file bellow:

import repl from 'repl'
import fs from 'fs'

const replServer = repl.start({
  prompt: 'app > ',
  useColors: true,
  replMode: repl.REPL_MODE_SLOPPY
})

const histFile = process.env.NODE_REPL_HISTORY || `${__dirname}/log/.node_repl_history`

try {
  if (fs.existsSync(histFile)) {
    fs.readFileSync(histFile, 'utf-8')
      .split('\n')
      .reverse()
      .filter(line => line.trim())
      .map(line => replServer.history.push(line))
  }
} catch (err) {
  console.error(err)
}

replServer.on('line', newLine => {
  const line = newLine.trim()
  if (line) {
    fs.appendFileSync(histFile, line + '\n')
  }
})

And run it like: node -r esm --experimental-repl-await console.js

When I trying to import a module I get this:

app > import { User } from './models/user'
Thrown:
import { User } from './models/user'
^^^^^^

SyntaxError: Cannot use import statement outside a module

Is there any chance to get the code above to work without errors? Thanks!