Import function - syntax error - unexpected token
This error shows my naivety I'm afraid but I'd be grateful for your help!
I'm attempting to import this function from a js file (calculator.js)
export function Add ( a, b )
{
return a + b;
}
When I referenced this function from my app.js, Webstorm cleverly auto-imported it for me and created this statement at the top of my app.js
import {Add} from "./Calculator";
Unfortunately, when I attempt to run app.js, I get the following error:
import {Add} from "./Calculator";
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:718:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Module.load (internal/modules/cjs/loader.js:641:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
at internal/main/run_main_module.js:17:11
Process finished with exit code 1
I'd appreciate some guidance on what I'm doing wrong!
Please sign in to leave a comment.
you have to pre-compile your files to be able to run them, Node.js doesn't support using ES6 modules in .js files. The easiest way to do this is passing
as Node parameters: in run configuration:--require @babel/registerOf course, you need to make sure to install the corresponding modules and set up the .babelrc accordingly
package.json:
"dependencies": {"@babel/cli": "^7.2.3",
"@babel/core": "^7.4.0",
"@babel/preset-env": "^7.4.2",
"@babel/register": "^7.4.0",
...
}
.babelrc:
{"presets": [
[
"@babel/preset-env"
]
]
}
That worked. Thank you so much!