Run Nodejs program that ignores parent node_modules - node.js

I have a library and I want to test it by doing a full install locally before publishing to npm. One of the things I want to test is if all the dependencies are part of the project. Because of how nodejs structures its module system though, (a nodejs module is searched for at every directory level) I do not have a good way to ensure that the module I am requiring came from the current folder's node_modules, and not the parent folder's node_modules.
Here is the project structure:
library-folder/
package.json
node_modules/
testing/integration/
package.json
node_modules/
test-install.js
I want to know if there is a way to tell nodejs to only require modules from the current directory, and ignore any parent directories.

I do not have a good way to ensure that the module I am requiring came from the current folder's node_modules, and not the parent folder's node_modules.
For that, you can use require.resolve and check the path it returns.

I resolved this issues by creating a file in the child's root project folder.
File's name should be called like this: .env
The content: SKIP_PREFLIGHT_CHECK=true

Related

NodeJS: installing dependencies from package.json located one level above the working folder

The project I need to work upon is structured in a way that the package.json file is one level abov the code folder. The code is located at "D:\MyNodeProj\Source\"
All the files and code is located under "Source" project including "node_modules" folder. Package.json file is located out of "Source" folder. It is located at "D:\MyNodeProj\package.json" When I try to run the project it gives an error: Cannot find module 'winston-logzio'
I know this has something to do with package.json's location as winston-logzio's entry is there in package.json
I tried npm install but it is also throwing error. It used to work before when package.json and node_modules folder were on same level. Now that package.json is one level up we are haviing all sorts of trouble.
What is the fix for this? Please note, we can not change the location of package.json now Don't know for what reason but the architect want it this way.
If you use prefix for the parent directory, you will be able to use npm install --prefix=./../ in the project folder (D:\MyNodeProj\Source\), this command will install node_modules in the parent directory(D:\MyNodeProj\). But in order to make the node_module visible to your project you can set NODE_PATH to the parent directory and then run the project.

Nodejs require module from external folder

I'm developing microservice architecture on nodejs.
I moved 'core' functionality to separate git repository, and each 'service' add that core as npm dependency.
In service I use core as
require('core/module1');
In that case nodejs takes 'core' from node_modules, it's ok for production but for development I want to take 'core' from the external folder not from node_modules.
My main idea - do changes in 'core' and immidiately get the result in 'service'.
I cannot use NODE_PATH for specify external 'core' folder, because I've used it now.
I found solution to use 'app-module-path' module for adding additional directories to the Node.js module search path.
if(isDevelopment()){
require('app-module-path').addPath('path_to_core_folder');
}
It's working solution, but maybe you can suggest some more clear way?
My folders structure
- core
module1
- service1
-index.js
-node_modules
-core
Thanks.
Either one works. If you require('modulename') and it's present in your node_modules folder, it will be loaded from there. If you want it to load from another folder or from your main folder, you need to do require('./modulename") which will look for it in the current folder. Alternatively you can do require('./my_modules/modulename') which will work for a subfolder.
i would look at the mockrequire module, it allows you to redirect the directory your modules are loaded from
npm link is the answer to your problem. You can run the below command in the root directory of 'service1' (where your package.json is present)
npm link [../relative-path-to/library]
Refer to https://docs.npmjs.com/cli/v7/commands/npm-link for more details about npm link.

Load custom modules from root directory with NPM

Probably a really simple answer but I'm lost. If I'm in a file several directories below root is there a way I can require ('mylib/module1') in that file and have it search for the 'mylib/module1.js' from the root rather than the relative path of where I'm using the require statement?
Obviously if I require('underscore') it will just look for it inpackage.json, but I couldn't find any way to set up a reference inpackage.json` to point the right folder, something like:
dependencies: {
"mylib/module1" : "./mylib/module1.js"
}
I did find a reference to browser that allows you to map to client-side files, but I'm looking for something that will work on the back-end.
One way of doing it without needing npm support is to rename your mylib folder to node_modules. You can then simply require('module').
The reason this works is because require searches recursively up the directory structure until / looking for folders called node_modules. This very simple mechanism allows node to have multiple levels of lib directories which can be overridden at multiple "local" level where necessary. For example, you can have the following directory structure:
/usr
/home
/diplosaurus
/node_modules
module1.js <-------- version 1
/code
/node_modules
module1.js <-------- version 2
/projectA
/node_modules
module1.js <------- beta version
main.js
/projetB
main.js
All code in your home directory would use version 1 of module1 but code in the code directory would use version 2 and projectA uses the beta version.
This also means that you can let npm manage your project's node_modules directory yet maintain a manually managed, higher level node_modules directory for your personal libraries.

Node.js npm dependencies in subfolder

I have a project in which I use node-webkit. node-webkit allows npm packages to be used for developing desktop applications. I make use of grunt to build my application.
My folder structure looks like this at the moment:
project root
node_modules/ (1)
package.json (1)
App/
node_modules/ (2)
package.json (2)
bower.json
bower_components/
...
controllers/
filters/
...
app.js
The npm dependencies for the application itself are kept within the App folder, but the dev dependencies for building the project are not related to the application source code, so i keep them in node_modules (1) inside the root folder. I also know that in a package.json file one can express dependencies and dev dependencies, exactly for this reason. I would rather have one package.json file in the root expressing ALL dependencies, including dev dependencies, but i would rather have a separation of those dependencies on folder level.
Two questions arise:
Is this a good way to organize my npm dependencies? If yes, awesome? If no, which I expect:
What is a better way to organize my dependencies? Is it possible to specify that dev dependencies go into folder a, and 'regular' dependencies go into folder b? If so, how do I do this?
In case anyone is wondering, this is the project i am talking about:
https://github.com/michahell/pinbored-webkit
[updated folder structure to include app.js for clarity]
It is perfectly fine to keep more than one package.json file and multiple node_module directories for a project. If you consider the parts as separate components.
An example might be if, you have one directory containing a node server, another containing a react app, and a third containing some kind of deployment script written in javascript.
#Michael package.json file contains all the dependencies related to that project.There is no need for multiple package files and multiple node_modules folders..
But you need to check where is your App.js file!!
your App.js , package.json must be in same folder unless configured.

Grunt doesn't look for node_modules in parent directory? like node does

I keep all my node_modules in the parent directory one level above the main project. Which works fine with node. But it seems grunt needs node_modules in the project directory.
Is there a way to get grunt to look in all parent directories to load whatever modules it needs?
http://runnable.com/U-xEnRK8Dhtr-eNY/output
Seems like this isn't possible and probably wouldn't be in the future either.
https://github.com/gruntjs/grunt/issues/696#issuecomment-21634036

Resources