简体   繁体   中英

Error: Cannot find module 'config' at Function.Module._resolveFilename

Hii I have simple node server, with the following structure

myapp
 -config 
    -default-json
 -index.js
 -package-lock.json
 -package.json

Here is my part of my index.js

'use strict';

const
    config = require('config'),
    express = require('express'),
    request = require('request'),
    body_parser = require('body-parser'),
    app = express().use(body_parser.json()); // creates express http server

// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));

when I run node index.js I get the following error

internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module 'config'
    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:\xampp\htdocs\chat\index.js:13:14)
    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 is wrong with my code?

You have to explicitly add the config module in your package.json :

"dependencies": {

  "config": "version number"
}

https://www.npmjs.com/package/config

It means there is no config.js in your current location. Put the exact location of the config.js file..

Try,

config = require('./config/config'),

I found solution by by installing config from npm

https://www.npmjs.com/package/config

follow the instruction above and it should work , it might help some one in future

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM