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

javascript - Node can't find module but it exists in node_modules? - Stack Overflow

programmeradmin2浏览0评论

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 mand 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 pleted 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._pile (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
│   │   ├── ponent.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
│   │   │   ├── lodashpat.js
│   │   │   ├── lodashpat.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
│           ├── pression_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.

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 mand 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 pleted 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._pile (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
│   │   ├── ponent.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.pat.js
│   │   │   ├── lodash.pat.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
│           ├── pression_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.

Share Improve this question asked Jun 8, 2017 at 23:25 guribe94guribe94 1,5433 gold badges15 silver badges30 bronze badges 1
  • I think korbit-node package.json is broken - i.e. try adding a line "main": "./korbit.js", to the package.json in the korbit-node folder - see docs.npmjs./files/package.json#main – Jaromanda X Commented Jun 8, 2017 at 23:32
Add a ment  | 

2 Answers 2

Reset to default 2

The main file of your module must be index.js. (node_modules/korbit-node/index.js). Here it is nonexistent.

replace node_modules/korbit-node/korbit-node.js with node_modules/korbit-node/index.js

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?

发布评论

评论列表(0)

  1. 暂无评论