Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

ecmascript 6 - Importing node modules from root directory using es6 and babel-node

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...