<p dir="auto">By default babel is looking for <code class="notranslate">node_modules</code> in current directory.<br>
But sometimes we need to use <code class="notranslate">node_modules</code> from some child or other directory.<br>
I tried to use <code class="notranslate">NODE_PATH</code> env, but it doesn't work.<br>
So, maybe it's good idea to add supporting some environment variable, for example <code class="notranslate">BABEL_NODE_MODULES</code> with list of path to search <code class="notranslate">node_modules</code> directory.<br>
Let's make babel more flexible!</p>
<p dir="auto">This is a bug report - I am getting this error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
Module build failed: Error: Couldn't find preset &quot;latest&quot; relative to directory &quot;/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman-browser-polyfills/modules&quot;
    at /Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
    at OptionManager.mergePresets (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
    at OptionManager.mergeOptions (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
    at OptionManager.init (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at transpile (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-loader/lib/index.js:46:20)
    at Object.module.exports (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-loader/lib/index.js:163:20)
 @ ./lib/index.js 136:9-22
 @ multi babel-polyfill ./lib/index.js

ERROR in .-browser-polyfills/modules/assert.js
Module build failed: Error: Couldn't find preset &quot;latest&quot; relative to directory &quot;/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman-browser-polyfills/modules&quot;
    at /Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
    at OptionManager.mergePresets (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
    at OptionManager.mergeOptions (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
    at OptionManager.init (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at transpile (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-loader/lib/index.js:46:20)
    at Object.module.exports (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-loader/lib/index.js:163:20)
 @ ./lib/index.js 131:13-30
 @ multi babel-polyfill ./lib/index.js"><pre class="notranslate"><code class="notranslate">
Module build failed: Error: Couldn't find preset "latest" relative to directory "/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman-browser-polyfills/modules"
    at /Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
    at OptionManager.mergePresets (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
    at OptionManager.mergeOptions (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
    at OptionManager.init (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at transpile (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-loader/lib/index.js:46:20)
    at Object.module.exports (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-loader/lib/index.js:163:20)
 @ ./lib/index.js 136:9-22
 @ multi babel-polyfill ./lib/index.js

ERROR in .-browser-polyfills/modules/assert.js
Module build failed: Error: Couldn't find preset "latest" relative to directory "/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman-browser-polyfills/modules"
    at /Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
    at OptionManager.mergePresets (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
    at OptionManager.mergeOptions (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
    at OptionManager.init (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at transpile (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-loader/lib/index.js:46:20)
    at Object.module.exports (/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/node_modules/babel-loader/lib/index.js:163:20)
 @ ./lib/index.js 131:13-30
 @ multi babel-polyfill ./lib/index.js
</code></pre></div>
<p dir="auto">Webpack should be able to find the babel modules, because I am using NODE_PATH, which allows me to store Babel deps in a particular location, and I run webpack like so:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#!/usr/bin/env bash
NODE_PATH=${NODE_PATH}:~/.suman/global/node_modules webpack"><pre class="notranslate"><span class="pl-c"><span class="pl-c">#!</span>/usr/bin/env bash</span>
NODE_PATH=<span class="pl-smi">${NODE_PATH}</span>:<span class="pl-k">~</span>/.suman/global/node_modules webpack</pre></div>
<p dir="auto">I have this webpack config:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const path = require('path');

module.exports = {

  entry: ['babel-polyfill', './lib/index.js'],

  output: {
    path: path.resolve(__dirname + '/dist'),
    filename: 'suman.js'
  },

  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          presets: ['latest'],
          plugins: ['transform-runtime']
        }
      },
      {
        test: /(\/lib\/init\/|cli.js$)/,
        loader: 'ignore-loader'
      }
    ]
  },

  resolve: {
    alias: {
      fs: require.resolve('suman-browser-polyfills/modules/fs'),
      assert: require.resolve('suman-browser-polyfills/modules/assert')
    },
    extensions: ['.js']
  },

  node: {
    // assert: true,
    buffer: true,
    child_process: 'empty',
    cluster: 'empty',
    console: true,
    constants: true,
    crypto: 'empty',
    dgram: 'empty',
    dns: 'mock',
    domain: true,
    events: true,
    // fs: 'empty',
    http: true,
    https: true,
    module: 'empty',
    net: 'mock',
    os: true,
    path: true,
    process: true,
    punycode: true,
    querystring: true,
    readline: 'empty',
    repl: 'empty',
    stream: true,
    string_decoder: true,
    timers: true,
    tls: 'mock',
    tty: true,
    url: true,
    util: true,
    v8: 'mock',
    vm: true,
    zlib: 'empty',
  }
};"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-s1">path</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'path'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-smi">module</span><span class="pl-kos">.</span><span class="pl-c1">exports</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span>

  <span class="pl-c1">entry</span>: <span class="pl-kos">[</span><span class="pl-s">'babel-polyfill'</span><span class="pl-kos">,</span> <span class="pl-s">'./lib/index.js'</span><span class="pl-kos">]</span><span class="pl-kos">,</span>

  <span class="pl-c1">output</span>: <span class="pl-kos">{</span>
    <span class="pl-c1">path</span>: <span class="pl-s1">path</span><span class="pl-kos">.</span><span class="pl-en">resolve</span><span class="pl-kos">(</span><span class="pl-s1">__dirname</span> <span class="pl-c1">+</span> <span class="pl-s">'/dist'</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
    <span class="pl-c1">filename</span>: <span class="pl-s">'suman.js'</span>
  <span class="pl-kos">}</span><span class="pl-kos">,</span>

  <span class="pl-c1">module</span>: <span class="pl-kos">{</span>
    <span class="pl-c1">loaders</span>: <span class="pl-kos">[</span>
      <span class="pl-kos">{</span>
        <span class="pl-c1">test</span>: <span class="pl-pds"><span class="pl-c1">/</span><span class="pl-cce">\.</span>js<span class="pl-cce">$</span><span class="pl-c1">/</span></span><span class="pl-kos">,</span>
        <span class="pl-c1">loader</span>: <span class="pl-s">'babel-loader'</span><span class="pl-kos">,</span>
        <span class="pl-c1">exclude</span>: <span class="pl-pds"><span class="pl-c1">/</span>node_modules<span class="pl-c1">/</span></span><span class="pl-kos">,</span>
        <span class="pl-c1">options</span>: <span class="pl-kos">{</span>
          <span class="pl-c1">presets</span>: <span class="pl-kos">[</span><span class="pl-s">'latest'</span><span class="pl-kos">]</span><span class="pl-kos">,</span>
          <span class="pl-c1">plugins</span>: <span class="pl-kos">[</span><span class="pl-s">'transform-runtime'</span><span class="pl-kos">]</span>
        <span class="pl-kos">}</span>
      <span class="pl-kos">}</span><span class="pl-kos">,</span>
      <span class="pl-kos">{</span>
        <span class="pl-c1">test</span>: <span class="pl-pds"><span class="pl-c1">/</span><span class="pl-kos">(</span><span class="pl-cce">\/</span>lib<span class="pl-cce">\/</span>init<span class="pl-cce">\/</span><span class="pl-c1">|</span>cli.js<span class="pl-cce">$</span><span class="pl-kos">)</span><span class="pl-c1">/</span></span><span class="pl-kos">,</span>
        <span class="pl-c1">loader</span>: <span class="pl-s">'ignore-loader'</span>
      <span class="pl-kos">}</span>
    <span class="pl-kos">]</span>
  <span class="pl-kos">}</span><span class="pl-kos">,</span>

  <span class="pl-c1">resolve</span>: <span class="pl-kos">{</span>
    <span class="pl-c1">alias</span>: <span class="pl-kos">{</span>
      <span class="pl-c1">fs</span>: <span class="pl-en">require</span><span class="pl-kos">.</span><span class="pl-en">resolve</span><span class="pl-kos">(</span><span class="pl-s">'suman-browser-polyfills/modules/fs'</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
      <span class="pl-c1">assert</span>: <span class="pl-en">require</span><span class="pl-kos">.</span><span class="pl-en">resolve</span><span class="pl-kos">(</span><span class="pl-s">'suman-browser-polyfills/modules/assert'</span><span class="pl-kos">)</span>
    <span class="pl-kos">}</span><span class="pl-kos">,</span>
    <span class="pl-c1">extensions</span>: <span class="pl-kos">[</span><span class="pl-s">'.js'</span><span class="pl-kos">]</span>
  <span class="pl-kos">}</span><span class="pl-kos">,</span>

  <span class="pl-c1">node</span>: <span class="pl-kos">{</span>
    <span class="pl-c">// assert: true,</span>
    <span class="pl-c1">buffer</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">child_process</span>: <span class="pl-s">'empty'</span><span class="pl-kos">,</span>
    <span class="pl-c1">cluster</span>: <span class="pl-s">'empty'</span><span class="pl-kos">,</span>
    <span class="pl-c1">console</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">constants</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">crypto</span>: <span class="pl-s">'empty'</span><span class="pl-kos">,</span>
    <span class="pl-c1">dgram</span>: <span class="pl-s">'empty'</span><span class="pl-kos">,</span>
    <span class="pl-c1">dns</span>: <span class="pl-s">'mock'</span><span class="pl-kos">,</span>
    <span class="pl-c1">domain</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">events</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c">// fs: 'empty',</span>
    <span class="pl-c1">http</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">https</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">module</span>: <span class="pl-s">'empty'</span><span class="pl-kos">,</span>
    <span class="pl-c1">net</span>: <span class="pl-s">'mock'</span><span class="pl-kos">,</span>
    <span class="pl-c1">os</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">path</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">process</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">punycode</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">querystring</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">readline</span>: <span class="pl-s">'empty'</span><span class="pl-kos">,</span>
    <span class="pl-c1">repl</span>: <span class="pl-s">'empty'</span><span class="pl-kos">,</span>
    <span class="pl-c1">stream</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">string_decoder</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">timers</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">tls</span>: <span class="pl-s">'mock'</span><span class="pl-kos">,</span>
    <span class="pl-c1">tty</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">url</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">util</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">v8</span>: <span class="pl-s">'mock'</span><span class="pl-kos">,</span>
    <span class="pl-c1">vm</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">zlib</span>: <span class="pl-s">'empty'</span><span class="pl-kos">,</span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">as you can see, in <code class="notranslate">resolve.alias</code>, we are referencing another project. No Babel dependencies are installed in</p>
<p dir="auto">But that should not matter, the Babel deps needed are located in the directory referenced in NODE_PATH.</p>
<p dir="auto">What's probably happening is that Webpack/Babel is doing a relative path lookup check for the needed Babel deps, but it should also check on NODE_PATH, if it needs to do a "pre-check".</p>