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

Error: Cannot find module '@babel/preset-env' with babel-register on globally installed module #8777

Open
pvdlg opened this issue Sep 27, 2018 · 2 comments

Comments

@pvdlg
Copy link

pvdlg commented Sep 27, 2018

Bug Report

Current Behavior
When using @babel/register, using programatic configuration, with @babel/preset-env and using the only option, a execution the following error Error: Cannot find module '@babel/preset-env' is thrown.

Input Code
See repo https://github.com/semantic-release/travis-deploy-once/tree/babel-issue-%238777:

To reproduce:

## Clone the repo and checkout branch with reproduction code
$ git clone https://github.com/semantic-release/travis-deploy-once
$ git checkout babel-issue-#8777
## Install the package globally from the file system
$ yarn global add file:$PWD
## Change working directory to anywhere other than the repo
$ cd ~
## Run the globally installed binary
$ travis-deploy-once

The error Error: Cannot find module '@babel/preset-env' will be thrown because try to load the plugins/preset from the current working directory (~ in this example) rather than from where the code run.

If the only option is removed from the config, then the travis-deploy-once works as expected on Node 10 (it should return "Not running on Travis"). However it fails on Node 6 with a syntax exception as the sources are not compiled.

Expected behavior/code
@babel/register should load babel plugins and preset from where it runs and not from the current working directory.

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

require('@babel/register')({
  only: [/travis-deploy-once\/(lib|cli.js)/, /node_modules\/(got|cacheable-request)/],
  presets: [['@babel/preset-env', {targets: {node: 'current'}, useBuiltIns: 'entry'}]],
});
require('@babel/polyfill');

Environment

  • Babel version(s): 7.1.0
  • Node/npm version: Node 6
  • OS: OSX
  • Monorepo: no
  • How you are using Babel: babel-register
@babel-bot
Copy link
Collaborator

Hey @pvdlg! 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.

@pvdlg
Copy link
Author

pvdlg commented Sep 27, 2018

For information change the @babel/register config as follow resolve the problem:

require('@babel/register')({
  only: [/travis-deploy-once\/(lib|cli.js)/, /node_modules\/(got|cacheable-request)/],
-   presets: [['@babel/preset-env', {targets: {node: 'current'}, useBuiltIns: 'entry'}]],
+   presets: [[require.resolve('@babel/preset-env'), {targets: {node: 'current'}, useBuiltIns: 'entry'}]],
});
require('@babel/polyfill');

That really look like a workaround though....It basically resolve the path of the plugins/presets with require.resolve because it seems @babel/register doesn't do that properly.

I still think @babel/register should be able to resolve @babel/preset-env by itself, by resolving from the file in which it's defined, rather than the current working directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants