How can I test my module locally using a symlink? - node.js

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.

Related

How to build node addon, so that it can be imported only by name, and not path

When I build a node add-on by creating a standard binding.gyp and running node-gyp build, the addon.node library is created in the subdirectory build/{Release|Debug}/. When I write tests or other javascript code to use this module, I have to give explicit path to the library location
For example,
var addon = require('./build/Release/addon')
However, I would like to do this by only specifying the module name and let node's module search for the library
var addon = require('addon')
How can that be achieved?
I believe the only way to do this is setting require() and having the module in the node_modules folder or having the module in a node_modules folder in one of the parent directories.
Node uses the require() method with no path defined as an indicator to look for the module in the node_modules directory. Unless its an native module.
Its detailed in the docs here.
There's a module for that if you're okay with another dependency.
https://www.npmjs.com/package/bindings
That will search all of the possible build output locations automatically, including debug and release directories.

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.

node.js require function not finding module

I have a server.js file that I downloaded from someone's website. The first line is: var express=require('express');
When I try to run this server with "node server.js" I get the following error: "Cannot find module 'express'." The express module is installed in the default node install location:
C:\Users\myname\node_modules\express\
I'm able to successfully run express by executing "node express.js" from the express install location in node_modules. I also tried copying over the express folder and file into my c:\node-testing\ directory where my server.js file is located but I still get the error. Any idea what the problem might be and how to fix?
You can set the NODE_PATH environment variable to tell nodejs to search other paths for globally installed modules that are not in the project directory.
See http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders for details.
On Unix installations there are some built-in default locations, but on Windows, it appears you have to set this environment variable manually to support a global location.
FYI, if you want require to load a module from the project directory, then you have to use
require("./filename");
with the ./ in front of it. That's why it didn't work when you copied it to the project directory. node makes a distinction between loading from the project directory vs. loading from the node_modules directory below and thus requires a different syntax to specify which one you want. Express.js is also not a stand-alone module because it depends on a bunch of other modules so you could not copy only it. I'd recommend using the NODE_PATH option or install express into your project directory (it will end up in a node_modules sub-directory).
Node.js will only search for modules in from the current (and parent) directories. Unlike npm, Node has no concept of "global" modules.
You need to run npm install to install your modules into the directory containing your code.

Get path to npm module folder (with no main)

How do I get path to the folder that contains npm module?
I tried require.resolve but I got Error: Cannot find module 'bower-strapless'.
(even though I had installed the module locally).
The module does not have a main in package.json -- does that matter?
OK, it seems that if there is no main in package.json, you can't just specify a module name — you have to provide a path to (any) file that actually exists. And then it is possible to use path.dirname to get to the package root.
In my case I needed less subfolder, so I used the following: path.dirname(require.resolve('bower-strapless/less/bootstrap.less')).

How to provide module dependencies for required script in a sibling directory

I am running a node.js script (foo.js) that requires a helper script that is located in a sibling directory.
//foo.js:
var magic = require('../util/magic');
magic.js uses a npm module express. However, the main directory of the 'program' (holding package.json and node_modules) is the folder where foo.js is located.
/program
/node_modules
/express
..
/foo.js
/package.json
/util
/magic.js
When running the program, the statement require('express') in magic.js fails - the module cannot be found.
Is there a way to make node.js load the express module from the program/node_modules directory?
I would like to avoid any of the following:
move the node_modules directory to a common parent of util and program
add a node_modules directory to util
pass in a reference of the required modules to magic.js
Appreciate your help!
You could supply a relative path instead the module name, because this is how Node.js is going to check for the modules folder if you don't specify a path:
/util/node_modules
/node_modules
Since you know where the module already exists, just do this instead:
var express = require('../program/node_modules/express');
However, you should place any files related to the module within the module itself when developing modules. If you don't, they don't get packaged with the module either when it is published, and you have this inconvenience of not being able to access dependencies specified in the package file.

Resources