root directory for node module

为君一笑 提交于 2019-12-03 23:25:41

I think the best solution is that you have as main an index.js and on it all the submodules so you can do did something like require('your-module').server

It's hard without seeing your folder structure, but you can:

  • use module.exports = to export the babel output
  • symlink the other files into the directory

package.json

{ "main": "lib" }

lib/index.js

module.exports = require('./path-to-transpiled-code.js');

// Now also export all the other stuff in this folder dynamically

fs.readdirSync(DIRECTORY_TO_EXPORT, function (err, files) {
  if (err) { throw err; }
  files.forEach(function (file) {
    if (file !== 'index.js') {
      fs.symlinkSync(path.join(__dirname, file), path.join(DIRECTORY_TO_EXPORT, file));
    }
  })
});

If I got your question right, the only thing you need is to point your's module main path inside package.json.

{
   "main": lib/index.js 
}

Where index.js is your module's main file. For more info check npm documentation here

Let's imagine you have a package my-module, and inside you have a subfolder my-submodule. Your my-module/package.json file will contain the following line:

{
    ...,
    "main": "lib/index.js",
    ...
}

That will allow your package to be required by the line

var myMod = require('my-module');

To allow using the same syntax for submodule, all you have to do is to put package.json file in your submodule folder. It should contain the following:

{
    "main": "lib/index.js"
}

After that, you can use simple require syntax:

var mySubMod = require("my-module/my-submodule");

You have several options how to accomplish your goal.

Export it all in lib/index.js

In your main module's entry point, you export everything that you would like to expose from your module.

  • + Full control over what is part of the public API and what is considered "private"
  • + Plays really nice with ES2015 modules specification
  • - Does not satisfy your requirement to require the component (server) through a direct require() call with path to the server module

Example:

'use strict'

// this is whatever is the primary export of your module - be it a
// function, object or anything else which can have other properties
// assigned to it
const main = require('./main')
const server = require('./server')

module.exports = main
module.exports.server = server
// ...

Usage:

'use strict'

const mymodule = require('my-module')
const server = require('my-module').server
const { server } = require('my-module')

import { server } from 'my-module'

Create entry files for the components in your root

You can create separate files for each of the component you would like to expose as part of you public API in your module's root folder.

  • + Satisfies your requirement of being able to require a component via specific path
  • - Slightly more complex structure, requires extra file for each public component
  • - Does not play well with ES2015 modules (you have to use the full path to the component and cannot use destructuring syntax)

Example folder structure:

my-module
├── lib
│   ├── index.js
│   └── server.js
└── server.js

The my-module/server.js file would only point to the actual implementation:

'use strict'

module.exports = require('./lib/server')

General notes

It should be noted that while technically possible, relying on full module paths by your consumers is not the best option, because the folder and file structure now becomes part of your API, so moving files around is suddenly a breaking change. Whenever I find myself in need to require a module's component this way, I treat it as if I was messing around with that module's internals and that I should expect my code to break even with a patch-version upgrade of that module.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!