How to specify node_modules folder for locally referenced node module - node.js

We need to specify node_modules folder for locally referenced node
module that resides in an external folder relative to the current
application folder.
We have a scenario where we are referencing an external common module’s js files within another client-side app, however the same node modules being references in both are being duplicated in the final build. we have tried using webpack.optimize.DedupePlugin() without any success.
The directory structure is as follows:
We have a private npm module on the local disk called ‘common’, and then we have our project folder called ‘app’.
Both folders are on the same level in directory structure & are under the same root.
The ‘app’ itself is a npm module & has a package.json file with its own node_modules folder.
|—root
|— common/node_modules
|— app/node_modules
We want to specify via configuration either via npm or web pack a way for the app to resolve references from its specified path of node_modules and not to refer to any other node_modules location including the one in common module.

You can use npm link or just a simple symlink. Do something like that:
cd root/app (your app directory)
npm link ../common
OR
cd root/app (your app directory)
ln -s ../common node_modules/common

Related

Copy folder from inside node js project directory to specified directory

So, I'm building my own node js module. My own node js module will be installed globally and I want it to be able to copy folders or files from inside its directory to a specified folder. How can I achieve this?
I've tried with fs-extra and fs, but it returned errors:
btw, these are the directories that I want to copy:

How are relative paths resolved inside of node packages?

I am making a npm package which, after installation, runs a postinstall script located in the lib/ directory. This postinstall script in turn copies a config file, located in the same directory (as the module), to the user's project's root using fs.copyFile function. But I am a little confused about how the two paths are referenced when from inside the node_modules directory.
So, long talk made short, I want to figure out what prefixes should I use to refer to files in MY module and for the user's project's directory. Basically, how are relative paths resolved by node when referenced from a script inside the node_modules directory?

get source folder when executing globally installed npm cli package

I am creating my own CLI using NodeJS. One task of the CLI is to copy folders recursively from a source folder within the project.
So, I install my CLI globally using npm install -g from within my CLI project folder.
Now I can run it in another test folder.
However, when I run it, the recursive copy fails because it is trying to find the files from the source folder in the test folder I created. Not from /usr/local/lib/node_modules/[myCli].
I have tried many solutions using path and require.resolve and __filename but they all give me a folder starting from the test folder in which I am executing my CLI.
Other then hardcoding the source path to /usr/local/lib...., there most be a way to get the folder of the globally executed script?
If you search for the current working directory its process.cwd() https://nodejs.org/api/process.html#process_process_cwd
__filename give you only path of file where it installed in node_modules
To find your project files you can use rc file in your home directory containing the path of your project
https://en.wikipedia.org/wiki/Run_commands
in hope this helps

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.

How can I test my module locally using a symlink?

I am trying to run a module that I am developing on my local machine. So I have create a test nodejs application and within this test application I created the 'node_modules' director. The folder structure looks like it would if I had used npm install and specified my module as a dependency. Within the node_modules folder I have create a symlink to the directory where my module under test is.
When I attempt to run my test application node complains: Cannot find module 'my_module'.
I can figure out why this wouldn't work. Can this be done in this way?
Thanks!
The recommended way for doing this is:
In your my_module directory, do npm link .. npm will tell you it has made my_module available for linking.
Then, in your test_app directory, do npm link my_module. npm will now make my_module available to your app.
Note that you can still get a a "cannot find module" error this way, but this is then most likely because your my_module module is structured wrongly. By default, Node.js will look for an index.js file in the module's root directory. Otherwise, you need to specify a main entry in the module's package.json, containing the path to your main js file, relative to the module's root directory.

Resources