In js file, i used import to instead of require
import co from 'co';
And tried to run it directly by nodejs since it said import is 'shipping features' and support without any runtime flag (https://nodejs.org/en/docs/es6/), but i got an error
import co from 'co';
^^^^^^
SyntaxError: Unexpected token import
Then i tried to use babel
npm install -g babel-core
npm install -g babel-cli
npm install babel-core //install to babel locally, is it necessary?
and run by
babel-node js.js
still got same error, unexpected token import?
How could I get rid of it?
Current method is to use:
npm install --save-dev babel-cli babel-preset-env
And then in in
.babelrc
this install Babel support for latest version of js (es2015 and beyond) Check out babeljs
Do not forget to add
babel-node
to your scripts insidepackage.json
use when running your js file as follows.Now you can
npm populate yourfile.js
inside terminal.If you are running windows and running error internal or external command not recognized, use node infront of the script as follow
node node_modules/babel-cli/bin/babel-node.js
Then
npm run populate
babel-preset-es2015
is now deprecated and you'll get a warning if you try to use Laurence's solution.To get this working with Babel 6.24.1+, use
babel-preset-env
instead:Then add
env
to your presets in your.babelrc
:See the Babel docs for more info.
@jovi all you need to do is add .babelrc file like this:
and install these plugins as devdependences with npm.
then try babel-node ***.js again. hope this can help you.
Until modules are implemented you can use the Babel "transpiler" to run your code:
and then
If you dont want to type
--presets node6
you can save it .babelrc file by:See https://www.npmjs.com/package/babel-preset-node6 and https://babeljs.io/docs/usage/cli/
Involve following steps to resolve the issue:
1) Install the CLI and env preset
2) Create a .babelrc file
3) configure npm start in package.json
4) then start app
You have to use babel-preset-env and nodemon for hot-reload.
Then create .babelrc file with below content:
Finally, create script in package.json:
Or just use this boilerplate:
Boilerplate: node-es6