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

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

Related

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.

npm: start script fails

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

Whats the proper way to start nodemon on a deploy environment?

I seem to be having an issue running nodemon from node_modules in a deployed instance.
I have roughly this in my package.json
{
...
"version": "0.0.3",
"main": "dist/src/server.js",
"description": "Persistence Layer",
"engines": {
"node": "~6.7"
},
"scripts": {
"start": "nodemon",
},
"dependencies": {
...
"nodemon": "^1.11.0",
...
}
}
I have the following in my nodemon.json file
{
"restartable": "rs",
"verbose": true,
"debug": 5858,
"delay": 1,
"watch": [
"dist/",
"node_modules/"
],
"ext": "js",
"args": [
"--debug=5858",
"--max_old_space_size=6384",
"--optimize_for_size",
"--max_executable_size=6384",
"--stack_size=6384"
]
}
When i try npm run start i get the following:
jrlil#28178a64e860:/app# npm run start
npm info it worked if it ends with ok
npm info using npm#3.10.8
npm info using node#v6.9.1
npm info lifecycle api#0.0.3~prestart: api#0.0.3
npm info lifecycle api#0.0.3~start: api#0.0.3
> api#0.0.3 start /app
> nodemon
sh: 1: nodemon: Permission denied
npm info lifecycle -api#0.0.3~start: Failed to exec start script
npm ERR! Linux 3.10.0-514.16.1.el7.x86_64
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "start"
npm ERR! node v6.9.1
npm ERR! npm v3.10.8
npm ERR! code ELIFECYCLE
npm ERR! -api#0.0.3 start: `nodemon`
npm ERR! Exit status 126
npm ERR!
npm ERR! Failed at the -api#0.0.3 start script 'nodemon'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
However, when i run it using the following everything works as expected.
$node node_modules/nodemon/bin/nodemon.js
[nodemon] 1.12.1...
Why isn't npm run able to look into my node_modules folder and start nodemon?
This is actually more of a Linux question than a Node question, as it's a permissions issue -- the script nodemon being ran by npm does not have the right permissions.
If you use npm run start to invoke nodemon and you have the right permissions (e.g. root), npm will "hand off" the execution to nodemon and in the process possibly change the user to one without root permissions to be safe:
From the docs:
If npm was invoked with root privileges, then it will change the uid
to the user account or uid specified by the user config, which
defaults to nobody. Set the unsafe-perm flag to run scripts with root
privileges.
If you run node_modules/nodemon/bin/nodemon.js yourself (and you have root permissions), it bypasses that "hand off" so that nodemon.js is run with root permissions.
The most correct way to deploy a node application is to use something like pm2 to manage processes, and not use nodemon since nodemon is mostly used to watch for changes and restart the server (which is mostly only useful in development contexts). If you still want to use nodemon, you can combine it with the forever package with nodemon like explained here.

Running babel-cli from npm script not working

I followed the directions here to install babel-cli. I added "build": "babel src -d lib" to my package.json in the directory I want to run it in. However, on running, I get this error:
🐕 npm run build
> ipfs-readme-standard#1.0.0 build /Users/richard/src/ipfs-readme-standard
> babel src -d lib
src doesn't exist
npm ERR! Darwin 14.5.0
npm ERR! argv "/Users/richard/.nvm/versions/node/v5.0.0/bin/node" "/Users/richard/.nvm/versions/node/v5.0.0/bin/npm" "run" "build"
npm ERR! node v5.0.0
npm ERR! npm v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! ipfs-readme-standard#1.0.0 build: `babel src -d lib`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the ipfs-readme-standard#1.0.0 build script 'babel src -d lib'.
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 ipfs-readme-standard package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! babel src -d lib
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs ipfs-readme-standard
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls ipfs-readme-standard
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /Users/richard/src/ipfs-readme-standard/npm-debug.log
I'm at a loss. Shouldn't src be generated? There's no extra step on babeljs.io that I am missing.
Shouldn't src be generated?
This is the folder that contains the script that you want to be transpiled. If it doesn't exist then babel will throw the error you posted.
Also, take note of what it says at the bottom of page you linked to:
Pre-6.x, Babel enabled certain transformations by default. However, Babel 6.x does not ship with any transformations enabled. You need to explicitly tell it what transformations to run. The simplest way to do this is by using a preset, such as the ES2015 Preset.
This means that even if you create a src directory and place a file containing ES6 code in it, Babel will happily run, but the output will be (almost) identical to the input.
This is a quick example of how to get up and running with the babel-cli.
Create a project, then install the babel-cli package and ES2015 preset:
mkdir babeltest && cd babeltest
touch package.json
npm install babel-cli babel-preset-es2015 --save-dev
Next edit package.json:
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"build": "babel src -d lib"
},
"scripts": {
"build": "babel --presets es2015 src -d lib"
},
"devDependencies": {
"babel-cli": "^6.0.0"
}
}
Notice that the command in the npm scripts is slightly different to that on the babel homepage, in so far as we're telling it to use the installed presets.
Next make a file in the src directory:
mkdir src && cd src
touch main.js
In main.js add:
[1,2,3].map(x => x * x)
Then run babel via npm:
npm run build
And inspect the output in lib/main.js
"use strict";
[1, 2, 3].map(function (x) {
return x * x;
});
You also get this error when your node modules are not installed, If you download the code from the internet and immediately try running the code, it throws the above error, just run
npm install
and then
npm run build // or other commands should work
In case someone is still looking for solution, check if .babelrc is missing
If so simply create a new .babelrc file and paste the above snippet in it.
{
"presets": ["es2015", "stage-0"]
}

Npm install failed with "cannot run in wd"

I am trying to get my node environment set up on a new Ubuntu 12.04 instance, with Node 0.8.14 already installed, but I ran into problems when I try to run npm install.
So when I try npm install, it says that I need to run it as root or adminisrator:
Error: EACCES, mkdir '/usr/local/lib/node_modules/coffee-script'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/coffee-script']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/coffee-script',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/coffee-script',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ 'DirWriter._create (/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23)',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:297:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
But when try to run it as sudo, it says the following:
npm WARN cannot run in wd PackNodeDev#0.0.1-166 npm install -g coffee-script node-gyp (wd=/home/ubuntu/PackNode)
In my package.json, it contains the following scripts:
"scripts": {
"preinstall": "npm install -g coffee-script node-gyp",
"start": "node server.js",
"test": "mocha --require should --compilers coffee:coffee-script --colors"
},
The rest of devdependencies are valid since I have been installing it all right on my own machine (Mac)
Does anyone have a clue why this is happening?
The documentation says (also here):
If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the user config, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.
Your options are:
Run npm install with the --unsafe-perm flag:
[sudo] npm install --unsafe-perm
Add the unsafe-perm flag to your package.json:
"config": {
"unsafe-perm":true
}
Don't use the preinstall script to install global modules, install them separately and then run the regular npm install without root privileges:
sudo npm install -g coffee-script node-gyp
npm install
Related:
package.json for global module installation
The only thing that worked for me was adding a .npmrc file containing:
unsafe-perm = true
Adding the same config to package.json didn't have any effect.
I have experienced the same problem when trying to publish my nodejs app in a private server running CentOs using root user. The same error is fired by "postinstall": "./node_modules/bower/bin/bower install" in my package.json file so the only solution that was working for me is to use both options to avoid the error:
1: use --allow-root option for bower install command
"postinstall": "./node_modules/bower/bin/bower --allow-root install"
2: use --unsafe-perm option for npm install command
npm install --unsafe-perm
OP here, I have learned a lot more about node since I first asked this question. Though Dmitry's answer was very helpful, what ultimately did it for me is to install node with the correct permissions.
I highly recommend not installing node using any package managers, but rather to compile it yourself so that it resides in a local directory with normal permissions.
This article provides a very clear step-by-step instruction of how to do so:
https://www.digitalocean.com/community/tutorials/how-to-install-an-upstream-version-of-node-js-on-ubuntu-12-04
!~~ For Docker ~~!
#Alexander Mills answer - just made it easier to find:
RUN npm set unsafe-perm true
I fixed this by changing the ownership of /usr/local and ~/Users/user-name like so:
sudo chown -R my_name /usr/local
This allowed me to do everything without sudo

Resources