How to properly organize a module for node.js - node.js

I'm currently learning node.js.
I'd like to create a module named 'bio' that would contain some native C++ extensions and some javascript code.
I've generated a simple "package.json" file with npm init in the root directory and my c++ sources in the src/ folder. The C++ file is compiled to /build/extension.node .
/package.json
/build/extension.node
/src/extension.cpp
Where should I put the javascript files ?
how should I tell node that my javascript files are part of the 'bio' package ?
how should I set the $NODE_PATH ?
Thanks

The javascript files are commonly put to ./lib/. Check small NPM packages such as dirty or request for examples.
To tell node the package name put package.json and other files to a bio folder.
You should not set NODE_PATH. You should rely on node_modules convention instead.
Read http://nodejs.org/docs/latest/api/modules.html#modules_folders_as_modules and related documentation.

Related

Using locally develop npm module

I have developed a nodejs project as an umd library, with the purpose to use it in a another nodejs project. The library project builds fine and generates the index.js file and index.min.js file.
But when I tried installing the library project locally using npm install "asolute path". It brings all the things in the library project. And the size of my project I want to use the library project grew. Seems it is because of all the files in library project is getting copied.
Thanks for any help in advance.
Have you added an .npmignore file?
Also, you'll probably want to use npm link for local development.

How does require work?

I have 2 files in a folder called js. I am using webpack.
js/app.js and js/login.es6.
I'm trying to include the login from my app.js:
require('login.es6') fails
require('./login.es6') works.
Any idea why?
When you write require('login.es6') node will look for a module named login.es6 in your node_modules.
When you write require('./login.es6') node understands that ./login.es6 is a relative path and will load your js/login.es6.js file.
This is needed to distinguish between modules and local files. There may be a npm module called login.es6; this way you can reference both the module and your local file in your project.
The node.js docs on require are pretty nice and gives a good overview of how modules are prioritized when loaded.
The gist is, if you don't start the string with ./, require will first look for a core module, then recursively look in the node_modules directory/-ies. So it's normal to start require calls to local files with ./.

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 module in Alloy project

I need to put this node.js module into Alloy project. It's a Facebook SDK node module.
I put the install command, and a node_module folder appear into my project folder, but I can't use it.
Where have I to put this node_module folder? Why the requires into facebook.js are not founded by the compiler?
Please, can anyone help me?
Titanium is not a pure NodeJS environment. When you use require('test') in a Titanium Alloy project, it will look for a file named test.js in the directory <Your project>/app/lib/.
Titanium can't handle a require on a directory with a package.json.
So if you want a pure NodeJS module, you'll have to put every files needed in the <Your project>/app/lib/ directory.
But keep in mind that you can't use a NodeJS module which depends on NodeJS API like requests, because there's no such things in Titanium (you have to Ti.HttpClient instead).

Resources