Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node can't find module but it exists in node_modules?

I've been playing with node for the past day and I've run into an issue I would really appreciate help on.

I created a new project and am trying to use a module I installed using npm, the module exists in the node_modules directory as expected and the command ran without error but node is unable to find it and throws an error (I have tried multiple packages with the same result). Below explains what I have done:

I created a new project using npm init and completed the guided package.json creation. I then created a javascript file containing this line of code.

const k = require('korbit-node');

and installed the module using npm install korbit-node then tried to run it locally using node index.js (what i called the js file) and was given this error.

$ node index.js

module.js:472
    throw err;
    ^

Error: Cannot find module 'korbit-node'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/gabe/Desktop/js_sandbox/index.js:1:73)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)

To me this seems to be a fairly straight-forward error message telling me that the package isn't installed. However my project structure looks like this:

├── index.js
├── node_modules
│   ├── debug
│   │   ├── CHANGELOG.md
│   │   ├── LICENSE
│   │   ├── Makefile
│   │   ├── README.md
│   │   ├── component.json
│   │   ├── karma.conf.js
│   │   ├── node.js
│   │   ├── package.json
│   │   └── src
│   │       ├── browser.js
│   │       ├── debug.js
│   │       ├── index.js
│   │       └── node.js
│   ├── iconv-lite
│   │   ├── Changelog.md
│   │   ├── LICENSE
│   │   ├── README.md
│   │   ├── encodings
│   │   │   ├── dbcs-codec.js
│   │   │   ├── dbcs-data.js
│   │   │   ├── index.js
│   │   │   ├── internal.js
│   │   │   ├── sbcs-codec.js
│   │   │   ├── sbcs-data-generated.js
│   │   │   ├── sbcs-data.js
│   │   │   ├── tables
│   │   │   │   ├── big5-added.json
│   │   │   │   ├── cp936.json
│   │   │   │   ├── cp949.json
│   │   │   │   ├── cp950.json
│   │   │   │   ├── eucjp.json
│   │   │   │   ├── gb18030-ranges.json
│   │   │   │   ├── gbk-added.json
│   │   │   │   └── shiftjis.json
│   │   │   ├── utf16.js
│   │   │   └── utf7.js
│   │   ├── lib
│   │   │   ├── bom-handling.js
│   │   │   ├── extend-node.js
│   │   │   ├── index.d.ts
│   │   │   ├── index.js
│   │   │   └── streams.js
│   │   └── package.json
│   ├── korbit-node
│   │   ├── README.md
│   │   ├── korbit.js
│   │   └── package.json
│   ├── lodash
│   │   ├── README.md
│   │   ├── dist
│   │   │   ├── lodash.compat.js
│   │   │   ├── lodash.compat.min.js
│   │   │   ├── lodash.js
│   │   │   ├── lodash.legacy.js
│   │   │   ├── lodash.legacy.min.js
│   │   │   ├── lodash.min.js
│   │   │   ├── lodash.mobile.js
│   │   │   ├── lodash.mobile.min.js
│   │   │   ├── lodash.underscore.js
│   │   │   └── lodash.underscore.min.js
│   │   ├── lodash.js
│   │   └── package.json
│   ├── ms
│   │   ├── index.js
│   │   ├── license.md
│   │   ├── package.json
│   │   └── readme.md
│   └── needle
│       ├── README.md
│       ├── bin
│       │   └── needle
│       ├── examples
│       │   ├── deflated-stream.js
│       │   ├── digest-auth.js
│       │   ├── download-to-file.js
│       │   ├── multipart-stream.js
│       │   ├── parsed-stream.js
│       │   ├── parsed-stream2.js
│       │   ├── stream-events.js
│       │   ├── stream-to-file.js
│       │   └── upload-image.js
│       ├── lib
│       │   ├── auth.js
│       │   ├── cookies.js
│       │   ├── decoder.js
│       │   ├── multipart.js
│       │   ├── needle.js
│       │   ├── parsers.js
│       │   └── querystring.js
│       ├── package.json
│       └── test
│           ├── basic_auth_spec.js
│           ├── compression_spec.js
│           ├── cookies_spec.js
│           ├── decoder_spec.js
│           ├── errors_spec.js
│           ├── helpers.js
│           ├── keys
│           │   ├── ssl.cert
│           │   └── ssl.key
│           ├── parsing_spec.js
│           ├── proxy_spec.js
│           ├── querystring_spec.js
│           ├── redirect_spec.js
│           ├── stream_spec.js
│           ├── url_spec.js
│           └── utils
│               ├── formidable.js
│               ├── proxy.js
│               └── test.js
└── package.json

and I can clearly see the module in the node_modules file so I am unsure why it isn't being found.

I've seen these questions Node.js NODE_PATH environment variable about making sure the NODE_PATH is set properly but I found that even after I explicitly pointed the path to the file it still did not work. I also of course found questions about the package not being installed but that is not the case in this situation.

Could someone please tell me why this isn't being found by node or point me to some resources about this issue/how node handles packages? I'm sure its a simple issue but everything I have found online/in the documentation has been unhelpful so far.

like image 626
guribe94 Avatar asked Jun 08 '17 23:06

guribe94


People also ask

Can not find module node path?

To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node . You can then import path with the following line of code import * as path from 'path' .

Can not find module next?

A module not found error can occur for many different reasons: The module you're trying to import is not installed in your dependencies. The module you're trying to import is in a different directory. The module you're trying to import has a different casing.

What does module not found mean?

The 'module not found' error is a syntax error that appears when the static import statement cannot find the file at the declared path. This common syntax error is caused by letter-casing inconsistencies that are present in your filename(s) between your repository and local machine, or both.


1 Answers

I've run into this once before. I made the mistake of naming the project folder the exact same as the node-module I am trying to install and require.

Is the folder your package.json file in called korbit-node?

like image 103
maxwell Avatar answered Oct 15 '22 09:10

maxwell