Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm install not working with --prefix

Tags:

node.js

npm

It seems that npm install --prefix ./server (with no args) is not working with --prefix flag. I just want to install all packages from package.json. All I get after that command is:

npm WARN enoent ENOENT: no such file or directory, open '/home/.../ProjectName/server/package.json'

All is fine when I put npm install package_name -S --prefix ./server for example. Then NPM will create node_modules in server and will add package_name package.

My files structure is:

ProjectName
|
+-- client
|   +-- node_modules
|   +-- package.json
+-- server
|   +-- node_modules
+-- package.json

"Main" package.json contains all scripts (for Heroku and for me) and dependiencies for server. client is Angular2 app that's why it has own node_modules and package.json.

I use NPM 4.2.0. With version 5.0.3 (newest?) it seems that --prefix flag is not working at all.

EDIT #1

I've just discovered that I can solve my problem with npm install (which will install node_modules in my project folder) and then copy node_modules to server/node_modules. Without that copy jasmine throws errors during tsc build.

Now I have to have node_modules in main catalog and copy of them in server. That's so odd..

EDIT #2

According to @Damian Lattenero answer:

npm --prefix ./server install ./ProjectName/package.json

or

npm --prefix ProjectName/server install ./ProjectName/package.json

IS NOT WORKING and generates:

npm ERR! code ENOLOCAL npm ERR! Could not install "RecursiveNotebook3/package.json" as it is not a directory and is not a file with a name ending in .tgz, .tar.gz or .tar

THIS WORKS:

npm --prefix ProjectName/server install ./ProjectName

but generates:

npm WARN saveError ENOENT: no such file or directory, open '/home/tb/Projects/RecursiveNotebook3/server/package.json' npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN enoent ENOENT: no such file or directory, open '/home/ tb/Projects/RecursiveNotebook3/server/package.json'

and

package-lock.json next to node_modules

and

empty etc catalog next to node_modules

and

There are some problems with build (tsc -p server) with mongodb package.

like image 801
tBlabs Avatar asked Jun 09 '17 22:06

tBlabs


People also ask

What does npm install -- prefix do?

Normally, when we run an npm install package-name command, npm will install the packages in a default directory (node_modules folder). To install the packages into a specified directory, we need to use the --prefix option followed by the directory path.

Why npm install is not working?

The Npm command not found error can appear when you install or upgrade npm. On Windows, the cause of this error could be that a PATH or system variable is not correctly set. The error can also occur if you do not have npm or Node. js installed, have an outdated version, or have permission issues.

What is prefix in npm?

Description. Print the local prefix to standard output. This is the closest parent directory to contain a package. json file or node_modules directory, unless -g is also specified. If -g is specified, this will be the value of the global prefix.


3 Answers

Try:

npm --prefix ./server install ./ProjectName/package.json

or

npm install --prefix ./server ./ProjectName/package.json

Also, to understand better what the --prefix do, you can check this two answers:

How to npm install to a specified directory?

npm - install dependencies for a package in a different folder?

like image 98
A Monad is a Monoid Avatar answered Sep 25 '22 20:09

A Monad is a Monoid


Works for me

npm install --prefix ./server ./server
like image 41
Israel Avatar answered Sep 21 '22 20:09

Israel


Running the newest version of Ubuntu (Ubuntu 16.04.2 LTS), I encountered the same problem with npm install. I also got an ENOENT error, indicating that npm cannot find the necessary files.

When I installed nodejs-legacy, as shown here under:

sudo apt-get install nodejs-legacy

npm subsequently compiled fine, and my Angular application deployed as it should.

like image 26
Flying Dutchman Avatar answered Sep 23 '22 20:09

Flying Dutchman