Cannot find module './config.json' - javascript

I want to make a module linked to a config.json located outside the node_modules folder. The problem is that my index.js located in modules can't take into account the config.json located outside node_modules, do you have any idea what I should do?
I have tried
let configFile = require('./config.json')
and
let configFile = require('.../config.json')

It is important to first locate where the 'distance' of your index.js file to your config.json file.
Say, for example, if your file tree is like this:
cool-bot / modules / index.js
cool-bot / config.json
You could have your index.js file like this:
let configFile = require('../config.json')
../ will look up to the parent of the parent of your current folder.
./ will look up in the current folder.
Hope this was useful for you.

Okay first of all I have one question. Why? Why would you put your index.js in node_modules?
Anyways, you should check how far the bot config json file is from your index.js, for example:
v RootProjectFolder
⠀⠀config.json
⠀⠀v node_modules
⠀⠀⠀⠀v bot_folder
⠀⠀⠀⠀⠀⠀index.js
config.json's parent folder is RootProjectFolder
index.js is 3 folders away from RootProjectFolder;
RootProjectFolder -> node_modules -> bot_folder -> index.js
so you will need to go 3 folders back:
./ = 1 folder back (folder in which the file is in)
../ = 2 folders back (parent folder of the folder in which the file is in)
../../ = 3 folders back
So in this situation, you would do:
const configFile = require('../../config.json');
I can't give a specific answer based on your situation as I don't know your
project's structure.

Related

nodejs how to make a folder path discoverable globally from any sub-dir

Im creating a tool that lets users create directories. The directory names will be vary for each user. There will be a root 'config' folder in the parent created directory.
So it could look something like this:
MyFolder
config
otherFolder1
otherFolder2
subFolder1
I need that config folder path discoverable no matter what sub-directory im in but not discoverable in any parent dir above 'MyFolder'. I had the idea of saving the parent 'MyFolder' path upon initialization BUT then i run into an issue if they change the name of 'MyFolder'

config file on node_modules

I created a generic npm package which has my business logic, but I need some google cloud storage information that is in my config files. How can I access this file, if my package is in my node_modules folder? What would be a good solution for that?
this is the structure:
-config
-google_storage_config
-node_modules
-package
-serviceWhichNeedsThatConfig
Based on your folder structure, we will assume your path to the config will be ../../../config/google_storage_config, since node_modules/package/serviceWhichNeedsThatConfig should always be in the root directory.
Now, to access any variables from this config file, simply include the following code in the serviceWhichNeedsThatConfig,
var config = require('../../../config/google_storage_config');
console.log(config.myVariable);
Hi~Have you tried require?
var config = require('../../config/google_storage_config');

how to require a module without mentioning the relative file path

this is an example folder structure
folder1
--lib
----app.js
folder 2
--www
----Application.js
The Application.js file in the folder2 requires app.js file from the folder1 in the following way var app = require('../../folder1/lib/app')
and then Application.js is browserified.
What I want to achive is require the app.js into the Application.js without mentioning the path .ie var app = require('app') and without changing the folder structure, but on being browseried it will map the actual file i.e. folder1/lib/app.js for require('app').
EDIT
I am thinking about creating a file in folder1 that will be responsible to browserify the code in folder2.
Ex: $folder2> node ../folder1/build.js www/Application.js
will output a browserified file mapping require('app') to the app.js in folder1
I tend to use app-module-path for this.
My folder structure:
index.js
app/
lib/
hash.js
controllers/
index.js
middleware/
auth.js
and in index.js:
require('app-module-path').addPath(__dirname);
Then for example in controllers/index.js, I can just do this:
var auth = require('app/middleware/auth');
var hash = require('app/lib/hash');
This is for nodejs. Now for browserify you can use aliasify.
replacements: {
"app/(\\w+)": "app/$1"
}
It makes things much cleaner. 👍

How to write a file path that will point to a directory?

How to write a file path that will point to a directory.
From what I have googled,
/path means root,
./path means current directory,
../path means parent directory
I am playing with Framer.js + Yeoman.
If you look at the screenshot attached:
myLayers = Framer.Importer.load("../imported/test1") is not working?
When I put the "imported" folder under the "app" folder it is working.
Looks like you need to hope out one more parent directory, to the root test1.framer directory, before you can go into the imported directory. ie:
myLayers= Framer.Importer.load("../../imported/test1")
(starting from main.js file and only going up one parent directory only gets you in the app directory)

Why am I unable to specify a relative path in a .gitignore?

I have the following directory structure on a windows machine:
.gitignore
WebUserApp/
lib/
angular/
angular-ui-router/
typings/
.gitignore and WebUserApp are at the same level and under the WebUserApp is a lib direcotry. The lib directory contains three folders and I want to ignore the sending of the angular and angular-ui-router folders.
I tried the following .gitignore and it did not work:
# Ignore
WebUserApp/lib/angular
WebUserApp/lib/angular-ui-router
I tried this .gitignore and it worked:
# Ignore
/angular
/angular-ui-router
Can someone explain to me why the first version of .gitignore does not work
I think
someFolder/
ignores all folders named someFolder independent of the level in the tree, as long as it is below the current level.
You actually might want to try:
# Ignore
/WebUserApp/lib/angular/
/WebUserApp/lib/angular-ui-router/
Then you should be able to reference an relative path.
A slash at the end of the path such as
/WebUserApp/lib/angular/
git will just ignore folders named like that, but no files or symbolic links.
Try this
angular/
angular-ui-router/
It must be work fine.

Categories

Resources