Where does meteor install npm packages referenced in packages.json? - node.js

After meteor installs npm packages:
npm: updating npm dependencies -- winston...
Npm.require results in module not found error, by inspecting the code and debugging via node-inspector, I discovered that putting a standard node_modules folder (installed via npm install) in .meteor folder in the root of my meteor app folder gets it to find it.
However when deploying to meteor.com or using any automated build-pack (like Heroku's) this might not be possible, I would rather have a scenario where I can link the automatically downloaded modules to the paths Npm.require looks in.
Any idea where it downloads the packages?
Thanks

For meteorite packages this would be:
/path/to/your/project/packages/package_name/.npm/package/node_modules/
For core meteor packages I guess this is
~/.meteor/packages/package_name/hash/npm/node_modules/

Related

How much damage can I do by installing a module in the wrong place with npm?

I've got an iOS app, using google cloud functions and want to install the Request module/package https://www.npmjs.com/package/request in the node_modules folder.
My folder structure is:
- Desktop
- myApp
- firebase
- functions
- node_modules
I was reading up on npm about npm install and there was some stuff about installing locally, globally and some other things I didn't quite get.
Is there a standard way to install packages with npm?
And if I wanted to install "Request" in the terminal do i "cd" to the node_modules folder and npm install from there or is it from within the functions folder?
Thanks.
In the same directory where you have the node_module directory (i.e. in your functions directory) you should also have package.json file. If you are in that directory and you run npm install request --save then you will install the module and put it in the dependencies in package.json file. That is what installing locally means. For more info see:
https://cloud.google.com/functions/docs/writing/dependencies
(Cloud functions for Firebase work pretty much the same as Google Cloud Functions)
Update
The --save is the default behavior of npm install since v5.0 so you don't need to use the --save flag since the saving is done automatically (there is --no-save to prevent saving).
See the release notes for more info:
https://github.com/npm/npm/releases/tag/v5.0.0

NPM on OSX, error getting global installed packages

I have nodejs installed on a MBP which runs OSX 10.9, I have installed as a package downloaded from the nodejs website. Then I have installed the MEAN stack following instructions on mean.io.
The commands are:
sudo npm install -g mean-cli
mean init yourNewApp
That works correctly
Now the real issue is when after my app is created I enter the dir using the terminal, and write gulp, and it thows me some errors that some mandatory modules are not found.
The modules are written in the package.json file that mean generated, and they are installed as global modules on ~/.npm
I browsed the folder and there are all the required packages folders, inside the folders there is a package.tgz file which has the code of the package and a package folder which holds a package.json file describing the package itself.
Now I don't understand why the packages are compressed and why if they are installed globaly can not be accessed from gulp on my project folder.
Thanks in advance.
If you install some global module then you better don't put it into the package.json of your app because when you run your app that's the first place where is going to search and if it is there your app is going to look at node_modules folder and if it is not there your app will crash.
My advice is try to install your modules inside your app, npm install your_module --save because your app is gonna be portable and with a simple npm install you will be able to install all your needed packages.
But if you still wanna install global packages you maybe wanna follow this rules:
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.
If you have the time to read the link then you will see that there are exceptions and how to handle them.

Using npm how can I download a package as a zip with all of its dependencies included in the package

What I'm trying to do is download packages with all their dependencies, in order to transfer them to another computer that does not have an internet connection and install it there.
So the scenario would be:
Download package (to zip/tarball/whatever file) without installing it.
Included in that downloaded file would be all of its dependencies (correct versions, and it's dependencies' dependencies).
Transfer file to other computer.
Run npm install to file location (optional -g important).
Package is installed with dependencies.
Happy camper.
I feel like there has to be a npm command to download and pack (create) files this way.
I've tried looking for a solution for this to no avail.
This is my first time using node so I'm affraid I'm not researching it correctly because lack of knowledge of the node/npm lingo.
I just used this gist by Jack Gill to do exactly what you describe -- bundle up a package, with all its dependencies. Basically what the script does is re-write a module's package.json file to move all its dependencies to bundleDependencies, then pack the whole thing. Upload the resulting tarball to your server, then npm install it. Works a treat.
Download the package to a machine with internet.
Make sure your app package has a package.json file at its root with all of your dependencies listed in it. You can make npm save your dependencies in package.json by doing npm install dependency-name --save. The --save flag will cause npm to write the dependency to your app's package.json file if it has one. If it doesn't have on then it will do nothing. You can also instruct npm to create a package.json file for your app if you need to by simply running npm init from in your app's directory.
Run npm install from inside the app's directory. This will create the node_modules directory and install all the dependencies listed in the app's package.json file.
Zip up the directory now that it has a node_modules directory in it with all your dependencies installed. Transfer the zip archive to another machine.
Simply unpack the archive in its final destination and you're done. The app is now where it needs to be and the dependencies are already installed.
Now just run the application with node app.js, replacing "app.js" with whatever the name of the app's main entry point file is.
You can just use the npm pack command.
So for example:
npm pack lodash
This command will download the npm package and create a file lodash-4.17.4.tgz.
Installing this can be done with:
npm install ../../my-location/lodash-4.17.4.tgz
More details here:
https://docs.npmjs.com/cli/v8/commands/npm-pack
Simply run npm install in the package directory and archive the entirety of it.
Assuming there are no non-npm requirements you need to meet and both machines are running the same version of node, nothing more needs to be done. All of the downloaded dependencies will be installed inside the ./node_modules. But it is a generally good idea to archive the entire package, as the developer might have implemented some additional setup routines.
you can download package with all its dependencies with its dependents using single command. Kindly refer this link npm-package-downloader

Locally installed versus globally installed NPM modules

In my package.json file, I have bower listed as a dependency. After I run npm install, bower gets installed locally. When I try to run bower after installing it locally I get an error
"bower" is not recognized as an internal or external command
It seems the only way to resolve this is to install bower globally. Why should I have to do this? If my project contains a local copy of bower, why won't node use it?
Installing locally makes bower 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'); 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.
Edit: This documentation explains it pretty thorougly.
You can execute your local instance by typing the line below in cmd:
node_modules/bower/bin/bower <bower args>
We use both PHP and JavaScript, so we have composer and npm.
Each of the projects we work on have different packages both for runtime of the package as well as build/dev tools.
As there are version constraints in each project, installing version x of a package globally (that would be run from the command line), would cause us issues, we install all the tooling in each package. Much easier to define in the appropriate composer.json / package.json files.
But running the CLI tools is a pain if you have to constantly add an additional path to the command.
To that end, we have recommend to the team that the following paths are added to your $PATH in the appropriate .bashrc (or equivalent):
./vendor/bin:./node_modules/.bin
(EDIT: For Windows, the paths would be .\vendor\bin;.\node_modules\.bin;)
So, whilst in project X, we have access to the CLI tools for that project. Switch to project Y, and we get that projects tools.
Sure, you are going to get duplications, but each project is maintained by different teams (and some people are in multiple teams), so again, having 1 version in the global setup is an issue there.
Usually you install NPM modules globally if you want them included in your path to be ran from the command line. Since it is installed locally you will have to run it from the node_modules folder.

How do you include npm libraries in a node.js app

I'm trying to create a little app with node-ncurses which I installed over npm install ncurses
with this library install i'm trying to run the examples for node-ncurses from the following
https://github.com/mscdex/node-ncurses/tree/master/examples
But I get path errors with the examples for require('ncurses'), what is wrong?
My ncurses library is install into ~/.npm/ which seems correct to me.
You want to be installing them locally into the same folder of your project. If you miss out the -g flag and just run npm install ncurses within your project directory, you should then be able to run require("ncurses") just fine. All NPM modules installed locally goes into a node_modules folder within your project.
A little further hint, if you install with:
npm install ncurses --save
That will add ncurses to your package.json as a dependency, which means any other dev who might check out your project can run npm install in the project's directory and automatically get ncurses installed as it's listed in package.json as a dependency.
Without the -g flag anything you install with NPM with install into a node_modules folder relative to where you ran the command.
My first recommendation would be to make sure that you are in the working directory of your project and then install ncurses again.
Here is an old, but relevant blog post about how it was designed.

Resources