111

I have this code:

"use strict";

import browserSync from "browser-sync";
import httpProxy from "http-proxy";

let proxy = httpProxy.createProxyServer({});

and I have installed babel-core and babel-cli globally via npm. The point is when I try to compile with this on my terminal:

babel proxy.js --out-file proxified.js

The output file gets copied instead of compiled (I mean, it's the same as the source file).

What am I missing here?

2
  • does the let change to var but the import statements remain?
    – runspired
    Commented Oct 30, 2015 at 16:22
  • For newer versions of react , use the new babel modules : stackoverflow.com/a/53927457/6665568 . It has better error messages and supports new features of react. Commented Dec 26, 2018 at 4:37

10 Answers 10

170

Babel is a transformation framework. Pre-6.x, it enabled certain transformations by default, but with the increased usage of Node versions which natively support many ES6 features, it has become much more important that things be configurable. By default, Babel 6.x does not perform any transformations. You need to tell it what transformations to run:

npm install babel-preset-env

and run

babel --presets env proxy.js --out-file proxified.js

or create a .babelrc file containing

{
    "presets": [
        "env"
    ]
}

and run it just like you were before.

env in this case is a preset which basically says to compile all standard ES* behavior to ES5. If you are using Node versions that support some ES6, you may want to consider doing

{
    "presets": [
        ["env", { "targets": { "node": "true" } }],
    ]
}

to tell the preset to only process things that are not supported by your Node version. You can also include browser versions in your targets if you need browser support.

12
  • 15
    this is useful. why didnt they put this in the documentation? is it safe to install babel-preset globally?
    – kidcapital
    Commented Nov 12, 2015 at 17:00
  • @kidcapital The docs do include this, but only as a side note it seems. I spent a lot of time trying to figure out how to get babel 6.0 configured properly back when it was first released.
    – nick
    Commented Nov 15, 2015 at 21:13
  • The gist of this post has been added as an info box at the Setup page per PR#72. A very minor improvement, but you gotta start somewhere! Thanks Logan.
    – nick
    Commented Nov 16, 2015 at 1:46
  • 7
    So much for convention over configuration ... so out of the box Babel does nothing - just copies files?
    – alex.p
    Commented May 11, 2016 at 21:52
  • Please ask a new question with all of the info about your setup so someone can answer it. Commented Jan 3, 2017 at 2:07
5

Most of these answers are obsolete. @babel/preset-env and "@babel/preset-react are what you need (as of July 2019).

3

I had the same problem with a different cause:

The code I was trying to load was not under the package directory, and Babel does not default to transpiling outside the package directory.

I solved it by moving the imported code, but perhaps I could have also used some inclusion statement in the Babel configuration.

3
  • Could you elaborate on that? my frontend code is not "under the package directory" either and it works fine. My server code contains "import" but babel-cli is not resolving those ...
    – Felipe
    Commented Oct 26, 2016 at 13:37
  • Hmm, not sure what to elaborate on? Babel was not transpiling. Try looking at all your sources of Babel config…
    – w00t
    Commented Oct 26, 2016 at 13:50
  • the problem I'm trying to solve is that my server files are orgnanized in several folders: I have servers.js, then /api/...stuff... When I run from memory, each "import" or "require" in those files are being resolved dynamically. When I run babel from the command line, I only outputs a single file, but doesn't resolve the relative imports, so I cannot use that to run my server ...
    – Felipe
    Commented Oct 26, 2016 at 14:57
3

First ensure you have the following node modules:

npm i -D webpack babel-core babel-preset-es2015 babel-preset-stage-2 babel-loader

Next, add this to your Webpack config file (webpack.config.js) :

// webpack.config.js
...
module  :  {
  loaders  :  [
    {
      test    :  /\.js$/,
      loader  :  'babel',
      exclude :  /node_modules/,
      options :  {
        presets  :  [ 'es2015', 'stage-2' ] // stage-2 if required
      } 
    } 
  ]
}
...

References:

Good Luck!

3

As of 2020, Jan:

STEP 1: Install the Babel presets:

yarn add -D @babel/preset-env @babel/preset-react

STEP 2: Create a file: babelrc.js and add the presets:

module.exports = {
  // ...
  presets: ["@babel/preset-env", "@babel/preset-react"],
  // ...
}

STEP 3:- Install the babel-loader:

yarn add -D babel-loader

STEP 4:- Add the loader config in your webpack.config.js:

{
//...
  module: [
    rules: [
      test: /\.(js|mjs|jsx|ts|tsx)$/,
      loader: require.resolve('babel-loader')
    ]
  ]
//...
}

Good Luck...

2
  • Hi Akash, I have legacy react app in which we used types in .js file extention when I try to make build using webpack 4 it gives me error unexpected tokens and etc previously we are using test: /\.jsx?$/, loader: 'babel', exclude: path.resolve(__dirname, "node_modules"), query: { plugins: ['transform-runtime'], presets: ['es2015', 'stage-0', 'react'], } Please help me to fix that I tried above things but no luck Commented Jul 15, 2021 at 20:00
  • Hi @MuhammadAsad, can you try stage-2 instead of stage-0?
    – Aakash
    Commented Jul 17, 2021 at 4:51
1
npm install --save-dev babel-preset-node5

npm install --save-dev babel-preset-react

...and then creating a .babelrc with the presets:

{
  "presets": [
    "node5",
    "react"
  ]
}

...resolved a very similar issue for me, with babel 3.8.6, and node v5.10.1

https://www.npmjs.com/package/babel-preset-node5
https://www.npmjs.com/package/babel-preset-react

1

Same error, different cause:

Transpiling had worked before and then suddenly stopped working, with files simply being copied as is.

Turns out I opened the .babelrc at some point and Windows decided to append .txt to the filename. Now that .babelrc.txt wasn't recognized by babel. Removing the .txt suffix fixed that.

0

fix your .babelrc

{
  "presets": [
    "react",
    "ES2015"
  ]
}
0

In year 2018:

Install following packages if you haven't yet:

npm install babel-loader babel-preset-react

webpack.config.js

{
    test: /\.jsx?$/,
    exclude: /node_modules/,
    use: [
      {
        loader: 'babel-loader',
        options: {
          presets: ['es2015','react']  // <--- !`react` must be part of presets!
        }
      }
    ],
  }
0

Ultimate solution

I wasted 3 days on this

import react from 'react' unexpected identifier

I tried modifying webpack.config.js and package.json files, and adding .babelrc, installing & updating packages via npm, I've visited many, many pages but nothing has worked.


What worked? Two words: npm start. That's right.

run the

npm start 

command in the terminal to launch a local server

...

(mind that it might not work straight away but perhaps only after you do some work on npm because before trying this out I had deleted all the changes in those files and it worked, so after you're really done, treat it as your last resort)


I found that info on this neat page. It's in Polish but feel free to use Google translate on it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.