node.js cannot find module xml2js - linux

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

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

Cannot find module: Ember-template-compiler after upgrading to 2.12.1

After upgrading to Ember cli 2.12.1 I receive the following error, when trying to serve ember.
Cannot find module 'C:\Users\assaue\web\client\bower_components\ember\ember-template-compiler'
Error: Cannot find module 'C:\Users\assaue\web\client\bower_components\ember\ember-template-compiler'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Class.htmlbarsOptions (C:\Users\assaue\web\client\node_modules\emberx-file-input\node_modules\ember-cli-htmlbars\ember-addon-main.js:72:25)
at Object.toTree (C:\Users\assaue\web\client\node_modules\emberx-file-input\node_modules\ember-cli-htmlbars\ember-addon-main.js:31:43)
at C:\Users\assaue\web\client\node_modules\ember-cli-preprocess-registry\preprocessors.js:180:26
at Array.forEach (native)
at processPlugins (C:\Users\assaue\web\client\node_modules\ember-cli-preprocess-registry\preprocessors.js:178:11)
at module.exports.preprocessTemplates (C:\Users\assaue\web\client\node_modules\ember-cli-preprocess-registry\preprocessors.js:161:10)
Try updating below in package.json
"ember-cli-htmlbars-inline-precompile": "^0.3.6"
Maybe any one of your addons might expect ember to be installed through bower.
I think some of your addon listed in package.json requires ember js to be in bower directory. For my app, the addon was jquery-ui and it was referred as following.
templateCompilerPath = this.project.bowerDirectory + '/ember/ember-template-compiler';
Add
"ember": "2.13.0" ( Latest Version now )
in bower.json
Ember has moved ember package from bower to npm as ember-source, but still it doesn't work as expected because of third party addons
when you don't have ember package in bower.json.
You may get WARNING: Ember.js is now provided by node_moduleember-source, please remove it from bower in terminal when you run your ember app.
I am pretty sure that the problem is in the emberx-file-input module judging from your log.
at Class.htmlbarsOptions (C:\Users\assaue\web\client\node_modules\emberx-file-input\node_modules\ember-cli-htmlbars\ember-addon-main.js:72:25)
Key fragment is \emberx-file-input\node_modules\ember-cli-htmlbars
It doesn't have the correct ember-cli-htmlbars yet and hence it throws the error. Try to find the version of the emberx-file-input which has the correct dependency.

How to add node require libraries to path?

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.

Error: Cannot find module '.../webpack'

I'm just starting with node, and server stuff in general. I was following along with a well-reviewed youtube video and am at a loss here. I installed webpack and webpack-dev-server both globally and in my project folder. Now if I try to run webpack (typing nodejs webpack in the terminal) or webpack-dev-server, it just give me this error:
andrew#AndrewLaptop:~/Documents/mean-todo-app$ nodejs webpack
module.js:340
throw err;
^
Error: Cannot find module '/home/andrew/Documents/mean-todo-app/webpack'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
Shoudln't it be looking in mean-todo-app/node_modules/webpack instead of mean-todo-app/webpack?
Again, new to all of this so I could be missing something stupid, but I did search to no avail.
Thanks
edit: guess I could have mentioned I'm on Ubuntu if that matters
try using
npm install webpack
and do this in the root directory of your app.
var webpack = require('webpack');
you can use './webpack' only if you have something.js in the same directory
if you require modules installed from third party source like npm use only require('webpack');
Directly use webpack on your terminal. You don't need to refer nodejs here. Refer this documentation for executing webpack via command line as well as npm scripts: http://survivejs.com/webpack/developing-with-webpack/getting-started/#adding-a-build-shortcut
I had this problem as well. Struggled a few hours trying to figure this out. I'm on windows 10, so hopefully this will help you...
First, you need to run 2 separate processes. In Windows Power Shell, you run: node server
THEN... in another WPS, you run: webpack-dev-server
From Chrome, type in: localhost://8080
You don't want to run "node webpack-dev-server" in the second shell. That kept throwing me off.

Cannot find module connect/utils when installing node+express

I just installed node by cloning the git repo + make && make install. I then installed npm, and used npm to install express (seems like it fetches connect automatically since it's a dependency). However, when trying to start a simple express example i get the error message:
Error: Cannot find module 'connect/utils'
at Function._resolveFilename (module.js:219:11)
at Function._load (module.js:171:25)
at require (module.js:246:19)
at Object.<anonymous> (/home/yngve/local/node/lib/node/.npm/express/1.0.3/package/lib/express/response.js:16:13)
at Module._compile (module.js:302:26)
at Object..js (module.js:308:10)
at Module.load (module.js:234:31)
at Function._load (module.js:201:10)
at require (module.js:246:19)
at Object.<anonymous> (/home/yngve/local/node/lib/node/.npm/express/1.0.3/package/lib/express/index.js:52:1)
Seems someone has the same problem here: http://groups.google.com/group/express-js/browse_thread/thread/287086bd30f79cf1/adbb5c669872e45a
but the conclusion seems to be that one needs connect installed (which I have).
I've checked that I have the following files at least:
/home/yngve/local/node/lib/node/.npm/connect/0.5.8/package/lib/connect/utils.js
/home/yngve/local/node/lib/node/.npm/express/1.0.3/node_modules/connect/connect/utils.js
Anyone have a clue?
Resolved
Added
require.paths.unshift('<path_to_node>/lib/node/connect');
But the newest rev. is fixed, so this shouldn't really be necessary (see answer below).
It seems like it was a bug in express. Installing the latest version, 1.0.5, fixed this problem for me.
It sounds like you don't have your require path including your npm directory. Use require.paths to modify that.
Some more info:
http://jherdman.github.com/2010-04-05/understanding-nodejs-require.html

Resources