Mojito - Load NodeJS Module - javascript

I'm using :
Mojito 0.5.5
NPM 1.2.14
NodeJS 0.8.22
I'm trying to create a simple Mojito application using NodeJS modules, packaged with NPM.
Documentation say that :
Create a Mojito application.
Add any needed dependencies to dependencies object in package.json.
Install dependencies with npm: {app_dir}$ npm install
When Mojito starts, your application will have access to the installed npm modules.
Indeed, the example above isn't working.
"dependencies": {
"mojito": "0.5.x",
"async": "0.2.x"
},
status: function(ac) {
var async = require('async');
ac.done({
status: 'loaded'
});
}
Mojito server : "Error: Cannot find module 'async'"
Web page error : "Error: MODULE_NOT_FOUND"
Any clue ?
PS : Of course, after npm install, the project folder have a node_modules folder with the async package.

As today, mojito npm package is used as a cli but it also holds the mojito core. This is problematic because most developers will run the app by doing mojito start, which runs from the global mojito package. This, plus the fact that the current YUI is messing with the require, it causes a lot of troubles when requiring dependencies.
The solution is simple, make sure you install mojito locally in your application, and run the app by doing ./node_modules/mojito/bin/mojito start or by doing node server.js instead of mojito start.
Aside from that, we are working on splitting the cli and core into separate packages, and waiting for YUI 3.9.0 to solve this problem once for all so developers can keep using mojito start.

Related

How to run npm commands like create-react-app offline

I am working offline without any internet connectivity. I have node JS installed on my system. When I am trying to run the npm create-react-app command it gives me error. Is there any way for me to run npm commands and get react application running offline? Below is the error code I get:
npm ERR! code ENOTFOUND
npm ERR! errcode ENOTFOUND
npm ERR! network request to https://registry.npmjs.org/create-react-app failed,
reason: getaddrinfo ENOTFOUND registry.npmjs.org
and get react application running offline
npm start works offline already. Once you created your application, you should have no problem.
i am trying to run the npm create-react-app command [offline]
Npm is basically a package manager, which downloads dependencies for you, located under /node_modules at your project root.
create-react-app does two things :
Install specific dependencies for React : as a huge framework, React doesn't only require Node.js to run, but also multiple other packages (including React istelf).
Creating a template project with some presets.
If you are working offline and need to create multiple React projects that way, then you'll have no choice but to run the create-react-app command at least once while being online.
However, once you created your first React app, you can just leave the folder untouched : this will give you a fully functional offline copy for your React applications.
When you're back offline, just copy those files to the folder of the React application you want to create (make sure to copy everything, including the node_modules folder). You should then be able to run npm start offline, and build your application.
I believe this is what you are looking for
https://npm.io/package/create-react-app-offline
It helps you create (download) a react project installer, which you can use to create any react JavaScript project offline.

Is there a way to have NPM build a package after it is installed?

I am currently using a github url as a package which is basically a private base library for my application.
While a npm package will run the npm build script after it is installed it currently appears that if you use a github url as package no build script will be run after installation (Yes in that thread they say that if using npm 5 a prepare script will be run but from my testing it does not work).
I am wondering if it's possible to define a script in a parent npm package.json that would run the build script of a dependency after it installs?
Or if you have any better way to deal with this problem would be most welcome.

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

Use node.js module in a Titanium app?

Currently I am writing a small Titanium app for testing. I need to include a module from NPM to titanium.
In this case I am trying with https://github.com/oortcloud/node-ddp-client
I am having the error saying Titanium couldn't find module.
The code I used for include is
var DDPClient = require("./lib/node_modules/ddp");
Can I use node.js modules in Titanium?
Thank you
require('./lib/node_modules/ddp/lib/ddp-client.js');
It's very likely this module won't work for you. It has a lot of dependencies that use NodeJS specific modules, and specific APIs.
Luckily, someone has already written a module to connect to a Meteor server using DDP (I plead complete ignorance of this protocol and stack, by the way):
https://github.com/yubozhao/Ti.Meteor
You can use try this module https://github.com/smclab/titaniumifier
Get a Titanium™ SDK CommonJS module out of a Node package!
Titanium now has partial support for npm modules: http://docs.appcelerator.com/platform/latest/#!/guide/Node.js_Support
For Alloy projects, do your npm install commands in app/lib so that your packages are stored in app/lib/node_modules.
For non-alloy projects, do your npm install in Resources/ so that your packages are stored in Resources/node_modules.
Note that you may have problems with packages that rely on native node modules.
sure, why can't?
here is an example using node module in Alloy project:
1.install q.js, which will create a folder named "node module" and contain some files :
$ npm install q
$ find node_module
node_modules/
node_modules/q
node_modules/q/README.md
node_modules/q/queue.js
node_modules/q/package.json
node_modules/q/q.js
node_modules/q/LICENSE
2.copy the q.js to your app/lib/ folder:
$ mkdir app/lib
$ cp node_modules/q/q.js app/lib
3.declare it in your Titanium file:
// in app/alloy.js
Q = require('q')
4.use it in your controller:
// app/controllers/index.js:
var defer = Q.defer();
refer to: http://developer.appcelerator.com/question/154529/how-to-use-nodejs-modules-with-titanium#answer-285207

Node NPM - install versus install -g

I am a node newbie and am somewhat confused with the whole "install" thing.
What is the difference between install, and install -g?
Can something installed with install -g be accessed anywhere, or does this make it available to the Node server but not your application? Is there any reason to use one, and not the other?
Cheers
From the node.js blog:
If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
So for example, lets say you wanted to install the Grunt CLI. Odds are you'll use Grunt in multiple projects, so you'll want to install that globally using -g.
Now lets say you're working on a project and your project is going to require a module such as Express. You would cd to your projects root directory and install the module without -g.
Here is a more in depth explanation.
install means the module will be created in your local node_modules folder which is highly recommended for anything your application relies on (for versioning, amongst other reasons).
install -g means install the module globally on your machine. This is generally only recommended to use with modules that perform some task unrelated to the execution of your application.
Simple examples of this are Yeoman generators, the Express generator, PhantomJS, etc.
There is an official blog post about it here
The only difference is npm install mod will install it in your local directory. Let's say you are working in 'projectA' directory. So
> npm install mod
will install "mod" in
> projectA/node_modules/mod/
so any .js file inside projectA can use this module by just saying require('mod')
whereas 'npm install mod -g` will install it globally in the user's node module directory. It will be somewhere in
> /usr/bin/npm/node_modules/modA
you can use this module anywhere in any of your project, in addition to that if there is any terminal command is there in 'modA'. It will be accessible from you terminal directory.
> modA --version
> version 1.1

Categories

Resources