'require is not defined' in Meteor.js when including NPM package - javascript

I'm trying to use a npm package from Meteor.js (Release 0.6.6.3) using Meteor.require. However it throws an error saying that require is not defined. Why is this and how can we solve it?
mrt add npm
npm install github
packages.json
{
"github": "0.1.8"
}
github.js
var GITHUB = Meteor.require('github');
Error
ReferenceError: require is not defined
The npm package has lines such as
var https = require('https')
var url = require('url')
var crypto = require('crypto')
Must the package's code be manually edited to use Npm.require? Editing them manually got rid of the errors.
However theres a line:
module.exports = SOMETHING
How should we call module from within meteor?

Meteor.require is a function added by the meteor npm smart package, which actually doesn't do much for using npm other than wrapping some asynchronous callbacks. It's a few months old, so you might want to try using Meteor's Npm.require directly in case something broke.
The monkey-patching of the Meteor global by this package is misleading.

Making comments above an answer.
Is Meteor.require() a typo? That is what is in your code though your question text refers to the correct Npm.require().
I think module.exports is there for non-meteor use of the same file. Within meteor variables for export should be
declared as globals inside the package
exported with api.export() within the package.js file.
The documentation on this is a bit rough but look at namespacing and writing packages. Also looking into the various meteor packages on github is very useful.

Use Npm.require() in meteor.
Like this:
var fs = Npm.require("fs");
For that you need to have a Meteor package: meteorhacks:npm , npm-container

Make sure you're using the meteor-npm package.
https://www.npmjs.com/package/meteor-npm

Related

vertx module not found when using webpack

When using webpack and libraries that want to use when.js (when), it is possible that the following error is thrown when compiling:
[2] ERROR in ./node_modules/when/lib/env.js 32:14-35
[2] Module not found: Error: Can't resolve 'vertx' in 'path-to-project/node_modules/when/lib'
The issue seems to be isolated to webpack, and is documented on when's github. Running npm install vertx or npm install #vertx/core will not resolve the issue either, since the problem lays in the import of vertx in the when library.
To resolve the issue:
Install #vertx/core (npm i #vertx/core)
Edit the file ./node_modules/when/lib/env.js, and change line 32 from
var vertx = vertxRequire('vertx');
to
var vertx = vertxRequire('#vertx/core');
This will reference the #vertx/core package instead of the vertx package, which does not seem to be able to be found when using webpack and npm. I haven't experienced any issues with just using vertx/core. You might also need to update your gitignore to include the changes to the library if others are also working on your project.
The issue can indeed be solved by adding the #vertex/core package, but instead of editing the ./node_modules/when/lib/env.js file manually as recommended by #Todd Rylaarsdam, I'd use the NormalModuleReplacementPlugin in your Webpack config to replace the old vertx package, like so:
new webpack.NormalModuleReplacementPlugin(
/^vertx/,
'#vertx/core',
)
This way you don't have to make any manual changes to the files of a package you're using.
See https://webpack.js.org/plugins/normal-module-replacement-plugin/ for more info about this plugin.

Get local system home directory path from server

I have a server running, I want to get the local system file path where aws server is running using node js
Use process.cwd() which is also documented here: https://nodejs.org/api/process.html
This might solve your problem:
__dirname
Check with a console log whether it's what you wanted or not.
There are multiple ways of achieving this. One of them is you can use this package,
app-root-path
Install the package with the command :
npm i -S app-root-path //install the package
Use package where you required :
var appRoot = require('app-root-path'); //require package
console.log(appRoot); //use variable as string
You can import the package and use variable where ever you require inside the app.
You can check this package documentation visiting this link.

Custom Flow type definitions apparently not being read

I have run the following:
$ npm install flow-bin
$ yarn global add flow-typed
$ flow-typed install
Which correctly populates ./flow-typed/npm/ with definitions, some are valid and some are stubs.
But these type definitions for libraries are not showing up in VSCode (with the official flowtype module installed, flowtype.flow-for-vscode—everything coming from the libraries show as typed with any on mouse hover), and running the flow checker from the command line returns no errors, even if I deliberately mis-use a library, such as:
const express = require('express');
express.chicken();
All of the sections in my ./flowconfig file are empty.
I have also tried adding my own definitions to the ./flow-typed/ directory (not the npm subdir) to no avail.
Does anyone have suggestions as to what I could be doing wrong and how to fix it?
Seems to have fixed itself when I restarted my laptop.

Aurelia js add npm package

I want to use the following npm package https://www.npmjs.com/package/poker-evaluator
It can also be found on github: github.com/chenosaurus/poker-evaluator
On the project folder I ran the following command line, npm install poker-evaluator --save
And it seems that I installed the library -> Package Json
I want to be able to run the functions from this module(poker-evaluator) and I can't insert this module.
tried multiple times and in different ways ...
Unfortunately, the package you are looking at relies on NodeJS APIs, so it cannot be used in the browser. If you look at its source, you'll see: https://github.com/chenosaurus/poker-evaluator/blob/master/lib/PokerEvaluator.js#L1
var fs = require("fs");
var path = require("path");
These are Node APIs that work with files. You'll have to use this package on your server and wrap it with an API that your Aurelia application will talk to.

Sharing code between AngularJS and Nodejs

What is the best way of sharing code between frontend and backend using javascript, specifically saying between nodejs and angularjs?
Thing is that we are using same enums and constant values such as error codes in both backend and frontend. Right now we just copy&paste each change to both platform which isn't a good solution. There are also some services that could be shared.
I have seen libraries such as browserify; but thats not exactly what I am looking for. I am looking for a solution similar to maven dependency in java. In java, libraries can be shared easily using maven, whereas I can't find a similar way of doing that in javascript.
Is there a way to isolate these services and give them as dependency to nodejs using npm and to angularjs using bower independently? Or what are the ways for sharing the same code between frontend and backend?
There are several ways to do this. The first is that you can make a new package which is required via bower for the front-end code and via npm for the backend code. I have several packages published to both systems.
Install with Bower -- information on how to install modules that aren't in the registry
NPM Install docs -- all the ways to install with npm (private github with auth: git+ssh://git#github.com/[org]/[repo])
Just create a new module with your shared data and install it using both package managers. Both of them allow you to install an unpublished module so if it's private data you can keep it as such.
If your front end requires require.js you can use something like amdefine to make it available to your node backend, or if you're just using legacy window code you can do something like:
var mydata = {};
if(typeof window !== 'undefined'){
window.mydata = mydata;
} else {
module.exports = mydata;
}
If you are sharing a lot of data though I highly recommend looking into browserify to write your entire codebase in commonjs and using browserify to generate your client bundle. There is a laundry list of resources about using browserify, including stuff on how to use browserify and angular together
Disclaimer - I'm still developing this approach and it's a little manual.
I did this using npm, an npm cli called pac, and bower. Pac let's me avoid using npm install in production by keeping the modules as .tgz files (committed to project in source control). With pac, when someone checks out the node project, they run pac install then npm rebuild instead of npm install.
My shared code is kept in a directory (my-module). It has both package.json and a bower.json.
My consuming node app has a package.json dependency for:
"my-module" : "x.y.z"
My consuming client has a bower.json dependency for:
"my-module" : "../relative/path/to/my-module"
When I make updates to my-module, I update my node app by:
Making a tar.gz of the contents of my-module:
tar -czvf my-module.tar.gz -C my-module
Removing the old version from the node app's node_modules
Rerunning npm install path/to/my-module-tar.gz
Rerunning pac (this makes a .tgz of node_modules/my-module)
Committing the updated pac .modules/my-module.tgz
I update my client by:
Removing the old client/bower_components/my-module
Rerunning bower install or bower update

Categories

Resources