Add local project dependency for npm install - node.js

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

Related

Install global dependency manualy

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.

How to use local package rather than remote package in NodeJs

We have a package from private npm package store. Now we have source code of it and want to use the source code as local package to test something before pushing to repo in dev machine. How can I achieve it?
use npm link https://docs.npmjs.com/cli/link
Example from the docs
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
Note that package-name is taken from package.json, not from directory name.
Simply put it in a folder like __server-root/lib/<module-name> and require that folder instead of the npm module.
following up the answer of Enslev your require could look something like this:
const yourPrivateModule = require('./lib/<module-name>/index.js');
npm install /path/to/local/package
This has been answered before

`npm link` works even when I do not `npm link <package_name>` in the desired directory

I read the npm documentation on npmjs.com for linking local packages and it says I need to do it in two steps:
Navigate to the local package dir and run npm link
Navigate to the package in which I intened to use this package and run npm link <package_name>
For example, if I want to use packageB as a dependency in packageA, I need to go to packageB's directory, run npm link, then do a npm link packageB in packageA's directory.
However, in practice, when I just to npm link in packageB's directory and then require('packageB') inside packageA, it works, and any changes in packageB are instantly reflected in packageA.
Can anyone tell me how this is happening?
In fact when you do 'npm link', it create a link to you package globally on your system (you know it's something like when you do a "npm install -g xxx").
And when you require a package via Node it checks at many directory (the current node_modules, the parent ... & the global directory )

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

How can I commit changes that I've made in node_modules back to git?

Sometimes it's easier to maintain a fork of a node package for your module, I'd like to be able to edit a module that's in node_modues that I've installed via npm install githubaccount/myrepo.git.
Currently any changed that I made to a file I have to copy back to the repo. This is tedious.
How can I edit modules in npm and have them tracked by Git?
Python's pip has an option where you can define an 'egg' which symlinks a clone of the repo to the site_packages folder. I know it's a different system, but I've found it really quite useful in the past.
You can use the npm link command to do this. Here's how to use that. Let's assume that your npm module is located in ~/npm-module and your project using the npm module is in ~/my-project.
cd ~
# First clone the npm module
git clone https://..../npm-module.git
# Go into the module's directory:
cd npm-module
# Link the module
npm link
# CD into the project using the npm module
cd ../my-project
# Link the module
npm link npm-module
The first call to npm link will create a link in npm's cache directory pointing to your local clone of the npm-module. The second call will link it into the project that's using the module (into the node_modules folder).
If you take a look at your project's node_modules folder, you will see that it now contains a symbolic link to the npm-module's sources. Any changes you make in that folder will also be in the cloned project. You can commit changes there and push them back to your remote.
This is similar to what you do with Python's egg feature.
Here's the npm link documentation.

Resources