npm install not working with --prefix - node.js

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.

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?

Works for me
npm install --prefix ./server ./server

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.

SOLUTION
Those lines in package.json solves all my problems:
"scripts": {
"init": "npm i && mv ./node_modules ./server && sudo npm i typescript -g",
Strange but works...

This part of my server package.json and all working fine:
"scripts": {
"start": "node dist/app.js",
"server": "nodemon --exec ts-node src/app.ts",
"build": "tsc -p .",
"client": "npm start --prefix ../client",
"client:install": "npm install --prefix ../client",
"client:build": "npm run build --prefix ../client",
"dev": "concurrently \"npm run server\" \"npm run client\""
},

Try this one it will definitely work, I'm assuming your project root directory package.json also has dependencies.
npm install && npm install --prefix ./server && npm install --prefix ./client
or user this script
"scripts": {
"client-install": "npm install --prefix ./client",
"install-all": "npm install && npm run client-install && npm run server-install",
"server-install": "npm install --prefix ./server",
},

Related

How do I get SASS with auto-refresh?

When entering the command
npm install sass --watch ...
I get back,
npm ERR! enoent This is realated to npm not being able to find a file.
Though, the file is there and everything is spelled correctly.
Can anyone help?
Hey John take a look at this reply. Basically there are different solutions:
Single Ampersand solution
Adding to your package.json the following:
"dev:watch" : "npm run sass:watch & npm run livereload"
Parallelshell solution
Using Parallelshell and adding to your package.json the following:
"serve": "live-server",
"start": "parallelshell \"npm run scss && npm run scss -- -w\" \"npm run serve\""
Concurrently solution
Using Concurrently. Install it npm install concurrently --save-dev and add the script:
"dev:watch": "concurrently \" npm run sass:watch \" \" npm run livereload \" "

who does npm install of webpack-dev-server require '-g'?

Trying to run "webpack-dev-server --open" from VSCode bash terminal. Error: webpack-dev-server not found
Why do I have to do the install with '-g'? If I install local, I get the not found error. In this case, do I have to modify the path to pick up the local install?
npm install webpack-dev-server -g
When you install webpack-dev-server locally in your project, you can run it with npx:
npx webpack-dev-server --open
Or:
./node_modules/.bin/webpack-dev-server --open
You can also add a script in package.json:
scripts: {
dev: "webpack-dev-server --open"
}
Then run npm run dev.
There's no need to install it globally, which is not recommended.

NPM run build with React + Node + concurrently How to?

I have spend hours trying to figure this out any advice is welcome. The objective here is to assemble a postbuild script that will work on a nodeJS app running a react client.
React is on post 3000 and node is on 5000. So it requires the concurrently library. Below are two attempts do-postbuild and heroku-postbuild (both fail).
"scripts": {
"server": "nodemon server.js --ignore client",
"client": "npm start --prefix ../client",
"dev": "concurrently \"npm run server\" \"npm run client\" ",
"do-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix && npm run build --prefix client",
"heroku-postbuild": "cd ../client && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
},
folder structure
client
server
|_package.json (above)
|_server.js
npm run dev - WORKS perfectly
When I attempt npm run heroku-postbuild it yields the following:
npm ERR! errno 1
npm ERR! ver1.02#1.0.0 heroku-postbuild: `cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build`
npm ERR! Exit status 1
When attempting to write npm run do-postbuild it throws an error like it is searching for client in the server folder
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/Users/sites/server/client/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
This is not a HEROKU solution it is for general UBUNTU server with root access.
Solution here is you don't have to dockerize the app as a bundle (client and server together).
What worked for me was to treat the client and server as two different apps.
Client side:
npm run build locally from the same folder as contains your package.json file
then post the app build folder as very straight forward client side app with HTML CSS Javascript
Server side:
upload the server files (not including node_modules folder)
run npm i(from the folder with the package.json file)
I set up reverse proxy to map the port to a specific location on the server for the react to reach it
set up cron job to start server side (and check periodically to ensure it is running)
Thats it - works perfect.
Please add below Scripts and try, it will work 100%.
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
}
Add this in your scripts
"client-install": "npm install --prefix client",

Node & Yarn Command Means

I am trying to found
npm run install:global
npm install -g win-node-env
yarn && npm run dev:build && npm run dev:serve
What does this three command means ?
I tried to search google but not got relative info.
Without any extra info...
1:
npm run runs the next argument defined in the "scripts" property of your package.json file. So, npm run install:global will execute whatever the corresponding script entry by that name. If your package.json file has this entry (and it should):
package.json:
"scripts": {
"install:global": "npm install -g whatever-the-script-is"
}
2:
npm install -g win-node-env will globally install the win-node-env package, so that it's accessible from a terminal session by typing whatever win-node-env exposes.
3:
yarn && npm run dev:build && npm run dev:serve
Yarn is an alternative for npm. This command would run yarn, then the script in package.json that correspond to dev:build and immediately after run dev:serve.
npm run install:global
Run specified npm script
npm install -g win-node-env
Install package globally
yarn && npm run dev:build && npm run dev:serve
same as
npm install && npm run dev:build && npm run dev:serve
Install local packages and run script
This is how package.json might look like
{
"name": "demo"
"scripts": {
"install:global": "npm install -g my-unrealistic-package"
"dev:build": "echo \"build something\"",
"dev:serve": "echo \"serve something\""
}
}

Script for initial build of Angular 2 project?

How can I write a script for npm that installs the node_modules in an angular 2 project and compiles the ts files. This doesn't work:
"scripts": {
"firstBuild": "npm install && tsc",
....
I get the error:
> npm firstBuild
Usage: npm <command>
where <command> is one of:
PS:
I can run "start" : "tsc && concurrently \"tsc -w\" \"lite-server\" just like npm start , why can't I do npm firstBuild ?
I think you just forgot to use run.
Try npm run firstBuild.

Resources