babel: [Bug]: 'return' outside of function.

💻

  • Would you like to work on a fix?

How are you using Babel?

@rollup/plugin-babel

Input code

(there is no input code)

Configuration file name

No response

Configuration

No response

Current and expected behavior

Current behaviour: SyntaxError Expected behaviour: no SyntaxError

Environment

Babel ^7.22.6 Node v18.12.1

Possible solution

Finally ship proper MJS files.

Additional context

Install @babel/core and you will find this file:

node_modules/@babel/types/lib/index.js

"use strict";

if (typeof process === "object" && process.version === "v20.6.0") {
  if (exports["___internal__alreadyRunning" + ""]) return;
  Object.defineProperty(exports, "___internal__alreadyRunning", {
    value: true,
    enumerable: false,
    configurable: true
  });
}
Object.defineProperty(exports, "__esModule", {
  value: true
});
// and more...

Error is in line 4: if (exports["___internal__alreadyRunning" + ""]) return;

You are not allowed to output a return in module scope:

[!] (plugin commonjs--resolver) SyntaxError: /var/www/html/RuntimeTypeInspector.js/node_modules/@babel/types/lib/index.js: 'return' outside of function. (4:51)

image

I’m not sure why this project is shipping such old-fashioned and buggy files. Projects like Monaco editor ship browser-consumable MJS files - via importmap.

So this is a bug report, but at the same time a feature request.

Any thoughts why there are no browser-consumable MJS files yet in the released package?

About this issue

  • Original URL
  • State: closed
  • Created 10 months ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

Could you open a new issue and show the stack trace? That’s a different error.

Actually, it might be fixed by https://github.com/babel/babel/pull/15892 and we just need to release it.

If needed, we are working on a migration guide: https://next.babeljs.io/docs/v8-migration

Without inspecting too much, the error is coming from:

https://github.com/babel/babel/blob/e976248b4b1e78f27b9f3ec352861aa8a5978334/packages/babel-core/cjs-proxy.cjs#L48-L53

Seems like your build tool is picking the wrong entry in @babel/core package.json - the require field:

  "exports": {
    ".": {
      "require": "./cjs-proxy.cjs",
      "default": "./lib/index.js"
    },
    "./package.json": "./package.json"
  },

But hard to say why in your particular case because of missing information.

On my end, this works perfectly:

npm install @babel/core@8.0.0-alpha.2
node
> const { traverse } = await import("@babel/core");

I’m confused as to why it causes an infinite loading loop, but it’s due to line 2 of https://unpkg.com/browse/@babel/core@8.0.0-alpha.2/lib/config/helpers/config-api.js (we’ll fix it).

If you have a demo with the import map I would still be happy to take a look and figure out if it’s a problem on our end 😃

Yep, that’s what I’m wondering too, but I had no time to fully investigate so far.

Here is a live demo: https://killtube.org/~playcanvas/babel-8-via-importmap/test.html

However, I think it’s easiest to clone my little test repo to be able to quickly change the importmap for example:

https://github.com/kungfooman/babel-8-via-importmap

If you have a demo with the import map I would still be happy to take a look and figure out if it’s a problem on our end 😃

I don’t really know what could be causing that, maybe a demo hosted somewhere would help.

However, for your use case you may also consider loading @babel/standalone from a CDN, which is a browser-ready pre-bundled version of Babel. It’s not ESM (yet) but UMD, so you would have to load it with a script tag and use the Babel global.

That file is a CommonJS file, and CommonJS supports top-level return.

If you wish, you can use Babel 8.0.0-alpha.2 that is published as ESM (make sure that all your @babel/ dependencies are on the same alpha version)