Install global dependency manualy - node.js

I'm working on a project which is gonna be deployed on a VM(Windows OS) but I can't download dependencies because all npm ports are blocked and there is no way to open them.
So the only way to solve this is to zip all local dependencies and then copy them to the VM. This is pretty simple however I use two global dependencies: PM2 and pm2-windows-service.
My question is how to copy these two dependencies to the VM and then make them global ?

You can install global dependencies locally and use them from the node_modules path for example:
node node_modules/.bin/pm2 start app.js
instead of
pm2 start app.js
Actually I tend to recommend using the minimal amount of global dependencies ie. only npm

From npm-install:
npm install -g <tarball file>
And:
A package is:
a) a folder containing a program described by a package.json file
b) a gzipped tarball containing (a)
...
And also:
npm install <tarball file>:
Install a package that is sitting on the filesystem. Note: if you just
want to link a dev directory into your npm root, you can do this more
easily by using npm link.
Tarball requirements:
The filename must use .tar, .tar.gz, or .tgz as the extension.
The package contents should reside in a subfolder inside the tarball (usually it is called package/). npm strips one directory
layer when installing the package (an equivalent of tar x
--strip-components=1 is run).
The package must contain a package.json file with name and version properties.
Example:
npm install ./package.tgz
So just copy both (packed) packages and run the above command inside your VM, like npm i -g /pm2.tar.

Related

NodeJS - npm install practice

Created new folder and did npm install serve in it.
It created package-lock.json and node_modules/ folder.
When I run in the same folder serve it shows error:
command not found: serve
What is the way to install?
I am using: npm#6.5.0
My dev environment is MACOS
I read a great many pages on this topic and nothing worked until I tried the following
./node_modules/.bin/serve -s build
Also if you are using VS CODE you may want to bring up the terminal window outside of VS CODE - this seems to have snared a lot of people.
First of all, you should start your project running
npm init
This will create the package.json file.
Then, you can install the serve package globally.
npm install -g serve
And now you can run serve.
The serve binary was not found because the operating system cannot locate it in the PATH environment variable.
When you do the npm install serve command. The serve module is only installed into the node_modules directory found under the the project folder. Unless you explicitly include the absolute path of this node_module directory as part of your PATH env var, the OS won't know where to find serve.
Like others say, the typical practise would be to install the module using the -g flag. G means global.
When -g is used, npm will put the binary in its node directory somewhere and this this directory would have been included as part of your PATH when you install node, thus making the any new binary discoverable.
If the node.js module has a "command" and you want to run it without installing the module globally(npm install -g serve). You can run it like ./node-modules/.bin/command from the root folder of the project.
Now, what is generally used is npx, so that you can from within a project easily run any of the binaries within its local node_modules or your system's global node_modules/ and any other commands on the $PATH.
For example, here we install webpack as a local dependency. You can image doing this in a folder after running npm init. Then we run webpack without having to worry about referencing the bin file:
$ npm i -D webpack
$ npx webpack

npm install in a specific local folder

I'm currently trying to execute the command npm install to get all dependencies and modules necessary to run my package.json.
The problem is that I don't have Internet access to fetch from the internet, so I have downloaded the node_modules on a different PC and copy-paste it on my local folder that contains all. If I tried to run npm install without arguments, it's still trying to fetch from internet and fails.
I have read their documentation and apparently they have listed few npm install that takes different arguments, but still, I'm unable to install from the folder already downloaded.
I've tried to do npm install node_modules on the path that contains the package.json, but nothing. I'm running on windows 7.
If someone has an approach to specifying the local node_modules and just install all modules inside, I'll appreciate.
Thanks!
You probably should use npm-link...
From the docs:
Go to the node_modules directory, and, inside each package, run npm-link:
$ cd node_modules
$ cd package-name
$ npm link
$ cd ..
...
In the directory of your project which needs the local modules:
$ npm link package-name

Install NodeJS package locally

When i try to install package on my local directory using npm install connect,but it just keep pop up some warning about
no such file or directory, open '/Users/felixfong/package.json'
But i don't not want to install package at my computer directory,i want to install at my local web app directory
Are you sure you are inside your local web app directory when you run the npm install connect command?
cd app-directory/
npm install connect
Also ensure that a package.json file is also present in the app-directory.
If it isn't present, you can use npm init command to create the package.json file interactively.
You have to go inside your project directory using
Then you can check package.json.
If package.json file is not there then initialize npm using the following command:
npm init
Then your can install the package using the following command:
npm install connect
'npm install connect' does not save the connect npm package in package.json file.
For saving the package into package.json file you have to givt --save option like:
npm install connect --save
Make sure that you are in web app's directory. Current path can be checked via command pwd in Linux and cd in windows. Navigate to your web app directory if you are somewhere else. Check existence of package.json by listing the content of the folder. ls and dir can be used for ubuntu and windows respectively for listing content. Commands for ubuntu are as below:
pwd
cd your-path/
ls
Now Initialize npm in your web app directory if package.json is not already existing there.
npm init
This will ask some information like:
name of the app,
its version,
description,
entry point,
test command,
git repo,
keywords,
author and
license (if any)
You can select default values by leaving the fields empty if you aren't sure or confused about any field. Combining this information as json, npm will create a file named package.json
Just run the desired command now after initialization of npm or if its already initialized:
npm install connect

Add local project dependency for npm install

What is the proper syntax to add local project dependency in npm package.json file?
I have git project locally in C:\projects\MyApp
I want to get this project with npm install. I tried following
"dependencies": {
.....
"my-app": "file://../projects/MyApp/MyApp.git"
.....
}
but getting error
Could not install ....
Any suggestion?
Finally got it working
"my-app": "../projects/MyApp"
Its' simple until you know.
Local dependency has to be a directory on your filesystem.
Alternately there is npm-link.
Excerpt from the docs:
Package linking is a two-step process.
First, npm link in a package folder will create a globally-installed
symbolic link from prefix/package-name to the current folder (see
npm-config for the value of prefix).
Next, in some other location, npm link package-name will create a
symlink from the local node_modules folder to the global symlink.
Example:
cd ~/projects/node-redis # go into the package directory
npm link # creates global link
cd ~/projects/node-bloggy # go into some other package directory.
npm link redis # link-install the package
File is the wrong protocol. You can use git+ssh or git+https.
Here can you find more information about your question:
https://stackoverflow.com/a/10391718/5111420
and I see a typo: dependencioes -> dependencies

How to npm install to a specified directory?

Is it possible to specify a target directory when running npm install <package>?
You can use the --prefix option:
mkdir -p ./install/here/node_modules
npm install --prefix ./install/here <package>
The package(s) will then be installed in ./install/here/node_modules. The mkdir is needed since npm might otherwise choose an already existing node_modules directory higher up in the hierarchy. (See npm documentation on folders.)
As of npm version 3.8.6, you can use
npm install --prefix ./install/here <package>
to install in the specified directory. NPM automatically creates node_modules folder even when a node_modules directory already exists in the higher up hierarchy.
You can also have a package.json in the current directory and then install it in the specified directory using --prefix option:
npm install --prefix ./install/here
As of npm 6.0.0, you can use
npm install --prefix ./install/here ./
to install the package.json in current directory to "./install/here" directory. There is one thing that I have noticed on Mac that it creates a symlink to parent folder inside the node_modules directory. But, it still works.
NOTE: NPM honours the path that you've specified through the --prefix option. It resolves as per npm documentation on folders, only when npm install is used without the --prefix option.
In the documentation it's stated:
Use the prefix option together with the global option:
The prefix config defaults to the location where node is installed. On
most systems, this is /usr/local. On windows, this is the exact
location of the node.exe binary. On Unix systems, it's one level up,
since node is typically installed at {prefix}/bin/node rather than
{prefix}/node.exe.
When the global flag is set, npm installs things into this prefix.
When it is not set, it uses the root of the current package, or the
current working directory if not in a package already.
(Emphasis by them)
So in your root directory you could install with
npm install --prefix <path/to/prefix_folder> -g
and it will install the node_modules folder into the folder
<path/to/prefix_folder>/lib/node_modules
There currently is no documented way to install a package in an arbitrary directory. You should change into the target directory, make sure it has a package.json, and then use the regular commands.
While there currently is an undocumented option called --prefix, this feature might be removed in a future release. At least, it it is not planned to ever document this as an official feature.
I am using a powershell build and couldn't get npm to run without changing the current directory.
Ended up using the start command and just specifying the working directory:
start "npm" -ArgumentList "install --warn" -wo $buildFolder

Resources