Using babel-register in my cli npm application works locally but not globally after posting

I developed a javascript CLI application that uses ES2015 code with babel as a compiler. (babel-require hook)

The application works fine locally, but when I publish on npm it stops working (babel doesn't seem to compile ES2015 files anymore)

Setup:

sample ./bootstrap.js(ES5):

require('babel-register');
require('./src/app.js');

Sample ./src/app.js(ES2015):

import package from 'package';
...

sample ./bin/myapp:

#!/usr/bin/env node
require('../bootstrap.js');

Local work execution:

appdir$ ./bin/myapp
I'm running fine!
appdir$

Run globally (after installing npm):

$ sudo npm install -g myapp
└── myapp@0.1.0
$ myapp
/usr/local/lib/node_modules/myapp/src/app.js:1
(function (exports, require, module, __filename, __dirname) { import package from 'package';
                                                              ^^^^^^

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 Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/myapp/node_modules/babel-register/lib/node.js:138:7)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/usr/local/lib/node_modules/myapp/server.js:9:12)
    at Module._compile (module.js:425:26)   

Versions:

    ├─┬ babel-register@6.4.3
    │ │ ├─┬ babel-core@6.4.5
    │ │ │ ├─┬ babel-code-frame@6.3.13
    │ │ │ ├─┬ babel-generator@6.4.5
    │ │ │ ├── babel-helpers@6.4.5
    │ │ ├── babel-runtime@5.8.35

What I tried:

  • Adding ignore: falseto my file .babelrc, hoping it will enable compilation
  • using options requiring hook arguments instead of .babelrcfile

Bad luck:/

+4
1

, , . , , -, .

TL; DR

babel-register hook ignore only .babelrc, .

./bootstrap.js:

require('babel-register')({
  ignore: false,
  only: /myapp\/src/
});
require('./src/app.js');
  • ignore , node_modules , .
  • only src.

, , , , , ( :))...

:

  • node-debug

    $ sudo npm install -g node-inspector
    
  • (--debug-brk), .

    $ node-debug --debug-brk myapp
    
  • URL-, node-debug, voila

  • , require('./scr/app.js');
  • "play",
  • " " " ": "" (. ), ' '. , " ".
  • , "scope variable", Object.require.extension babel-register/lib/node.js ignore only undefined, , , !
  • shouldIgnore , : node_modules , !ignore && !only true ( ), . , ES2015 .

    babel-register/lib/node.js:120:
    function shouldIgnore(filename) {
      if (!ignore && !only) {  // here `ignore` and `only` were null, despite .babelrc values
        return getRelativePath(filename).split(_path2["default"].sep).indexOf("node_modules") >= 0;
      } else {
        return _babelCore.util.shouldIgnore(filename, ignore || [], only);
      }
    }
    
  • , thoses babe-register.

+8

Source: https://habr.com/ru/post/1626790/


All Articles