Package nodejs application with global packages - windows

We have a project which have to be packaged as a zip so we can distribute it to our cliens. With the normal node_modules directory i have no problems. I just put the directory and the node.exe together in my project folder and can start our project on every other computer without installing node or running any npm command.
But now i have a dependecy on phantomjs which needs to be installed as a global package npm install -g phantomjs.
How do i pack modules like this into our project? I first thought of copying phantomjs into the local node_modules directory and set the path variable NODE_PATH to this directory. It doesn't find phantomjs.
Development and client platforms are both windows.

Well, generally it is fine to install global dependencies with the --save flag and call their bins like ./node_modules/phantomjs/bin/phantomjs /*now executes*/ (just as an illustrative example).
However, with with Phantom it's not that simple, since it's downloading binaries and/or even compiling. You would have three options:
ssh into target and just npm install -g phantomjs before or define it in a manifest e.g. Dockerfile just like that, if you are using containers.
Compile it from source as advised here.
If you are using the CLI, then just the --save approach.
So I hardly advise just making a Docker image out of it and ship it as tarball. You can't zip the platform dependent Phantom installation, unfortunately.
Also lots of dependencies like karma-runner-phantomjs look for the path of the global dependencies to resolve it for their use.

Related

Install a dependency and save to package.json for a sub-package in yarn workspaces

I am trying to use Yarn Workspaces for my app that I am splitting into multiple packages such that I can share code between the mobile and web version of the app. Let me explain what I am trying to do with an example.
Let's say I currently have a mobile app called awesome-app. I am refactoring it by taking out the shared code and creating three packages as follows:
awesome-web
awesome-mobile
awesome-shared
Let's say I want to add new functionality to awesome-mobile for which I have to install depA to awesome-mobile. How can I do that such that yarn only installs depA and updates the package.json for the awesome-mobile. I tried using command yarn package <package-name> add depA but it ended up installing all the dependencies again which I want to avoid.
Also, let's say I want to use awesome-shared in awesome-web. Is there a yarn command such that it installs and updates the package.json for awesome-web automatically. Currently, I do it by hand and then I do yarn install in the root folder which ends up installing all the dependencies again.

Do NPM packages have to be installed globally to access their functionality via the command line?

I am having trouble understanding how the -g flag works in NPM. Specifically I'm struggling to understand how it relates to command-line functionality exposed by NPM modules.
I assumed that the difference between installing a package locally and globally was simply that a local package would not be available outside of the particular project. And of course that a globally installed package would be available in any project. I'm from a Rails background so this for me would be similar to installing a gem into a particular RVM versus installing it into the global RVM. It would simply affect which places it was available.
However there seems to be more significance than just scope in NPM. For packages that have command-line functionality, like wait-on, the package (as far as I can tell) is not available on the command line unless it's installed globally.
Local install doesn't make the command-line functionality available:
$ npm install wait-on
$ wait-on
=> -bash: /usr/local/bin/wait-on: No such file or directory
Global install does expose the command-line functionality
$ npm install wait-on -g
$ wait-on
=> Usage: wait-on {OPTIONS} resource [...resource]
Description:
wait-on is a command line utility which will wait for files, ports,
sockets, and http(s) resources to become available (or not available
using reverse flag). Exits with success code (0) when all resources
are ready. Non-zero exit code if interrupted or timed out.
Options may also be specified in a config file (js or json). For
example --config configFile.js would result in configFile.js being
required and the resulting object will be merged with any
Can you expose the command-line functionality using a local install?
Is it possible to install locally but also get the command line functionality? This would be very helpful for my CI setup as it's far easier to cache local modules than global modules so where possible I'd prefer to install locally.
If you are using npm 5.2.0 or later, the npx command is included by default. It will allow you to run from the local node modules: npx wait-on
For reference: https://www.npmjs.com/package/npx
I think you can access locally installed modules from the command line only if you add them to your "scripts" section of your package.json. So to use the locally installed version of wait-on, you can add an entry in "scripts" section of package.json like so "wait-on": "wait-on". Then to run it, you would have to do npm run wait-on. You can also do "wo": "wait-on" and then do npm run wo basically meaning what comes after the run is the script entry. In node_modules, there is a .bin folder and inside of this folder is all the executables that you can access this way.
Installing locally makes the package available to the current project (where it stores all of the node modules in node_modules). This is usually only good for using a module like so var module = require('module'); or importing a module.
It will not be available as a command that the shell can resolve until you install it globally npm install -g module where npm will install it in a place where your path variable will resolve this command.
You can find a pretty decent explanation here.
It is also useful to put commands in the scripts block in package.json as it automatically resolve local commands. That means you could have a script that depended on a package without having an undocumented dependency on the same.
If you need to run it locally on cmd, you have to go inside the node_modules and run from the path.

Karma and Jasmine installation without npm

I want to use Karma and Jasmine to test my AngularJS application. All of the documentation I've found to install Karma and Jasmine involve using npm. I can't use npm because I am restricted, the reason doesn't matter. So far I have pulled Jasmine and Karma from Github using zip files. I want to add Karma and Jasmine to my project, but I don't think unzipping the entire contents of the respective GitHub repos is the way to go.
I'd like to know what I need to make Karma and Jasmine usable within my AngularJS project without using npm.
I guess it is possible, but will take a huuuuuge amount of work because of the dependencies. If you take a look at karma's repository, you can find a file package.json (here). In this file there is a property dependencies (link), which lists the modules karma depends on. So you'll have to find their sources, manually download all of them with respective version number and put in the folder called node_modules created in the karma module folder. But each of these modules karma depends on also has dependencies listed in their own package.json - you'll have to download them too keeping in mind version numbers and putting them in module's node_modules folder. And this dependency nesting can be really really deep.
Some modules may have extra scripts to be executed after they have been installed (scripts), which are called by NPM by default on installation. Maybe there are some other things which I am not aware of. Generally speaking it was designed to be installed via NPM and it's rarely the case when someone has no access to use it.
I would advise to ask somebody who has access to NPM to do an install of required packages and share the result of installation with you. Everything will be installed in the node_modules folder of the directory you run NPM commands from, it would be easy to do.
Here you can download version I've created, it has karma v0.13.1, karma-jasmine v0.3.6 and karma-chrome-launcher v0.2.0. I hope it will work for you, because we might have different OS (mine is Ubuntu 14.04 x64), I'm not sure if NPM does something OS-specific while installation of any package.
You should place the content of the archive to your project directory, to execute tests from your project folder use a terminal command:
./node_modules/karma/bin/karma start
I would still advise to solve the problem of accessing the NPM if you want to closely work with modules it stores.

How to use npm with node.exe?

I have downloaded Windows Binary (.exe) of nodejs from the main page.
How can I install and use npm (Node package manager)?
The current windows installer from nodejs.org as of v0.6.11 (2012-02-20) will install NPM along with NodeJS.
NOTES:
At this point, the 64-bit version is your best bet
The install path for 32-bit node is "Program Files (x86)" in 64-bit windows.
You may also need to add quotes to the path statement in environment variables, this only seems to be in some cases that I've seen.
In Windows, the global install path is actually in your user's profile directory
%USERPROFILE%\AppData\Roaming\npm
%USERPROFILE%\AppData\Roaming\npm-cache
WARNING: If you're doing timed events or other automation as a different user, make sure you run npm install as that user. Some modules/utilities should be installed globally.
INSTALLER BUGS: You may have to create these directories or add the ...\npm directory to your users path yourself.
To change the "global" location for all users to a more appropriate shared global location %ALLUSERSPROFILE%\(npm|npm-cache) (do this as an administrator):
create an [NODE_INSTALL_PATH]\etc\ directory
this is needed before you try npm config --global ... actions
create the global (admin) location(s) for npm modules
C:\ProgramData\npm-cache - npm modules will go here
C:\ProgramData\npm - binary scripts for globally installed modules will go here
C:\ProgramData\npm\node_modules - globally installed modules will go here
set the permissions appropriately
administrators: modify
authenticated users: read/execute
Set global configuration settings (Administrator Command Prompt)
npm config --global set prefix "C:\ProgramData\npm"
npm config --global set cache "C:\ProgramData\npm-cache"
Add C:\ProgramData\npm to your System's Path environment variable
If you want to change your user's "global" location to %LOCALAPPDATA%\(npm|npm-cache) path instead:
Create the necessary directories
C:\Users\YOURNAME\AppData\Local\npm-cache - npm modules will go here
C:\Users\YOURNAME\AppData\Local\npm - binary scripts for installed modules will go here
C:\Users\YOURNAME\AppData\Local\npm\node_modules - globally installed modules will go here
Configure npm
npm config set prefix "C:\Users\YOURNAME\AppData\Local\npm"
npm config set cache "C:\Users\YOURNAME\AppData\Local\npm-cache"
Add the new npm path to your environment's PATH.
setx PATH "%PATH%;C:\Users\YOURNAME\AppData\Local\npm"
For beginners, some of the npm modules I've made the most use of are as follows.
axios - for more complex http posts/gets
isomorphic-fetch - for http(s) post/get requests
node-mailer - smtp client
mssql - interface and driver library for querying MS SQL Server (wraps tedious)
More advanced JS options...
async/await - async functions, supported via babel
For testing, I reach for the following tools...
mocha - testing framework
chai - assertion library, I like chai.expect
sinon - spies and stubs and shims
sinon-chai - extend chai with sinon's assertion tools
babel-istanbul - coverage reports
jest - parallel testing, assertions, mocking, coverage reports in one tool
babel-plugin-rewire - slightly easier for some mocking conditions vs. jest
Web tooling.
webpack - module bundler, package node-style modules for browser usage
babel - convert modern JS (ES2015+) syntax for your deployment environment.
If you build it...
shelljs - shell utilities for node scripts,. I used to use gulp/grunt, but these days will have a scripts directory that's referenced in package.json scripts via npm. You can use gulp tools inside plain scripts.
When Node.js is not installed using the msi installer, npm needs to be setup manually.
setting up npm
First, let's say we have the node.exe file located in the folder c:\nodejs. Now to setup npm-
Download the latest npm release from GitHub (https://github.com/npm/npm/releases)
Create folders c:\nodejs\node_modules and c:\nodejs\node_modules\npm
Unzip the downloaded zip file in c:\nodejs\node_modules\npm folder
Copy npm and npm.cmd files from c:\nodejs\node_modules\npm\bin to c:\nodejs folder
In order to test npm, open cmd.exe change working directory to c:\nodejs and type npm --version. You will see the version of npm if it is setup correctly.
Once setup is done, it can be used to install/uninstall packages locally or globally. For more information on using npm visit https://docs.npmjs.com/.
As the final step you can add node's folder path c:\nodejs to the path environment variable so that you don't have to specify full path when running node.exe and npm at command prompt.
npm can be downloaded (without installation) from here:
http://nodejs.org/dist/npm/
https://github.com/npm/npm/releases
I just installed latest version of node (0.6.12) in Windows 7 using msi (node-v0.6.12.msi).
npm is already shipped with it, no need to include it separately.
I was facing permission issue while running npm (npm install mysql), from the path where my nodejs resided, i.e.
C:\Program Files (x86)\nodejs
Then I followed below steps:
1) Added C:\Program Files (x86)\nodejs\npm in environment variables - Path system variable.
2) went back to only C:\ in command prompt and gave the command - npm install mysql - and voila! it worked..
Hope this helps.
I am running node.js on Windows with npm.
The trick is simply use cygwin. I followed the howto under https://github.com/joyent/node/wiki/Building-node.js-on-Cygwin-(Windows) . But make sure that you use version 0.4.11 of nodejs or npm will fail!
I've just installed 64 bit Node.js v0.12.0 for Windows 8.1 from here.
It's about 8MB and since it's an MSI you just double click to launch. It will automatically set up your environment paths etc.
Then to get the command line it's just [Win-Key]+[S] for search and then enter "node.js" as your search phrase.
Choose the Node.js Command Prompt entry NOT the Node.js entry.
Both will given you a command prompt but only the former will actually work. npm is built into that download so then just npm -whatever at prompt.
Use a Windows Package manager like chocolatey. First install chocolatey as indicated on it's homepage. That should be a breeze
Then, to install Node JS (Install), run the following command from the command line or from PowerShell:
C:> cinst nodejs.install
Here is a guide by #CTS_AE on how to use NPM with standalone node.exe:
https://stackoverflow.com/a/31148216/228508
Download the node.exe stand-alone from nodejs.org
Grab an NPM release zip off of github https://github.com/npm/npm/releases
Create a folder named: node_modules in the same folder as node.exe
Extract the NPM zip into the node_modules folder
Rename the extracted npm folder to npm and remove any versioning ie: npm-3.3.4 –> npm.
Copy npm.cmd out of the /npm/bin/ folder into the root folder with node.exe
I just installed Node.js for the first time and it includes NPM, which can be ran from the Windows cmd. However, make sure that you run it as an administrator. Right click on cmd and choose "run as administrator". This allowed me to call npm commands.
Search all .npmrc file in your system.
Please verify that the path you have given is correct. If not please remove the incorrect path.

Proper files for socket.io 0.7?

I'm developing something with node.js and socket.io, but I'm doing my local dev on Windows for my own convenience. Installation instructions for socket.io say just do npm install socket.io. This is fine for my linux environment, and I'm guessing node will just find it in modules. But on Windows I don't know what to do. I got version 0.6 working fine somehow, managing to find the files I need.
Now, it looks like I need two sets of files, one for the server side and one for the client. There's also two repos on github, socket.io and socket.io-client. So I'm trying to just download all the files I need from there. The issue is that the server one refers to the client one, but the socket.io-client files aren't in the server repo. If I put the server files in, and reference them in my node server, it crashes on startup saying Cannot find module 'socket.io-client'.
tl;dr If I'm just copying files into my project directory, rather than doing an npm install, what is the proper file structure to get socket.io version 0.7 running?
Had the same issue here and I'm not using NPM either. But nothing to do with Windows: I'm on Ubuntu with the same prob.
You also need to have the socket.io-client module available in your node_modules path or wherever you keep the server-side socket.io module.
For solving similar issues I created a runner script that simply set the NODE_PATH env variable as needed and then execute my script. I also put my own modules (or the modules I don't want to install via npm) in the node_modules subdirectory of my project. A better explanation is here http://www.bennadel.com/blog/2169-Where-Does-Node-js-And-Require-Look-For-Modules-.htm
#!/bin/sh
export NODE_ENV=development
if [ "${NODE_PATH}" = "" ]; then
export NODE_PATH=$(npm -g root 2>/dev/null)
fi
node ${1}

Resources