question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Unexpected token import" when running babel-watch

See original GitHub issue

Hey @kmagiera

Thanks for creating this package. It seems very useful! Unfortunately I wasn’t able to make it work so far. I have used the following command:

babel-watch --presets es2015 --watch src webpack/production.js

as well as other variations like these:

babel-watch --presets "es2015" --watch src webpack/production.js
babel-watch --presets=es2015 --watch src webpack/production.js
babel-watch --presets="es2015" --watch src webpack/production.js

I am getting the following error message:

C:\repos\my_repo\webpack\production.js:1
(function (exports, require, module, __filename, __dirname) { import webpack from 'webpack';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:404:25)
    at Module._extensions..js (module.js:432:10)
    at loader (C:\repos\my_repo\node_modules\babel-watch\runner.js:14:5)
    at Object.require.extensions.(anonymous function) [as .js] (C:\repos\my_repo\node_modules\babel-watch\runner.js:22:7)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:457:10)
    at process.<anonymous> (C:\repos\my_repo\node_modules\babel-watch\runner.js:38:21)
    at emitTwo (events.js:87:13)

The file in question starts like this:

import webpack from 'webpack';

I also have a .babelrc-file but it doesn’t seem to pick it up.

Any idea how I can get it to work? I’m currently using nodemon with the following command (and yes, the performance is pretty bad):

nodemon --exec babel-node --debug webpack/production.js --watch webpack/production.js

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
harm-meijercommented, Sep 18, 2020

What am I doing wrong here?

Have "babel-watch": "^7.0.0" in devDependencies and "dev": "babel-watch --presets es2015 ./index.js", in scripts of package.json, have an index.js with import express from "express"; but running yarn dev gives me:

import express from "express";
       ^^^^^^^

SyntaxError: Unexpected identifier
0reactions
Adam-Edrycommented, Nov 11, 2020

I ran into the same issue as @harm-meijer and it turned out to be caused by having a space in my path. Changing the directory let me run things as usual

Read more comments on GitHub >

github_iconTop Results From Across the Web

"unexpected token import" in Nodejs5 and babel?
In my package.json under scripts, I forgot to replace "node run" with "babel-node run.js". Yeah, I feel sheepish. :).
Read more >
[Solved]-Unexpected token import(ReactJS & Babel)-babel.js
Coding example for the question Unexpected token import(ReactJS & Babel)-babel.js. ... if you have not already added babel to dev dependencies, run.
Read more >
How To Use Import Statements in Nodejs - Better Programming
One of the most common is using import statements in Nodejs files. This leads to a syntax error called Unexpected identifier . Even...
Read more >
Mongodb – How to fix Error cannot find module 'core-js/es6 ...
... mongoose and babel-watch, but every time I run babel-watch I get this error: ... For core.js v3.9 import in App.tsx the next...
Read more >
babel-watch - Bountysource
If specify --debug-brk then app stop running: node_modules/.bin/babel-watch src/server.js -I --debug-brk. If only --inspect option - all fine:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found