Does Node 11 REPL support import?

Viewed 840

Based on this conversation I was hoping it might: https://github.com/nodejs/node/pull/22381

But when I tried it:

✗ nvm exec 11 node --experimental-modules  
Running node v11.1.0 (npm v6.4.1)
> (node:31601) ExperimentalWarning: The ESM module loader is experimental.

> 
> let pt = require("./src/constants/paymentTypes.js")
/Users/jcol53/work/orders/src/constants/paymentTypes.js:1
(function (exports, require, module, __filename, __dirname) { import { defineMessages, formatMessage } from '@tools/shared';
                                                                     ^

SyntaxError: Unexpected token {

Same result with ✗ nvm exec 11 node --experimental-vm-modules

Is import going to be supported in the Node 11 or Node 12 REPL?

1 Answers

It does support but only as experimental feature, it means

  • you should use --experimental-modules flag
  • javascript file with "import" shall have extension .mjs

So the full command will look like

node --experimental-modules index.mjs

In your case 'paymentTypes.js' should be renamed to 'paymentTypes.mjs'.

Related