Issue 1:
Bug Report

I would like to work on a fix!

Current behavior

A clear and concise description of the behavior.

REPL, Codesandbox, or GitHub Repo helps!

When I use @babel/preset-env with the useBuiltIns option, the core-js imports still output without the .js. This is usually fine, except when running in a node environment that supports built-in modules, e.g. with type: module in the package.json or outputting to a file with the .mjs extension.

This happens even if I use --out-file-extension .mjs.

--out-file-extension .cjs works, if you don't use modules: false, but then you're not outputting modules.

Input Code

test.js

const greeting = new Set(['Hello', 'world!']);
console.log(Array.from(greeting).join(' '));

npx babel test.js -d test/
node test/test.js

Output:

internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '<repo>/node_modules/core-js/modules/esnext.set.add-all' imported from <repo>/test/test.js
Did you mean to import core-js/modules/esnext.set.add-all.js?
at finalizeResolution (internal/modules/esm/resolve.js:275:11)
at packageResolve (internal/modules/esm/resolve.js:668:12)
at moduleResolve (internal/modules/esm/resolve.js:716:14)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:809:11)
at Loader.resolve (internal/modules/esm/loader.js:85:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:229:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:51:40)
at link (internal/modules/esm/module_job.js:50:36) {
code: 'ERR_MODULE_NOT_FOUND'
}

Expected behavior

A clear and concise description of what you expected to happen (or code).

No errors

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

Filename: babel.config.js

const presets = [['@babel/preset-env', {
targets: {node: true},
modules: false,
useBuiltIns: 'usage',
corejs: {version: 3, proposals: true},
}]];

export default {presets};

Environment

System:
OS: android
Binaries:
Node: 14.8.0 - /data/data/com.termux/files/usr/bin/node
npm: 6.14.7 - /data/data/com.termux/files/usr/bin/npm
npmPackages:
@babel/cli: ^7.10.5 => 7.10.5
@babel/core: ^7.11.4 => 7.11.4
@babel/preset-env: ^7.11.0 => 7.11.0

Possible Solution

One possible solution would be to always output with the extension. This would create longer output, but it would be future-proof and wouldn't require the user to do anything.

The best solution would probably be to guess whether the extension is needed, based on whether the package.json has type: module, and have a configuration option like addExtension or addCorejsExtension to force it one way or the other.

The last option I can think of is to always output .cjs, or add a configuration like changeExtension. .mjs would output to a .mjs file AND add the corejs path extensions. .cjs would output to .cjs and wouldn't add the extensions. .js, the default, would output to .js and possibly guess or use the addCorejsExtension option. I notice there is a --out-file-extension option for @babel/cli: having this keep the corejs extensions when you use .mjs would be fine, too.

Additional context

Add any other context about the problem here. Or a screenshot if applicable

Issue 2:
Bug Report

Current Behavior

$ node --experimental-modules dist/example.mjs
(node:5693) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/esm/default_resolve.js:79
let url = moduleWrapResolve(specifier, parentURL);
^

Error: Cannot find module /path/to/example/node_modules/core-js/modules/es.array.iterator imported from /path/to/example/dist/example.mjs
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:79:13)
at Loader.resolve (internal/modules/esm/loader.js:73:33)
at Loader.getModuleJob (internal/modules/esm/loader.js:152:40)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:43:40)
at link (internal/modules/esm/module_job.js:42:36) {
code: 'ERR_MODULE_NOT_FOUND'
}

Input Code

src/example.mjs (source code)

for (const x of [1, 2, 3]) {
console.log(x);
}

dist/example.mjs (generated code)

import "core-js/modules/es.array.iterator";

for (const x of [1, 2, 3]) {
console.log(x);
}

Expected behavior/code

node --experimental-modules dist/example.mjs
(node:5717) ExperimentalWarning: The ESM module loader is experimental.
1
2
3

Babel Configuration (.babelrc, package.json, cli command)

[
"@babel/preset-env",
{
"targets": {
"node": "8.5.0"
},
"useBuiltIns": "usage",
"corejs": 3,
"modules": false
}
]

Environment

Babel version(s): 7.6.4

Node/npm version: Node v12.11.1/npm 6.11.3

OS: Ubuntu 18.04

Monorepo: no

How you are using Babel: cli

Possible Solution

Could you add .js extension when importing polyfill?

import "core-js/modules/es.array.iterator.js"; // needs ".js"

for (const x of [1, 2, 3]) {
console.log(x);
}

Now, it will work properly!

$ node --experimental-modules dist/example.mjs
(node:5705) ExperimentalWarning: The ESM module loader is experimental.
1
2
3

Additional context/Screenshots

As of Node.js v12, extension requires on import syntax.