node js elementtree required get Cannot find module 'sax' - node.js

i am attempting to use elementtree in my node js server
however the following message is genearted on server startup
Cannot find module 'sax'
i had to install elementree from zip, what have i done wrong?
these are my rquire statements
var cache = require('./node_modules/node-cache');
var elementTree = require('./node_modules/node-elementtree');
this is my folder structure
module\node_modules\node-cache
module\node_modules\node-elementtree
my server js script resides in \module

The problem is because you are not using npm for installing your node-elementtree module
This is the package.json of node-elementtree. and it is clearly mentioned that there is a dependency on sax 0.3.5
"dependencies" : {
"sax": "0.3.5"
}
Incase if you had done npm install it would have resolved your dependency constraints.
Inside your node-elementtree module it uses sax here and your require didn't resolve to a proper module. Hence the error. Incase you can't use npm then install sax too using zip if possible but it may increase your dependency list once again as the above. So, Try resolving npm issue
Also you don't need to use
var cache = require('./node_modules/node-cache');
var elementTree = require('./node_modules/node-elementtree');
in your code
var cache = require('node-cache');
var elementTree = require('node-elementtree');
the above would do. Node automatically locates the module from ./node_modules directory and even from many more places

Related

Meteor as dependency in npm module? Error: Cannot find module 'meteor/mongo'

I have 2 meteor projects which share models. I'd like to consolidate the model schemas into an NPM private module.
My module contains this code:
import { Mongo } from 'meteor/mongo';
import { packageSchema } from './schema';
export const Packages = new Mongo.Collection('packages');
Packages.attachSchema(packageSchema);
export default Packages;
When I install this NPM module in my parent meteor project and start the server, I get the following error on the first line of this file:
Error: Cannot find module 'meteor/mongo'
What is the correct way to install/reference meteor in an NPM module? (It cannot be added as a standard NPM dependency.)
I was not able to find a way to include meteor in my NPM module. Instead, I put the code in an Atmosphere package and used the git subtree methods to include it in both projects. (This avoided known issues with submodules and symlinks.)

How can I pass process.env from main to my npm package

In my npm package I want to access process.env.MYCUSTOMVAR, which is from main.app. When I try to print process.env.MYCUSTOMVAR in the package, it returns {}. How can I access this variable?
In case it is relevant: my npm package has built using babel from webpack build.
Consider instead exporting that object in main.app and accessing it from external scripts using the require syntax:
// main.app.js
// ...whatever code you have...
module.exports.MYCUSTOMVAR = process.env.MYCUSTOMVAR;
// your_package.js
var MYCUSTOMVAR = require("./main.app").MYCUSTOMVAR;
That should work!

How to include dependencies in J2V8

How to include dependencies in J2V8? I would like to use certain dependencies in the javascript file for instance the crypto package.
var crypto = require('crypto');
function foo(){ return crypto.createHash('md5').update('Apple').digest("hex");}
However, I got the following error saying require keyword is undefined.
undefined:1: ReferenceError: require is not defined
var crypto = require('crypto');
^
ReferenceError: require is not defined at <anonymous>:1:14
com.eclipsesource.v8.V8ScriptExecutionException
at com.eclipsesource.v8.V8._executeVoidScript(Native Method)
Can anyone tell me how to import an package into J2V8?
Unless you're working with Node, require is not a feature. Usually, you want to use a bundler like webpack to pack your structured source code into one large file so that it can be understood by browsers. This way you can use require and npm packages for your frontend code, which makes development easier, and a bundler turns it with every build (or live update) into a different format, that's hard to read for humans, but is valid Javascript.
I have had success using node modules in J2v8, please check out this blog :http://eclipsesource.com/blogs/2016/07/20/running-node-js-on-the-jvm/
NodeJs nodeJS = NodeJs.createNodeJs();
After registering callbacks
nodeJs.exec(File thescripttoexecute)
Make sure you have the proper path to the node modules in the require() command.
You may have to make a nodejs package that takes dependencies and exports what you need. Then, You have to execute npm install manually.
or You can just npm install what-you-need.
Create Node JS Runtime, and use require with your your-package-dir/index.js or exact location of module that you need. like this:
V8Object module = nvm.require(file);
Now you can call the function likes:
module.executeJSFunction("test");
To deliver entire dependencies you have to bundlize module directory your self.
What if you have to support cross-platform, refer https://www.npmjs.com/package/node-pre-gyp also.

Can I access locally-installed packages from a globally-installed package?

I don't know if I've worded the question properly, so I apologize if it isn't clear from the title what I mean.
Say I have an NPM package which installs an executable. Presumably I want users to install this package with the -g flag so that they can run it whenever.
In this case, when I call require() from within the executable, it will look for packages installed globally.
But suppose this package provides generic functionality for Node projects. I might want to know which packages the current project has installed locally. Should I just assume:
path.join(process.cwd(), 'node_modules')
Or is there a more "correct" way to set the NODE_PATH in this case? (Or rather than set NODE_PATH, should I just require(absolute_path_to_file)?)
require will not only lookup the package inside $(CWD)\node_modules but also inside all node_modules of parent, grandparent, etc. So you can use resolve on npm to solve this problem
FILE: your_global_command.js
// npm install resolve
var resolve = require('resolve').sync;
// Lookup for local module at current working dir
function require_cwd(name) {
var absolute_path = resolve(name, { basedir: process.cwd() });
return require(absolute_path);
}
// Load local express
// this will throw an error when express is not found as local module
var express = require_cwd('express');
I also create a package to require a package at current-working-dir (instead of __dirname of module):
https://npmjs.org/package/require-cwd

Determine NPM modules used from a running node.js application

Other than grabbing the package.json file at the project root is there a way to determine the list of dependencies of a running node.js application? Does node keep this meta information available as some var in the global namespace?
If you are just looking for the currently installed npm packages in the application directory, then you can install the npm package (npm install -g npm) and programatically invoke ls to list the installed packages and the dependency trees.
Obviously, this has no bearing on whether the installed packages are actually require'd in the application or not.
Usage is not that well documented but this should get you started.
var npm = require('npm');
npm.load(function(err, npm) {
npm.commands.ls([], true, function(err, data, lite) {
console.log(data); //or lite for simplified output
});
});
e.g.:
{ dependencies:
{ npm: { version: '1.1.18', dependencies: [Object] },
request: { version: '2.9.202' } } }
Otherwise, I believe the only other option is to introspect the module module to get information pertaining to the currently loaded/cached module paths. However this definitely does not look to have been developed as a public API. I'm not sure if there are any alternatives so would be keen to hear if there are e.g.
var req = require('request'); // require some module for demo purposes
var m = require('module');
// properties of m contain current loaded module info, e.g. m._cache
I believe you could use require-analyzer, which sort of works according to Isaacs(could miss some). You could hear this in Nodeup's first podcast from 11:55.
Or you could try James node-detective which probably will find your dependencies better(but not by running code), but because of Javascript dynamic nature(12:46).
detective
Find all calls to require() no matter how crazily nested using a
proper walk of the AST.
P.S: to expose those package.json variables to node.js you could use node-pkginfo

Resources