8

I have mail configuration code in all.js.

Now i am trying to import this in my mail.js service, so i imported the config module as follows :-

mail.js

config = require('config'),

all.js

mailer: {
        auth: {
            user: "XXXXXXX",
            pass: "abc@123"
        }
    }

enter image description here

Gives me error cannot find module, but the module exists i have checked it.

How to solve this?

Anup
  • 9,396
  • 16
  • 74
  • 138

3 Answers3

13

I used the following code & it worked :-

config = require('../config/config');
Anup
  • 9,396
  • 16
  • 74
  • 138
  • 4
    Yeah, you have to use relative path (starting with '../' or './'), otherwise node.js will look into node_modules folder. – alex Feb 06 '14 at 19:08
  • 1
    Make sure to know how to differentiate between **../** and **./**. `. = This location .. = Up a directory` [reference](https://stackoverflow.com/questions/7591240/what-does-dot-slash-refer-to-in-terms-of-an-html-file-path-location) – Michał Dobi Dobrzański Sep 09 '18 at 13:41
4

This also occurs when you don't have the config package installed. You may just need to install the config package

npm i config
ebentil
  • 386
  • 4
  • 10
0

so you have to modify the path relative to the file you are using process.env not relative to the specified file config.json

eg:

node_modules
.gitignore
.env
config.json
folder:
     file.js

using config.json in file.js process.env.SECRET_KEY

.env: SECRET_KEY=../config.json //because you go out of folder in file.js to get into config.json
codmitu
  • 636
  • 7
  • 9