Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including babel-plugin-package-name-import with all npm packages installed. Try it out:

var babelPluginPackageNameImport = require("babel-plugin-package-name-import")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

babel-plugin-package-name-import v1.0.3

Import files from the current package by name

babel-plugin-package-name-import

Build Status

Import files from the current package by name

Installing

Install it locally to your project by running:

yarn add --dev babel-plugin-package-name-import

Add the plugin to your Babel configuration:

{
  "plugins": ["package-name-import"]
}

Usage

Given that your package.json contains:

{
  "name": "project",
  "main": "src/index.js"
}

You can import files from the current project by name:

import project from 'project'

Note that require is not supported as its argument can be dynamically computed.

eslint-plugin-import

If your project uses eslint-plugin-import, ensure that it can resolve current project imports by including the eslint-import-resolver-package-name-import plugin.

Flow

If your project uses Flow, ensure that it can resolve current project imports by adding to .flowconfig:

[options]
module.name_mapper='^projectapos; -> '<PROJECT_ROOT>'
module.name_mapper='^project\/\(.*\)apos; -> '<PROJECT_ROOT>/\1'

Note that <PROJECT_ROOT> is a special token and should be left unmodified.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free