How to add node require libraries to path? - node.js

After installing bitcoinjs and firing up node and running :
var bitcoin = require('bitcoinjs-lib');
I receive error :
Error: Cannot find module 'bitcoinjs-lib'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at repl:1:15
at REPLServer.defaultEval (repl.js:262:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:431:12)
at emitOne (events.js:82:20)
When I run these commands it appears to find the library :
$ pwd
/usr/local/lib/node_modules
$ ls
bitcoinjs-lib browserify npm uglify-js
$ node
> var bitcoin = require('bitcoinjs-lib');
undefined
I think it works in this case because I'm running node from library /usr/local/lib/node_modules
How can I add the modules library to node so can find a required library from any location instead of having to change directory into /usr/local/lib/node_modules?
I've tried modifying path in bash_profile to :
PATH=$PATH:/usr/local/lib/node_modules
But this does not fix issue.

To get this working the quick way, you need to set $NODE_PATH:
NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules
However, in general, Node modules shouldn't be installed globally (which means in a "system directory" like /usr/local/lib/node_modules).
Instead, you create a project directory, and install the module there:
$ mkdir my-project
$ cd my-project
$ npm install bitcoinjs-lib
If you run Node in that project directory, require('bitcoinjs-lib') will work.

A hacky way:
module.paths.unshift(<path>);
process.env.NODE_PATH = <path>
module.constructor._initPaths();
I figure out the above solution when I wrote a Cocos Creator(based on Electron) plugin which is loaded during the IDE opening. And I need to require a utils script from that plugin which is not located in any require paths. I tested the first code snippet in CC and Node.js v10.16.0 for Win10.

Related

npm run gulp primordials not defined

I'm trying to run npm run gulp and I'm coming up with this primordials is not defined error (I have also added the npm-shrinkwrap.json file as suggested which gets updated after running npm install but it doesn't fix my primordials is not define error):
> gulp-tutorial#1.0.0 gulp
> gulp
fs.js:47
} = primordials;
^
ReferenceError: primordials is not defined
at fs.js:47:5
at req_ (/Applications/MAMP/htdocs/wordpress/wp-content/themes/theming-main/node_modules/natives/index.js:143:24)
at Object.req [as require] (/Applications/MAMP/htdocs/wordpress/wp-content/themes/theming-main/node_modules/natives/index.js:55:10)
at Object.<anonymous> (/Applications/MAMP/htdocs/wordpress/wp-content/themes/theming-main/node_modules/vinyl-fs/node_modules/graceful-fs/fs.js:1:37)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
Node.js v17.0.1
Any suggestions 🙌 ?
Primordials are used by Node.js internal modules and are not accessible to end user scripts. (That's their entire point for existing. Otherwise, you could modify Object.prototype and end up affecting Node.js internals, causing weird crashes and things like that.)
The module that is giving you this error is the natives module. The page for the package says:
This module relies on Node.js's internals and will break at some point. Do not use it, and update to graceful-fs#4.x.
Sounds like it's at that point where it's broken. Use npm ls natives to find out what's using it. (You might need to be in the /Applications/MAMP/htdocs/wordpress/wp-content/themes/theming-main directory when you run the command.) From there, you can try to figure out how to get rid of it.
Another possibility would be to try downgrading to an earlier version of Node.js to see if it works with that version.
The problem was with the versions not matching up. I also had 2 NodeJS installation paths and that was throwing things off.
I uninstalled them all, started from the beginning and used brew to reinstall.
Then checked that the versions were all compatible.
I also had to install node-sass and referred to this compatibility table to get the correct version - https://github.com/sass/node-sass#node-version-support-policy
Now its working and I can get on developing :) As of today Im running Node v17.0.1 Npm v8.1.0 Gulp cli v2.3.0

Using nodejs bindings to run native c++ code produces an error

I'm trying to develop an electron app, and I would like to use an npm package that gives vibrancy to a window. This is the package I'm trying to use: https://github.com/sebascontre/windows10-fluently-vibrancy
It uses a visual c++ code to call native windows methods to achieve this vibrancy effect.
I've cloned and I've run the npm install and build as described in the readme and ran the following:
npm install
npm run conf
npm run rebuild
All built successfully and I also see the build\debug artifacts of the c++ build.
When I try to run it (as described in readme):
cd spec\app
electron .
I'm getting the following error message:
Uncaught Exception:
Error: The specified module could not be found.
\\?\F:\Development\windows10-fluently-vibrancy\build\Debug\Vibrancy.node
at process.func [as dlopen] (electron/js2c/asar.js:155:31)
at Object.Module._extensions..node (internal/modules/cjs/loader.js:857:18)
at Object.func [as .node] (electron/js2c/asar.js:155:31)
at Module.load (internal/modules/cjs/loader.js:677:32)
at tryModuleLoad (internal/modules/cjs/loader.js:609:12)
at Function.Module._load (internal/modules/cjs/loader.js:601:3)
at Module.require (internal/modules/cjs/loader.js:715:19)
at require (internal/modules/cjs/helpers.js:14:16)
at bindings (F:\Development\windows10-fluently-vibrancy\node_modules\bindings\bindings.js:112:48)
at Object.<anonymous> (F:\Development\windows10-fluently-vibrancy\index.js:1:35)
According to the message it cannot find the Vibrancy.node file - But I've verified that it exists in the specified location.
Any ideas how to make it run? I have cloned the package to test it since when trying to use the npm itself in my project it gave the same error so I figured I would try the source.
Today I had the exact same issue you're describing here.
I got it to work using electron-rebuild.
From the root of your project run the command: electron-rebuild
Hope this helps you, if not, maybe someone else.

Create npm package using webpack

I'm creating an npm package and using webpack for loader like babel, eslint etc.. However I'm under assumption that final compiled version of the package should only contain that one module, without webpackBootstrap.
My current package, webpack config and source. I stripped it down to just make it "work".
Steps I took to check if it's working:
npm install
npm run build
npm install -g .
node
var test = require('test-package');
Resulting in this error:
Error: Cannot find module 'test-package'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at repl:1:12
at REPLServer.defaultEval (repl.js:248:27)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:412:12)
at emitOne (events.js:82:20)
I'm new to webpack and npm so let me know if you need any more information.
Set output.libraryTarget to umd. That will give you something that's easy to consume from various module systems (global, AMD, CommonJS).
output.library is another useful field to set. That should match the name of your library global you want.
There was another problem beyond this. To make the import work npm link needed be used. This feature is highly useful during development. You can revert a link through npm unlink.

Why isn't the underscore module available in the Node.js console?

I ran the following code to install the underscore js module:
npm install -g underscore
I then tried to access it via the node console, but I get the following error:
node
> __ = require('underscore');
Error: Cannot find module 'underscore'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at repl:1:6
at REPLServer.self.eval (repl.js:109:21)
at rli.on.self.bufferedCmd (repl.js:258:20)
at REPLServer.self.eval (repl.js:116:5)
at Interface.<anonymous> (repl.js:248:12)
at Interface.EventEmitter.emit (events.js:96:17)
Why doesn't this example work?
I don't really know why, but it fails indeed (when installing underscore globally, as you have done).
If you install it without -g, it should work (be careful, however, as '_' is already used by Node REPL to hold the result of the last operation, as explained here:
Using the Underscore module with Node.js
Do you really need to install it globally?
I just had the same problem
$ export NODE_PATH=/usr/local/share/npm/lib/node_modules
sorted it out for me; this obviously depends on your platform and where npm has installed it. Also, as mentioned in Javo's answer, don't name it _ in the REPL.

node.js cannot find module xml2js

I made an application on my machine, and it works well. I uploaded it to the server, and it is crashing with the following error:
node.js:116
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'xml2js'
at Function._resolveFilename (module.js:289:11)
at Function._load (module.js:241:25)
at require (module.js:317:19)
at Object.<anonymous> (/var/www/node/price/index.js:3:14)
at Module._compile (module.js:373:26)
at Object..js (module.js:379:10)
at Module.load (module.js:305:31)
at Function._load (module.js:271:10)
at Array.<anonymous> (module.js:392:10)
at EventEmitter._tickCallback (node.js:108:26)
This is how my app starts:
var express=require('express');
var http=require('http');
var xml2js = require('xml2js');
var sys = require('sys');
var util = require('util');
I have installed both express and xml2js using npm. I have the exact same version (v0.4.0) for node on my machine and my server.
I have made sure that the path wher xml2js and express reside (/usr/local/lib/node/) is included in the paths where node looks for modules. (I edited the file 'module.js' to print the paths where it is looking for modules.)
node.js:116
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'xml2js', in paths: /root/.node_modules,/root/.node_libraries,/usr/local/lib/node,/var/www/node/price/node_modules,/var/www/node/node_modules,/var/www/node_modules,/var/node_modules,/node_modules
at Function._resolveFilename (module.js:289:11)
at Function._load (module.js:241:25)
at require (module.js:317:19)
at Object.<anonymous> (/var/www/node/price/index.js:3:14)
at Module._compile (module.js:373:26)
at Object..js (module.js:379:10)
at Module.load (module.js:305:31)
at Function._load (module.js:271:10)
at Array.<anonymous> (module.js:392:10)
at EventEmitter._tickCallback (node.js:108:26)
So what is wrong? I have the right path, the module is there. Why can't node find it? And the exact same code runs smoothly on my local machine. If it matters, my machine is a Mac, and the server runs CentOS.
require.paths.push('/usr/local/lib/node_modules');
is no longer valid for node v0.8.1 and above. Instead of using require.paths.push, you can set the environment variable NODE_PATH
export NODE_PATH=/usr/local/lib/node_modules
or if you install npm modules in your home directory, then
export NODE_PATH=~/.npm
As spmason mentioned, Node changed how modules are resolved. I've had the same issue as you and resolved it by installing all modules globally (--global) and adding /usr/local/lib/node_modules to the require before requiring any module:
require.paths.push('/usr/local/lib/node_modules');
require('blah'); // it works!
Node 0.4 changed how modules are resolved and it appears that this breaks xml2js.
Node 0.4 looks in ./node_modules for modules.
For me it did help to just link the modules directory to my project directory with ln -s /usr/local/lib/node node_modules
Personally, I found that the XML2JS module needs to be installed via npm locally. While I've only tried this on Windows, I've written a blog post here
Try installing it in the project instead of the global package directory.
If you're using a package.json to manage dependancies then you can just run npm bundle in the project directory and then add require.paths.unshift('./node_modules') at the top of your app file. In my opinion this is the best practice for all projects (especially considering the speed that the development of node takes place).
I guess the simple answer is the current packages for xml2js and xml2js-xpat are busted.
I ended up using node-xml instead. I wish xml2js hadn't been my the first npm module I tried to install.
just make ln -s /usr/local/lib/node /usr/local/lib/node_modules, but before move the content of node_modules into the original node library node - it helped me :)
Try to install the npm package with the -g flag to make it global
npm install -g package_name

Resources