Node.js: 无法找到模块,但指定的路径存在

huangapple go评论182阅读模式
英文:

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

问题

Node.js 无法找到子模块的原因可能与你的模块导出方式有关,以及相对导入路径的设置。你可以尝试以下几种方法来解决这个问题:

  1. 确保你的导入路径是正确的: 首先,确保在测试项目中,你使用的导入路径与实际的文件路径匹配。在你的测试文件中,你使用了以下导入语句:

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

    确保 "epic-engine" 是你的 npm 包的名称,且你在测试项目的 package.json 中将其作为依赖项进行了正确的声明。

  2. 使用相对路径: 如果你希望使用相对路径来导入模块,确保你的相对路径是正确的。你的模块导入语句似乎是相对于当前文件的位置的,确保它们指向正确的文件。你可以使用 ../../ 这样的相对路径来遍历文件夹,直到到达正确的模块文件。

  3. 检查 TypeScript 配置: 确保你的 TypeScript 配置文件(tsconfig.json)中的模块解析选项设置正确。通常,你可以将 module 选项设置为 "CommonJS""ESNext",具体取决于你的项目需求。确保你的 TypeScript 配置与你的项目结构相匹配。

  4. 检查包的入口文件: 确保你在你的包的 package.json 文件中正确指定了入口文件。根据你提供的信息,入口文件似乎应该是 "lib/index.js",这是一个相对于包根目录的路径。

  5. 重新安装依赖项: 如果你对项目的结构或依赖项进行了更改,确保你在测试项目中运行 npm install 以确保依赖项正确安装。

  6. 检查文件和文件夹名称: 确保你的文件和文件夹名称的大小写与导入语句中的大小写一致。在某些操作系统中,文件名和文件夹名可能是区分大小写的。

  7. 清除 npm 缓存: 有时,npm 缓存可能会导致奇怪的问题。你可以尝试运行 npm cache clean 来清除 npm 缓存,然后重新运行测试项目。

通过检查上述事项,你应该能够解决 Node.js 找不到子模块的问题。如果问题仍然存在,请确保提供更多关于项目结构和配置的详细信息,以便我可以提供更具体的帮助。

英文:

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:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->

Error:

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

Working directory:

Node.js: 无法找到模块,但指定的路径存在

Testing file:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

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

const eventbus = new EventBus();
const handler = new EventHandler&lt;SomeType&gt;(eventbus, &quot;type&quot;, () =&gt; {});
eventbus.createHandler(handler);
const event = new Event&lt;SomeType&gt;(eventbus, new SomeType(), &quot;type&quot;);

<!-- end snippet -->

package.json:

{
  &quot;devDependencies&quot;: {
    &quot;@tsconfig/esm&quot;: &quot;^1.0.2&quot;,
    &quot;@types/jest&quot;: &quot;^29.2.3&quot;,
    &quot;jest&quot;: &quot;^29.3.1&quot;,
    &quot;ts-jest&quot;: &quot;^29.0.3&quot;,
    &quot;tslint&quot;: &quot;^6.1.3&quot;,
    &quot;typescript&quot;: &quot;^4.9.3&quot;
  },
  &quot;name&quot;: &quot;epic-engine&quot;,
  &quot;description&quot;: &quot;Pure TS engine developed by EpicPuppy613&quot;,
  &quot;version&quot;: &quot;0.1.0-dev.5&quot;,
  &quot;main&quot;: &quot;lib/index.js&quot;,
  &quot;types&quot;: &quot;lib/index.d.ts&quot;,
  &quot;type&quot;: &quot;module&quot;,
  &quot;scripts&quot;: {
    &quot;test&quot;: &quot;jest --config jestconfig.json&quot;,
    &quot;build&quot;: &quot;tsc&quot;,
    &quot;prepare&quot;: &quot;npm run build&quot;,
    &quot;lint&quot;: &quot;tslint -p tsconfig.json&quot;,
    &quot;prepublishOnly&quot;: &quot;npm test &amp;&amp; npm run lint&quot;,
    &quot;preversion&quot;: &quot;npm run lint&quot;,
    &quot;version&quot;: &quot;git add -A src&quot;,
    &quot;postversion&quot;: &quot;git push &amp;&amp; git push --tags&quot;
  },
  &quot;repository&quot;: {
    &quot;type&quot;: &quot;git&quot;,
    &quot;url&quot;: &quot;git+https://github.com/EpicPuppy613/epic-engine.git&quot;
  },
  &quot;author&quot;: &quot;EpicPuppy613&quot;,
  &quot;license&quot;: &quot;MIT&quot;,
  &quot;bugs&quot;: {
    &quot;url&quot;: &quot;https://github.com/EpicPuppy613/epic-engine/issues&quot;
  },
  &quot;homepage&quot;: &quot;https://github.com/EpicPuppy613/epic-engine#readme&quot;,
  &quot;files&quot;: [
    &quot;lib/**/*&quot;
  ]
}

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?

答案1

得分: 1

After compiling the ts files into js files, wherever you have import syntax, Node.js looks for .js files to resolve them. So it needs to be explicitly given a module name with .js extension in import.

You may need to read this doc on how the import mechanism works in Node.js.

To fix this issue, you have multiple choices(since the target you've defined is ES6):

  1. change moduleResolution to nodeNext and add .js extension whenever you would importing modules in typescript:
import { EventBus } from "./modules/eventbus/eventbus.js";
import { EventHandler } from "./modules/eventbus/eventhandler.js";
import { EventType } from "./modules/eventbus/eventtype.js";
...

You don't need to be worried about it, typescript is well smart. Based on this comment from one of typescript contributor:

.js file extensions are now allowed

  1. Using rollup package. The rollup package won't manipulate your files. Instead, it bundles your output files.
英文:

After compiling the ts files into js files, wherever you have import syntax, Node.js looks for .js files to resolve them. So it needs to be explicitly given a module name with .js extension in import.

You may need to read this doc on how the import mechanism works in Node.js.

To fix this issue, you have multiple choices(since the target you've defined is ES6):

  1. change moduleResolution to nodeNext and add .js extension whenever you would importing modules in typescript:
import { EventBus } from &quot;./modules/eventbus/eventbus.js&quot;;
import { EventHandler } from &quot;./modules/eventbus/eventhandler.js&quot;;
import { EventType } from &quot;./modules/eventbus/eventtype.js&quot;;
...

You don't need to be worried about it, typescript is well smart. Based on this comment from one of typescript contributor:

> .js file extensions are now allowed


  1. Using rollup package. The rollup package won't manipulate your files. Instead, it bundles your output files.

答案2

得分: 0

> 我创建了一个独立的项目,用于从 npm 下载此包以进行独立测试。

首先,您可以使用方便的 npm link 命令来避免上传您的包,只是为了进行测试而麻烦。根据文档npm link 命令:

> ...对于安装您自己的东西非常方便,这样您就可以在不断重建的情况下进行开发和迭代测试。

将您的包安装为依赖项

解决了这个问题之后,我认为错误消息中已经提供了提示。请注意,它说:

> Cannot find module /workspaces/epic-engine-testing/node_modules/...

这里似乎 Node 正在寻找 epic-engine-testing 项目中的文件,因此您必须在 测试 项目的 package.json 文件中引用您的包(即您要测试的包)。因此,请进入您的 epic-engine-testing 项目文件夹,在终端中输入 npm install epic-engine@0.1.0-dev.5。这应该安装您的包,以便可以找到它。如果这不能解决问题,您需要分享 epic-engine-testingpackage.json 文件,以帮助我们了解发生了什么情况。

使用 export { ... } from &#39;...&#39; 语法

您的主文件可以使用重新导出语法并简化为以下内容:

export { EventBus } from &quot;./modules/eventbus/eventbus&quot;;
export { EventHandler } from &quot;./modules/eventbus/eventhandler&quot;;
export { EventType } from &quot;./modules/eventbus/eventtype&quot;;
export { Event } from &quot;./modules/eventbus/event&quot;;
export { SemVer } from &quot;./modules/semver&quot;;

// 使用上述语法时,下面的行不是必要的。
// export { SemVer, Event, EventBus, EventHandler, EventType };
英文:

> I created a separate project to download this package from npm for an independent test.

First, you can use the handy npm link command to save yourself the trouble of uploading your package just so you can test. As per the docs the npm link command:

> ...is handy for installing your own stuff, so that you can work on it and test iteratively without having to continually rebuild.

Install your package as a dependency

With that out of the way, I think the hint is in the error message. Note it says:

> Cannot find module /workspaces/epic-engine-testing/node_modules/...

Here it seems Node is looking for the file in the epic-engine-testing project, so you must have the package.json file for the test project reference your package (i.e. the one you want to test). So go into your epic-engine-testing project folder and at the terminal type npm install epic-engine@0.1.0-dev.5. That should install your package so it can be found. If that doesn't resolve it, you'll need to share the package.json file for the epic-engine-testing to help us see what's going on.

Using the export { ... } from &#39;...&#39; syntax

Your main file can use the re-exports synax and be simplified to this when exporting:

export { EventBus } from &quot;./modules/eventbus/eventbus&quot;;
export { EventHandler } from &quot;./modules/eventbus/eventhandler&quot;;
export { EventType } from &quot;./modules/eventbus/eventtype&quot;;
export { Event } from &quot;./modules/eventbus/event&quot;;
export { SemVer } from &quot;./modules/semver&quot;;

// the line below is not necessary when using above syntax.
// export { SemVer, Event, EventBus, EventHandler, EventType };

huangapple
  • 本文由 发表于 2023年1月4日 02:05:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/74997071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定