Thursday, 9 February 2023

Node.js: Cannot find module but the specified path exists

I am currently developing my own npm package and I created a separate project to download this package from npm for an independent test. The package is being developed in typescript and I have a main file with several additional module files. In my main file, I am importing all of the classes from the other modules, then exporting all of them under the main file. I don't know if this is good practice but when I run the main file on the test project, it says it can't find the module when the path it specifies exists in the working directory.

Code Snippets:

Main file:

import { EventBus } from "./modules/eventbus/eventbus";
import { EventHandler } from "./modules/eventbus/eventhandler";
import { EventType } from "./modules/eventbus/eventtype";
import { Event } from "./modules/eventbus/event";
import { SemVer } from "./modules/semver";
export { SemVer, Event, EventBus, EventHandler, EventType };

Error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/workspaces/epic-engine-testing/node_modules/epic-engine/lib/modules/eventbus/eventbus' imported from /workspaces/epic-engine-testing/node_modules/epic-engine/lib/index.js

Working directory:

image

Testing file:

import { EventBus, EventHandler, EventType, Event } from "epic-engine";

class SomeType extends EventType {
    constructor() {
        super();
    }
}

const eventbus = new EventBus();
const handler = new EventHandler<SomeType>(eventbus, "type", () => {});
eventbus.createHandler(handler);
const event = new Event<SomeType>(eventbus, new SomeType(), "type");

I tried a bunch of things including changing the references to use .js, using absolute paths instead, and changing some settings in tsconfig.json.

Why is Node.js not finding the submodules or would it be better to export the modules in a different way?



from Node.js: Cannot find module but the specified path exists

No comments:

Post a Comment