Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Babel 7 "SyntaxError: Unexpected token import" in lerna environment #7208

Closed
Raigen opened this issue Jan 12, 2018 · 10 comments
Closed

Babel 7 "SyntaxError: Unexpected token import" in lerna environment #7208

Raigen opened this issue Jan 12, 2018 · 10 comments
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@Raigen
Copy link

Raigen commented Jan 12, 2018

We use lerna to seperate concernes into multiple packages. One of them is depending on two others to combine them in the final build. For production we build them independently and then put the dependencies into the node_modules directory of the first package.
In development we do nothing special because babel-node from the first package seems to take care of the import statements also from the other packages.
After I updated to babel 7 for testing purpose babel does not transform the other packages anymore.
I created two repositories with a minimal reproducer of the environment, one with babel6 where the import works and one with babel 7 dependencies where it does not work anymore.

Using @babel/register here https://github.com/Raigen/babel7-tmp/blob/master/packages/pck2/index.js#L4 does not work either.

I am out of ideas right now. I expect this to work because babel itself is a monorepo with lerna and import-dependencies inside ./packages. But I can not get it to work even in the simple reproducer repos I created.

Input Code

https://github.com/Raigen/babel6-tmp
https://github.com/Raigen/babel7-tmp

Expected Behavior

https://github.com/Raigen/babel6-tmp

Current Behavior

https://github.com/Raigen/babel7-tmp

Context

We plan to refactor one of our packages with typescript. For this purpose we need to figure out how the development environment can work with one package on typescript and the other packages still on babel. And the best idea until now is to keep babel everywhere and move to babel 7 with the typescript preset. This does work for all packages running independently and in the build, but not when I try to run the one package that imports from the others.
Also we of course want to keep our dependencies updated, so even without typescript we would want to upgrade to babel 7 when it is final.

Your Environment

software version(s)
Babel 7.0.0-beta.37
node 8.9.4
npm 5.6.0
Operating System tested on Windows 10 and Mac OS High Sierra
@babel-bot
Copy link
Collaborator

Hey @Raigen! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@Andarist
Copy link
Member

You need to tweak your --ignore arg, babel@7 by default ignores things in node_modules and your lerna packages are linked inside it.

@Raigen
Copy link
Author

Raigen commented Jan 12, 2018

First of all, thank you for the fast answer!

I tweaked around with --ignore (more like trial and error) and found out some things.
First of all, it seems that it does not compiles pck2 from the node_modules but from ../ as we can see, when we have a closer look into the error message

C:\Users\foellerich\Git\babel7-tmp\packages\pck2\server.js:1
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^
(function (exports, require, module, __filename, __dirname) { import Bluebird from 'bluebird'

Or maybe the reporter just follows linked files, that I do not know. But someone is following here.

I could not ignore it from the node_modules with something like node_modules/!(pck2).

What actually worked was babel-node --ignore \"node_modules/,../pck2/node_modules\" index.js so it ignores it's own node_modules/ and the node_modules/ from pck2, but not the code from pck2.

When I just ignore node_modules it will fail with

Error: Reentrant preset detected trying to load "C:\Users\foellerich\Git\babel7-tmp\packages\pck2\node_modules\@babel\preset-env\lib\index.js". This module is not ignored and is trying to load itself while compiling itself, leading to a dependency cycle. We recommend adding it to your "ignore" list in your babelrc, or to a .babelignore.

Obviously there is a node_modules not from the current directory loaded, which is ../pck2/node_modules/. This also is a strong indication that babel-node is not fetching the lerna packages from node_modules when it tries to compile the node_modules directory from another lerna package, all while --ignore node_modules is defined.

I could not really figure out where babel 7 looks for my packages, so I do not really know how to most efficently --ignore. Also I would like to know the default --ignore pattern, I could not find it in the repo, but maybe my queries on friday afternoon are not precise enough.

In the end technically the question is answered, thanks to the help I figured out a pattern that works for me

babel-node  --ignore \"node_modules/,../pck2/node_modules\" index.js

I might even be able to use --only "src,../package2/src,../package3/src instead in our project since that is the structure we use there.

But I do not really understand the way this workes, what bothers me. I do not like to use parameters that I can not understand. This always leads to errors and then you have to ask again.
Some better documentation about --ignore and --only would be really helpful. And the way babel figures out which files to include.

Also I can not get this working with ignore array in .babelrc. It is .. ignored by babel-node. I have to use --ignore. I am back at my Unexpected token import.
Is it not supported in babel 7 anymore?

@renatonmendes
Copy link

I´m having the same problem. My Lerna packages are the same except that all my code resides inside a src directory inside each package. I´m trying with no success:

 "start": "nodemon --exec \"cd ./src && babel-node --ignore \"node_modules/,../packageName/node_modules\" index.js"

$ nodemon --exec "cd ./src && babel-node --ignore "node_modules/,../graphql/node_modules" server.js
[nodemon] 1.17.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `cd ./src && babel-node --ignore node_modules/,../graphql/node_modules server.js`
D:\dev\test\node_modules\@babel\core\lib\config\files\plugins.js:137
    throw new Error("Reentrant " + type + " detected trying to load \"" + name + "\". This module is not ignored and is trying to load itself while compiling itself, leading to a dependency cycle. We recommend adding it to your \"ignore\" list in your babelrc, or to a .babelignore.");
    ^

Error: Reentrant preset detected trying to load "D:\dev\test\node_modules\babel-preset-env\lib\index.js". This module is not ignored and is trying to load itself while compiling itself, leading to a dependency cycle. We recommend adding it to your "ignore" list in your babelrc, or to a .babelignore.
    at requireModule (D:\dev\test\node_modules\@babel\core\lib\config\files\plugins.js:137:11)
    at loadPreset (D:\dev\test\node_modules\@babel\core\lib\config\files\plugins.js:56:15)
    at createDescriptor (D:\dev\test\node_modules\@babel\core\lib\config\config-descriptors.js:152:21)
    at D:\dev\test\node_modules\@babel\core\lib\config\config-descriptors.js:104:12
    at Array.map (native)
    at createDescriptors (D:\dev\test\node_modules\@babel\core\lib\config\config-descriptors.js:103:27)
    at createPresetDescriptors (D:\dev\test\node_modules\@babel\core\lib\config\config-descriptors.js:95:10)
    at D:\dev\test\node_modules\@babel\core\lib\config\config-descriptors.js:67:19
    at presets (D:\dev\test\node_modules\@babel\core\lib\config\config-descriptors.js:57:25)
    at mergeChainOpts (D:\dev\test\node_modules\@babel\core\lib\config\config-chain.js:298:68)
[

In my case Lerna is using yarn:

{
  "lerna": "2.9.0",
  "npmClient": "yarn",
  "useWorkspaces": true,
  "packages": [
    "packages/*"
  ],
  "version": "3.0.0"
}

I´m using babel@7.0.0-beta.42

Any ideas on how to solve it ?

@Raigen
Copy link
Author

Raigen commented Mar 21, 2018

I solved it using --only like in my comment above. Put every src directory in it and files that are not part of src but should also be transformed.

@renatonmendes
Copy link

@Raigen, --only works fine and its clearer. You explicitely define what will be transpiled. Thanks.

@Nantris
Copy link

Nantris commented Apr 28, 2018

I'm having this issue as well. No issue if I put the config into my webpack.config and run things that way, but using @babel/register with a .babelrc fails completely. It definitely reads the file, but it ignores it.

I have a workaround for webpack, but I can't find a workaround for running Electron without @babel/register. babel-node gave me the same issues, though I'm not 100% clear on usage of that, so it could be my fault.

Register works fine on Windows 7 x64, but not at all on Linux Mint 17.3 x64. All versions are at beta.46

Any ideas on a fix or workaround would be greatly appreciated.

@loganfsmyth
Copy link
Member

loganfsmyth commented Apr 28, 2018

@slapbox Please check out jestjs/jest#6053 (comment). That is likely your issue.

This is a new change and isn't really documented yet.

@Nantris
Copy link

Nantris commented Apr 28, 2018

Holy crap that was fast. Thanks very much for your help. I was just coming back to report I was mistaken and register doesn't work on Windows 7 on beta.46, which you already know I'm sure. Thanks again!

@hzoo
Copy link
Member

hzoo commented May 22, 2018

Closing as resolved

@hzoo hzoo closed this as completed May 22, 2018
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 21, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Aug 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

7 participants