npm: start script fails - node.js

I'm developing a simple backend project with Node.js (v12.16.1) on Windows 10.
My package.json file contains the following command:
"scripts": {
"start": "NODE_ENV=production node --experimental-modules --require dotenv/config server.mjs dotenv_config_path=config/config.env",
...
}
And when I type npm run start, I get
> NODE_ENV=production node --experimental-modules --require dotenv/config server.mjs dotenv_config_path=config/config.env
'NODE_ENV' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! devcamper-api#1.0.0 start: `NODE_ENV=production node --experimental-modules --require dotenv/config server.mjs dotenv_config_path=config/config.env`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the devcamper-api#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Why does this failure occure?

Windows doesn't understand this way of setting environment variables.
Use cross-env package instead: https://www.npmjs.com/package/cross-env
npm install --save-dev cross-env
and then in your script:
"cross-env NODE_ENV=production ... "

Related

Posting to cyclic and it's asking for package.json to be in the root?

package.json not found at root level of repository.
Cyclic runs scripts defined "scripts" section to build, test and run your app.
But my project is set up like:
-final
--backend
---package.json(including npm start)
--frontend
---package.json(including npm start)
-package.json
^ includes:
"scripts": {
"frontend": "cd frontend; npm start",
"backend": "cd backend; npm start"
How do I merge these packages together and put them into the root directory so I can make this react application live?
I was thinking about something like this:
"scripts": {
"frontend": "cd frontend; npm start",
"backend": "cd backend; npm start"
but when I go to the root directory (final)
npm ERR! Missing script: "start"
npm ERR!
npm ERR! Did you mean one of these?
npm ERR! npm star # Mark your favorite packages
npm ERR! npm stars # View packages marked as favorites
npm ERR!
npm ERR! To see a list of scripts, run:
npm ERR! npm run

How do I run my nodejs application with nodemon?

Problem
Sorry in advance for asking a newbie question but I do hope anyone can help because I've been stuck for a few hours. I am currently working on a student portal assignment to develop a Backend API that reads from csv and stores into the database.
I'm having a very basic trouble of running or starting the Node Application.
Project Directory
Assignment
- .vscode
- external
- config
- controllers
- services
- app.js
- external-system.Dockerfile
- package.json
- packagelock.json
- router.js
- server.js (localhost:5000)
- javascript
- src
- config
- const
- controllers
- errors
- utils
- app.js
- router.js
- server.js (localhost:3000)
- database
- node_modules
- .babelrc
- .env
- .env.sample
- .eslintrc
- docker-compose.yml
- package.json
- packagelog.json
- file.csv
Anyway in short, the javascript folder is where I will have to retrieve the csv file, create the API and storage to the database for the full time students. The external folder is where I have to retrieve the information it and because they are not full time students. (FYI: The directory is given and fixed)
What I've tried
I tried an npm start to the javascript folder but I have the following error when trying to run localhost:3000.
> student-portal-system#1.0.0 prestart C:\Users\User\Desktop\Assignment\javascript
> npm run start:services && npm run build
> student-portal-system#1.0.0 start:services C:\Users\User\Desktop\Assignment\javascript
> docker-compose up -d
'docker-compose' is not recognized as an internal or external command, operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! student-portal-system#1.0.0 start:services: `docker-compose up -d`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the student-portal-system#1.0.0 start:services script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\User\AppData\Roaming\npm-cache\_logs\2020-09-13T15_27_31_396Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! student-portal-system#1.0.0 prestart: `npm run start:services && npm run build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the student-portal-system#1.0.0 prestart script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
In javascript directory(package.json)
{
...
"scripts": {
"test": "jest",
"build": "babel src --out-dir build",
"prestart": "npm run start:services && npm run build",
"start": "node ./build/server.js",
"prestart:dev": "npm run start:services ",
"start:dev": "nodemon --exec babel-node src/server.js",
"start:services": "docker-compose up -d",
"lint": "eslint \"src/**/*.js\""
and so on...
In external directory(package.json)
{
...
"scripts": {
"test": "jest",
"build": "babel src --out-dir build",
"prestart": "npm run build",
"start": "node ./build/server.js",
"start:dev": "nodemon --exec babel-node src/server.js",
"lint": "eslint \"src/**/*.js\""
... and so on ...
Would be great anyone could give some tips on how to resolve the errors or correct me whether I am running the application correctly. Appreciate it!
Seems like you do not have docker installed on your windows.
Check this https://docs.docker.com/docker-for-windows
But I am not sure that if you install just docker, the docker-compose will be also installed, in that case
you would need also install https://docs.docker.com/compose/install/ there is a Tab for installing it on windows.
Also I am not sure if you have a typo in docker-compose.yml file, I see that there is
docker.compose.yml filename which is wrong.

Node app on Plesk - Environment variables on custom scripts

I'm running a Node v12 app on Plesk Obsidian v18.0.29
I've set the required environment variables in the dedicated section:
Also the "Application Mode" should in theory act as NODE_ENV for the process and seems to do so.
Just FYI I also tried adding it to the "Custom environment variables" but that doesn't solve my problem.
The problem being that, despite the app itself running fine, when I try to run a custom script (DB migrations) Plesk seems to ignore these variables, as you can see from Using environment "development".
> REDACTED#0.1.0 migrate /var/www/vhosts/REDACTED/httpdocs
> sequelize db:migrate
[4mSequelize CLI [Node: 12.4.0, CLI: 5.4.0, ORM: 5.8.7][24m
Loaded configuration file "config/database.js".
Using environment "development".
[31mERROR:[39m Error parsing url: undefined
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! REDACTED#0.1.0 migrate: `sequelize db:migrate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the REDACTED#0.1.0 migrate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /var/www/vhosts/REDACTED/.npm/_logs/2020-09-04T08_13_39_221Z-debug.log
If I try to create a custom script that forces the NODE_ENV variable, the correct environment is considered, but the other variables are still ignored.
For example, by creating a script called migrateprod and running it through Plesk:
"scripts": {
"test": "npm run test:unit && npm run test:e2e",
"test:unit": "mocha tests/unit/**/**.spec.js --require ./tests/setup.js --exit",
"test:e2e": "mocha tests/e2e/**/**.spec.js --require ./tests/setup.js --exit",
"start": "node index.js",
"watch": "nodemon index.js",
"migrate": "sequelize db:migrate",
"migrateprod": "NODE_ENV=production sequelize db:migrate",
"migrate:down": "sequelize db:migrate:undo:all",
"cli": "node cli/index.js"
},
I can now see the environment production but the DATABASE_URL variable is not found, getting an error instead:
> REDACTED#0.1.0 migrateprod /var/www/vhosts/REDACTED/httpdocs
> NODE_ENV=production sequelize db:migrate
[4mSequelize CLI [Node: 12.4.0, CLI: 5.4.0, ORM: 5.8.7][24m
Loaded configuration file "config/database.js".
Using environment "production".
[31mERROR:[39m Error parsing url: undefined
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! REDACTED#0.1.0 migrateprod: `NODE_ENV=production sequelize db:migrate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the REDACTED#0.1.0 migrateprod script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /var/www/vhosts/REDACTED/.npm/_logs/2020-09-04T08_21_51_188Z-debug.log
Bottom line is, Plesk seems to ignore or avoid setting the environment variables while running custom scripts. I tried searching around but I couldn't find any similar case, which makes me think I'm just missing something.
Any ideas? Thanks in advance for your time!
Turns out Plesk does not forward environment variables to custom scripts, no matter how hard you try. So I modified the package.json to set the variables on the fly before running the script.
"scripts": {
... other scripts ...
"cli": "export $(cat ../.env.production | xargs) && node cli/index.js"
},
There's an .env file in a protected folder outside the application file system, from which I get the variables and send them as args to the custom script.
Not the most elegant solution, but it sorts out the problem.

working node.js projects on windows 10 now no longer working

Node.js projects are no longer working. Have node.js 7.10.x npm 4.2.1
Projects that were working are no longer working. I uninstalled node.js, removed npm-cache, reinstalled fresh. Deleted node_modules directory, ran npm install on project's package.json and npm start xxxx etc. No matter what project I run, the error remains the same as shown below. The same projects installed on Linux Ubuntu work just fine.
No clue as to why these projects are no longer working. The following error is consistent with each node project I've tested.
Any help would be appreciated. Thanks in advance.
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "test"
npm ERR! node v7.10.1
npm ERR! npm v4.2.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! muber#1.0.0 test: `NODE_ENV=test nodemon --exec 'mocha --recursive -R min'`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the muber#1.0.0 test script 'NODE_ENV=test nodemon --exec 'mocha --recursive -R min''.
npm ERR! Make sure you have the latest version of node.js and npm installed.
I think I know what the problem is if it works on Linux but not the Windows machine. Windows cannot run CLI scripts the same; it requires a different syntax.
To fix it, you simply have to do npm install --save cross-env and then in your package.json file, add cross-env to the beginning of the script declaration:
ie:
"scripts": {
"start": "NODE_ENV=test nodemon --exec 'mocha --recursive -R min'"
},
will become:
"scripts": {
"start": "cross-env NODE_ENV=test nodemon --exec 'mocha --recursive -R min'"
},
That should fix it.
To fix it on Windows, you'd have to change it to (I think):
"scripts": {
"start": "NODE_ENV=test&&nodemon --exec 'mocha --recursive -R min'"
},
You can do a quick test and change it to that and run it. If that fixes it, this is exactly your problem. But, that will break it on Linux, so generally, cross-env is recommended. Make sure you dont have spaces also at &&.

Laravel: npm run watch Error

I was working on a project and I run 'npm run watch'
and I got this error. I was working normally but all of sudden I got this error!
> # watch C:\projects\tests\blog
> node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development
node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --
config=node_modules/laravel-mix/setup/webpack.config.js
undefined:1
SyntaxError: Unexpected token
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe"
"C:\\Users\\Bruno\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "watch"
npm ERR! node v6.10.0
npm ERR! npm v4.3.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! # watch: `node node_modules/cross-env/dist/bin/cross-env.js
NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the # watch script 'node node_modules/cross- env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node node_modules/cross-env/dist/bin/cross-env.js
NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! C:\Users\Bruno\AppData\Roaming\npm-cache\_logs\2017-03-20T13_06_27_111Z-debug.log
What should I do?
If you are developing on a Windows system or you are running your VM on a Windows host system, you may need to run the npm install command with the --no-bin-links switch enabled:
Install using this command:
npm install --no-bin-links
And run this after:
npm run watch-poll
Source for 5.4
Source for <= 5.3
I just simply deleted built CSS and JS files as well as the mix-manifest.json file. Things worked afterwards
For me just deleted the mix-manifest.json file. Then did npm run watch-poll
My package.json script.
"scripts": {
"dev": "node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch-poll": "node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --watch-poll --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"hot": "node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"production": "node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
Running laravel from xampp shell (windows 7)
Note: Make sure you have the latest version of node.js and npm installed.
Clear NPM's cache:
sudo npm cache clean -f
Install called 'n':
sudo npm install -g n
Install latest stable Node.js version: sudo n stable
Alternatively pick a specific version and install like this:
sudo n (wanted node version)
You can get information on how to open an issue for this project with: npm bugs
It's literally described in the log output what to do...
try this also:
npm uninstall --save-dev sass-loader
It just ran perfectly when i tried to run the command npm run dev / npm run watch

Resources