npm install in a specific local folder - node.js

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

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.

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 resulting in 'ENOENT: no such file or directory'

I've installed Node.js for Windows and I'm trying to install a package via npm. The command prompt is in the directory of the project (C:\Users\username\Desktop\NodeTest), which contains a single helloworld.js file. Upon typing 'npm install express', I receive the following error:
ENOENT: no such file or direcotry, open 'C:\Users\username\package.json
I'm attempting this from a clean install and cmd is running as admin.
Any ideas?
I was facing the same issue. I firstly delete my node_modules and delete the cache by following command:
rm -rf node_modules && npm cache clean --force
then I delete the package-lock.json file from my project and then hit npm install in command prompt and it works.
As already pointed out by Subburaj this is because you are missing a package.json.
Just run npm init to initialize that file for you; afterwards it should work.
If you are working on a Windows machine using Vagrant/VM, there's a chance that symlinks are the culprit for your problem. To determine if that is the case, simply copy your package.json and package-lock.json into a test directory that is not mounted/shared between OSs.
mkdir /tmp/symlinktest
cd {{your directory with your package*.json}}
cp package*.json /tmp/symlinktest
cd /tmp/symlinktest
npm install
If this results in a successful install, you'll need to either exclude the node_modules directory from the mount (there's various articles on doing this, however I can't say I've had success) or run npm install outside the mounted volume.
I deleted the package-lock.json and It worked for me.
Basically I was Offline while I tried to install with npm, so go online and try
npm install again
Check the project folder which you opened in microsoft visual code. Generally you are not in the right path so npm is not able to search the package.json ... My project was in Document/hostel/hostel .. I opened Document/hostel ... So npm tried to find the package.json in Documents folder .. When i entered one level inside to Document/hostel/hostel .. it was fixed.

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

Making "sudo npm install --save-dev gulp" in Maverics nothing's happens

I'm trying to test Gulp and I find this problem.
I installed Gulp globally with: sudo npm install -g gulp.
Then I was created the folder: mkdir my-gulp
cd my-gulp
And the I try to do this: sudo npm install --save-dev gulp
It seems work fine, no errors, but my folder my-gulp continues empty.
I can see in console, this line:
gulp#3.8.6 ../../node_modules/gulp
And I thinkig the problem is here "../../", I think it must be "gulp#3.8.6 node_modules/gulp"
What do you think? Maybe is something about $PATH?
First, you should never run npm via sudo for local packages. (In fact, you shouldn't run it as root ever, but that's a different, more complex issue to solve.)
Second, you need to initialize NPM before you can install packages. It sounds like you didn't do this.
There's two ways:
If you have an existing package.json, which contains the required information about a project, you can copy it into your folder, then edit in a text editor of your choice to get started. If the package.json includes existing dependencies, you can either remove them, or run npm i in the folder to install them.
If you are starting fresh, run npm init to set up a new package.
Once you have a valid package.json, you can install packages into your application.
As to why you are seeing ../../gulp, I'm guessing you have a package.json further up the file tree somewhere.

Resources