3

This seems like just a normal module and export, not sure what's causing this.

myES6Module.js

const showCar = () => {
   //...code
}

const drive = () => {
  //...code
};

drive();

export { drive, showCar }

What's weird is in my tests I'm able to import and call these just fine and my tests use them and pass. But when I actually run the drive() which runs the app by prompting the user for terminal input, I get an error saying:

SyntaxError: Unexpected token export at new Script (vm.js:74:7) at createScript (vm.js:246:10) at Proxy.runInThisContext (vm.js:298:10) at Module._compile (internal/modules/cjs/loader.js:670:28)

Why would this resolve just fine for test but not live running of the code?

Here's how I'm running it, a script in my package.json:

"start": "node --experimental-modules ./myES6Module.js"

so it's when I run yarn start I get this. Otherwise, when I run my tests, drive() outputs to the console just fine.

if I comment out that exports, my script runs fine...but of course that breaks my tests which rely on exporting stuff.

UPDATE

I'm using --experimental-modules

So I tried this since I have babel-cli installed:

"start": "babel ./myES6Module.js"

package.json has the following babel packages:

"@babel/cli": "^7.0.0-beta.51", "@babel/preset-env": "^7.0.0-beta.51", "@babel/register": "^7.0.0-beta.51", "@babel/core": "^7.0.0-beta.51",

but that just console.logged the file content, it didn't run it. I don't want to use --experimental-modules either. I don't want to change my file extension so how do I get this running?

I took a look at this and it mentions about migrating if you are already using babel-node but is that the only way?

https://babeljs.io/docs/en/next/v7-migration

PositiveGuy
  • 17,621
  • 26
  • 79
  • 138
  • I think this might have already been answered here https://stackoverflow.com/questions/38296667/getting-unexpected-token-export – Ash Jun 29 '18 at 00:20
  • 1
    yea but `--experimental-modules` is supposed to solve this issue I thought. https://nodejs.org/api/esm.html so that I don't need babel for this – PositiveGuy Jun 29 '18 at 00:21
  • ah crap the file extension has to be mjs – PositiveGuy Jun 29 '18 at 00:23
  • I think you're just missing the `.mjs` extension (edit: sorry had the page open, looks like you figured it out :) ) – Ash Jun 29 '18 at 00:25
  • yea that's it. So I took out the experimental and tried babel. I'd like to get this to work now without --experimental-modules – PositiveGuy Jun 29 '18 at 00:27
  • well I'm using `@babel/register` and yes I do use that to run my mocha tests but trying to use it for my node script, I just tried `"start": "node @babel/register ./myES6Module.js"` but that's not quite right – PositiveGuy Jun 29 '18 at 00:40

1 Answers1

0

To answer your initial question, you need to have the file extension for the files to be .mjs so that you can define in your package.json scripts:

"start": "node --experimental-modules myES6Module.mjs"

The second part about removing the --experimental-modules flag as you noted just logged out the file contents when using babel. To get around this you can use babel-node but note its warning on use in production. In that warning you will find a link to Example Node Server w/ Babel demonstrating a working solution.

To produce the results you are after I've created a minimum working solution with the following which is working for me:

npm i babel-cli babel-preset-es2015

Update your package.json to:

"start": "babel-node --presets es2015 myES6Module.js"

And a working .js file using export:

const showCar = () => {
  //...code
}

const drive = () => {
  //...code
  console.log('driving')
};

drive();

export { drive, showCar }

Babel 7

If you want to use version 7 then try below.

npm i @babel/cli @babel/core @babel/node @babel/preset-env so your package.json has the below dependencies:

"@babel/cli": "^7.0.0-beta.51",
"@babel/core": "^7.0.0-beta.51",
"@babel/node": "^7.0.0-beta.51",
"@babel/preset-env": "^7.0.0-beta.51"

Set the run script to

"start": "babel-node myES6Module.js"

Create a .babelrc file at the root level of your project with

{
  "presets": ["@babel/preset-env"]
}

Now you can execute npm run start from the terminal and you should see the output driving logged to the terminal based on my example code.

I haven't used version 7 before and the docs say that the .babelrc file should use "presets": ["env"] but I got an error, however the above and "presets": ["@babel/env"] worked the same. Someone else might know the reasoning behind this error I ran into but that's for another question.

Ash
  • 11,292
  • 4
  • 27
  • 34