Import a module using a string reference
Install using pnpm:
pnpm add import-byOr yarn:
yarn add import-byOr npm:
npm i import-bySee the following example to understand how to use it:
- Create a file called is
detail.js.
// detail.js
module.exports.beverage = {
id: 12,
name: 'coffee'
};- And now, you can use
importBymethod to import the property from thedetail.jsfile.
const {importBy} = require('import-by');
const beverage = importBy('./detail#beverage');
const beverageName = importBy('./detail#beverage.name', __dirname);