且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

npm package.json主要和项目结构

更新时间:2021-11-26 09:55:01

很难确定不知道主index.jsmoduleA的内容,但这通常是通过不导入的方式完成的任何特定文件,而是包含package.json的目录-如:

It's hard to tell for sure not knowing the contents of your main index.js and moduleA but it's usually done in a way that you don't import any specific file, but rather the directory containing the package.json - like:

import {moduleA} from "myNpmModule";

现在,在package.json中称为主"的index.js应该导入其余模块,并将其导出为自己的module.exports属性.

Now, the index.js referenced as "main" in package.json should import the rest of the modules, and export them as its own module.exports properties.

例如,在dist/index.js中:

import {moduleA} from "./moduleA";
module.exports.moduleA = moduleA;

以及您的主代码中:

import {moduleA} from "myNpmModule";

类似的东西-可能存在差异,以适合您自己模块的结构.

Something like that - with possible differences to suits your own module's structure.

实际上,我编写了一个模块,该模块会自动执行类似的操作,将模块导入子目录并将其导出为属性.我没有将它放在npm上,因为它是供我自己使用的,当我将其发布到npm时,我将更新此答案.

Actually I wrote a module that automatically does something like that, importing modules in subdirectories and exporting them as properties. I haven't put it on npm because it was for my own use, when I publish it to npm I'll update this answer.

这是我上面所描述的一个有效示例-将import更改为require()以避免需要进行插秧步骤.

Here is a working example of what I described above - with import changed to require() to avoid the need for a transpilation step.

遵循我从这个答案中得到的建议的模块:

A module following my advice from this answer:

项目结构:

dist
  -- index.js
  -- moduleA
    -- index.js
package.json
moduleA.js

dist/index.js内容:

var {moduleA} = require('./moduleA');
module.exports.moduleA = moduleA;

dist/moduleA/index.js内容:

module.exports.moduleA = {
  info: 'This is what dist/moduleA/index.js exports as moduleA'
};

package.json内容:

{
  "name": "nested-project-structure-example",
  "version": "0.0.1",
  "description": "An example for a Stack Overflow answer",
  "main": "dist/index.js",
  "scripts": {
    "test": "node test.js"
  },
  // ...
}

moduleA.js内容:

module.exports = require('./dist/moduleA');

用法

使用此模块的项目:

Usage

A project that uses this module:

可以这样导入:

var {moduleA} = require('nested-project-structure-example');
console.error(moduleA.info);

这将通过package.json中引用的dist/index.js文件导入dist/ModuleA/index.js.请参阅 test1.js 例子.

This imports the dist/ModuleA/index.js via the dist/index.js file referenced in package.json. See test1.js for a working example.

var {moduleA} = require('nested-project-structure-example/dist/moduleA');
console.error(moduleA.info);

这直接导入dist/ModuleA/index.js,直接知道包括dist在内的内部路径.参见 test2.js 例子.

This imports the dist/ModuleA/index.js directly knowing the internal path including dist. See test2.js for a working example.

var {moduleA} = require('nested-project-structure-example/moduleA');
console.error(moduleA.info);

这将通过主项目目录中的moduleA.js文件导入dist/ModuleA/index.js.这样就不需要了解内部项目组织-不需要dist路径.请参阅 test3.js 例子.

This imports the dist/ModuleA/index.js via the moduleA.js file in the main project directory. That way doesn't need to know the internal project organization - dist path is not needed. See test3.js for a working example.

项目中moduleA.js的全部内容为:

module.exports = require('./dist/moduleA');

在项目的根目录中没有这样的文件,您将无法导入moduleA,而不必在路径中包括dist或直接通过包含在其中的项目的主js文件导入它package.json(在这种情况下为dist/index.js).

Without having such a file in your project's root directory you will not be able to import the moduleA without either including the dist in your path or importing it directly via the main js file of your project included in package.json (dist/index.js in this case).

这是实现问题目标的3种方法,其中两种不将dist包含在导入模块的代码中.我希望它能回答您的问题.

Those are 3 ways to achieve the goal of your question, two of which don't include the dist in the code that imports the module. I hope it answers your question.

只有这些选项,而无需将模块拆分为一组完全独立的模块,每个模块都是独立分布的.

Those are the only options that you have without splitting your module into a set of completely separate modules, each distributed separately.