0

I'm using Windows. I created a folder and ran npm init, and npm install. All of my dependencies work great, except my own module that is located inside the folder. (Let's call it MyModule).

My files look like this:

c:\folder>dir
2019-04-10  12:45    <DIR>          .
2019-04-10  12:45    <DIR>          ..
2019-04-10  12:26    <DIR>          MyModule
2019-04-10  12:40             2,941 main.js

In main.js I have the line:

const component = require("myModule/component");

And I get the error:

C:\workspace\Monitoring>node main.js
internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module 'myModule/component'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (C:\folder\main.js:2:22)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

What am I doing wrong?

Things I've tried:

  1. Running npm list only shows the dependencies from npm.

  2. I searched for other "Cannot find module" issues but they were all for npm packages.

  3. Cleaning up and running npm install again doesn't help.

1 Answer 1

1

I found that this solves my problem:

const component = require("./myModule/component");

Just added "./" in front

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.