使用es6和babel-node从根目录导入节点模块 [英] Importing node modules from root directory using es6 and babel-node

查看:174
本文介绍了使用es6和babel-node从根目录导入节点模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有2个文件 index.js & my-module.js 在我的根目录

   -  index.js 
- my-module.js

my-module.js

  export let myFunc =()=> {
console.log('myFunc被称为!!!');
}

index.js

  import {myFunc} from'./my-module'; 
myFunc();

如果我从命令行运行以下行,一切都按预期运行。

  $ babel-node index.js>> myFunc被称为!!! 

但是如果在导入my-module时删除点:


$来自'/ my-module'的b $ b

  import {myFunc}; 
myFunc();

我收到错误:

 错误:找不到模块'/ my-module'

任何我不能使用绝对路径导入模块的原因?无论如何更改.babelrc配置以支持它?



谢谢

解决方案

(几乎)任何工具'/ x'表示文件系统根目录中的'x'。 Babel实际上并没有看到路径,只是从'/ my-module'编译

  import {myFunc}; 

into

  var _myModule = require('/ my-module'); 

节点实际上查找模块。



< hr>

如果你真的想导入相对于项目的根,你可以使用一个插件。我建议使用不太模糊的东西,并确保记录下一个阅读代码的人员。



这里有一个例子,我们使用领先的表示项目相对。你可以使用任何你喜欢的



示例输入:

  import {a} from'〜my-module'; 
import {b} from'/ my-module';
import {c} from'./my-module';

脚本/ babel-plugin-project-relative-require.js

  module.exports = function(babel){
//获取工作目录
var cwd = process.cwd();

return new babel.Transformer(babel-plugin-project-relative-require,{
ImportDeclaration:function(node,parent){
// might always true,但是如果(!babel.types.isLiteral(node.source)){
return node;
}

var ref = node.source,那么我们可以安全的
值;

//确保一个值,确保它不是home相对的,例如〜/ foo
if(!ref || ref [0]!=='〜'|| ref [ 1] ==='/'){
return node;
}

node.source.value = cwd +'/'+ node.source.value.slice( 1);

return node;
}
});
};

.babelrc

 code> {
plugins:[
./scripts/babel-plugin-project-relative-require.js
]
}

输出(如果在/ tmp中运行):

 'use strict'; 

var _tmpMyModule = require('/ tmp / my-module');

var _myModule = require('/ my-module');

var _myModule2 = require('./ my-module');


I'm writing a node app with es6 using babel transpiler.

I have 2 files index.js & my-module.js on my root directory

- index.js
- my-module.js

my-module.js

export let myFunc = () => {
  console.log('myFunc was called!!!');
}

index.js

import {myFunc} from './my-module';
myFunc();

if I run the following line from the command line everything works as expected.

$ babel-node index.js >> myFunc was called!!!

but if I remove the dot when importing my-module:

import {myFunc} from '/my-module';
myFunc();

I'm getting an error:

Error: Cannot find module '/my-module'

Any reason why I can't import modules using an absolute path? anyway to change .babelrc config to support it?

Thanks

解决方案

Like (almost) any tool '/x' means 'x' in the root of your filesystem. Babel doesn't actually look at the paths, it just compiles

import {myFunc} from '/my-module';

into

var _myModule = require('/my-module');

And node actually looks up the module.


If you really want to import relative to the root of the project, you could use a plugin. I recommend using something that isn't very ambiguous, and make sure you document this for the next person reading your code!

Here's an example where we use a leading ~ to mean project relative. You could use anything you like e.g. ^ would also be good.

Example Input:

import {a} from '~my-module';
import {b} from '/my-module';
import {c} from './my-module';

scripts/babel-plugin-project-relative-require.js

module.exports = function (babel) {
  // get the working directory
  var cwd = process.cwd();

  return new babel.Transformer("babel-plugin-project-relative-require", {
    ImportDeclaration: function(node, parent) {
      // probably always true, but let's be safe
      if (!babel.types.isLiteral(node.source)) {
        return node;
      }

      var ref = node.source.value;

      // ensure a value, make sure it's not home relative e.g. ~/foo
      if (!ref || ref[0] !== '~' || ref[1] === '/') {
        return node;
      }

      node.source.value = cwd + '/' + node.source.value.slice(1);

      return node;
    }
  });
};

.babelrc

{
    "plugins": [
        "./scripts/babel-plugin-project-relative-require.js"
    ]
}

Output (if run in /tmp):

'use strict';

var _tmpMyModule = require('/tmp/my-module');

var _myModule = require('/my-module');

var _myModule2 = require('./my-module');

这篇关于使用es6和babel-node从根目录导入节点模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆