How to use local package rather than remote package in NodeJs - node.js

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

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.

`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 )

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 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.

How to put local node package on path?

Newbie question. I have chosen not to install express with -g option. I did not use npm -g which would put it on the path globally. Instead it is installed in my local mac user directory. What I am not clear on is exactly what or how you put a package like express on the path so it can be invoked etc? What exactly needs to be on the path (node_modules?) so these packages are available just like a -g installation? I could have used home-brew I suppose but anyway, I now have all node packages and everything local. Another situation is that I am not able to run any of the nodejs tutorials. Although there might be smarter ways to do this, I wonder if sudo is really such a good way to install a development package ....
Now for example, I want to run the tutorial javascripting which is a nodejs tutorial. How do I do this. If I just type:
Mac1$ javascripting
it finds nothing.
Same for
Mac1$ express
UPDATE: THIS WAS ANSWERED IN THE COMMENTS
The commands exist in a hidden directory after a regular
install npm install express
in my case this the command goes here: /users/MAC1/node_modules/.bin
It is this path that needs to be placed on the $PATH as described in the first comment.
Thanks guys.
npm installes executable to two places. By default running a npm install in a project will install any binaries in ./node_modules/.bin. When you use the -g flag (npm install -g package-name) it will install into a global path. You can find out the global path by running npm bin -g. Add that to your path and globally installed executables will be accessible.
You can also add ./node_modules/.bin to your path to allow easy access to executables added by packages in your project folder. I admit to using this on a trusted local machine. However, this is very dangerous and not a recommended way to expose the executables in the node_modules directory.
Best alternative is to add the executable to the scripts section of the package.json file and then use npm run-script <command> which will auto prepend the ./node_modules/.bin when executing.
package.json
{
"scripts": {
"foo": "foo --arguments"
}
}
Example
$ npm install foo
$ ls ./node_modules/.bin
foo
$ npm run-script foo
# Executes:
./node_modules/.bin/foo --arguments

Resources