最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSNodeJS module exporting class from nested directory - Stack Overflow

programmeradmin3浏览0评论

I'm new to writing custom JS modules even if I use it occasionally for both work and personal project.

At the moment I managed to write a module (let's call it my-module) that export a default class (DefaultClass), i.e. in my dist/index.js I have

// dist/index.js
import DefaultClass from "./default-class";

export {DefaultClass};

To test it correctly I set up a local Verdaccio registry, publish my module to it and then import it in a second test project.
In this test project I can use DefaultClass as follow:

import { DefaultClass } from "my-module";

const instance = new DefaultCLass;

So far so good.

I then added some other classes that I nested in dist/subfolder/ with the related dist/subfolder/index.js that exports them:

// dist/subfolder/index.js
import SubClassA from "./subclass-a";
import SubClassB from "./subclass-b";
import SubClassC from "./subclass-c";

export {
    SubClassA,
    SubClassB,
    SubClassC,
};

What I'm trying o do is to be able to import those classes in the test project directly/explicitly (is this the right word?) as follows:

import { SubClassA } from "my-module/subclass";

Just to be even more clear, the desired behaviour is the same as the AWS CDK that lets import only the desired submodule: e.g. for the Amplify module the given example is import * as amplify from 'aws-cdk-lib/aws-amplify';

It may be not relevant, but for the sake of being as clear as possible, my module source code is in src/ and then I use esbuild with a config file to build both src/index.js and src/subfolder/index.js and output the boundles to dist/.

At the moment, I get the error

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/path/to//test-project/node_modules/my-module/subfolder' imported from /path/to/test-project/index.mjs
发布评论

评论列表(0)

  1. 暂无评论