Powershell throws error when running npm start / npx tsc - node.js

I am trying to run a simple npm start from the powershell command in a express - typescript project. And I'm getting this error
/mnt/host/c/Users/-/AppData/Roaming/npm/node: line 8: /mnt/host/c/Users/-/AppData/Roaming/npm/node_modules/nodemon/bin/nodemon.js: not found
My package.json
{
"name": "express-typescript",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node build/index.js",
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": \[\],
"author": "",
"license": "ISC",
"devDependencies": {
"#types/express": "^4.17.14",
"typescript": "4.9.3"
},
"dependencies": {
"express": "4.18.2"
}
}
This is the node file config inside npm/node
\#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;;
esac
"$basedir/node_modules/nodemon/bin/nodemon.js" "$#"
exit $?
The thing is with git bash I have no problems but powershell is my main editor. I tried reinstalling Node, closing everything, restarting.
I don't know why node is looking for nodemon and even if I install the dependency globally it doesn't work. Any suggestion?
Same thing happens if I try to run npx node, npx tsc, etc.

Related

nodemon not working, Usage: nodemon [nodemon options] [script.js] [args] See "nodemon --help" for more [duplicate]

This question already has answers here:
nodemon not working properly
(19 answers)
Closed 7 months ago.
{
"name": "nodeserver",
"version": "1.0.0",
"description": "This is a node Server for LetsChat Application",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "neetu",
"license": "ISC",
"dependencies": {
"nodemon": "^2.0.4",
"socket.io": "^2.3.0"
}
}
First try to Install Nodemon as Global
npm install -g nodemon
As per your Package.json details, Run
nodemon index.js
If still you face issue, Try to add some details like Error Message you get running nodemon.

A2Hosting: npm works in terminal, but not for cron worker

I am using A2hosting and trying to configure cron worker, but no luck yet.
I followed this article to install node and npm, which is basically list of commands below
cd ~
wget https://nodejs.org/dist/v12.9.1/node-v12.9.1-linux-x64.tar.xz
tar xvf node-v12.9.1-linux-x64.tar.xz
mv node-v12.9.1-linux-x64 nodejs
mkdir ~/bin
cp nodejs/bin/node ~/bin
cd ~/bin
ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm
I have tried multiple commands
npm run --prefix ~/cloudflare-upload-tool start which produces /bin/bash: npm: command not found
and
~/nodejs/bin/npm run --prefix ~/cloudflare-upload-tool start which produces /usr/bin/env: node: No such file or directory
Both commands run fine in terminal, could you tell what are my options to fix this?
package.json
{
"name": "cloudflare-upload-tool",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "env-cmd node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.20.0",
"cloudflare": "^2.7.0",
"env-cmd": "^10.1.0",
"form-data": "^3.0.0",
"fs": "0.0.1-security",
"jsonwebtoken": "^8.5.1",
"path": "^0.12.7",
"tus-js-client": "^2.2.0"
}
}
Support told that this approach is not supported. Ended up creating node js app using UI, stopping it as we don't need to expose it and using command for cron worker below as suggested in other article
source /home/<USERNAME>/nodevenv/cloudflare_upload_tool/10/bin/activate && cd /home/<USERNAME>/cloudflare_upload_tool && npm run start

sh: 1: cucumber.js: not found

Trying to use this tutorial here:
https://github.com/lykmapipo/nodejs-cucumber-sample
The output to nvm current is: v10.12.0.
The output to npm --version is: 6.4.1>
I get the error below once I invoke npm test:
> nodejs-cucumber-sample#0.0.1 test /home/gnuc/code/nodejs-cucumber-sample
> cucumber.js
sh: 1: cucumber.js: not found
I am not sure as to why this is the case. The $PATH includes /home/gnuc/.nvm/versions/node/v10.12.0/bin. And I have already used npm install cucumber -g and npm install cucumber
Make sure that your package.json file includes this: "test": "cucumber-js"
So that it looks something like this:
{
"name": "hellocucumber",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": **"cucumber-js"**
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"cucumber": "^5.1.0"
}
}
So, you need to actually call the npm package/library. I have the following defined in my package.json:
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js"
},
you can also add some ---tags in this call.
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js --tags #RegressionTestSuite"
},
this will run any feature files that have #RegressionTestSuite at the top
Also, I have an output/results file created with a time stamp.
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js --tags #RegressionTestSuite --format json:./results/log_new_`date +%Y-%m-%m__%H-%M`.json""
},
I hope this helped.
node ./node_modules/cucumber/bin/cucumber-js
This command is working fine.
And you got sh:1: cucumber.js: not found error means first things please look out the path of cucumber.js

Running a custom built npm package not working

I am building an npm library and published it to npm.
After publishing I am trying to install the library globally.
But it's not working, looks like it's trying to open the file directly
My package.json looks like as follows:
{
"name": "lssomename",
"version": "1.0.4",
"description": "Test",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"run": "node index.js"
},
"author": "Test",
"license": "ISC",
"dependencies": {
"command-line-args": "^5.0.2",
"fs": "0.0.1-security",
"ncp": "^2.0.0",
"rimraf": "^2.6.2",
"zip-folder": "^1.0.0"
}
}
As soon as I try to run it throws the following error
/home/thabung/.nvm/versions/node/v8.5.0/bin/lssomename: line 1: $'\r': command not found
/home/thabung/.nvm/versions/node/v8.5.0/bin/lssomename: line 2: syntax error near unexpected token `('
'home/thabung/.nvm/versions/node/v8.5.0/bin/lssomename: line 2: `const log = require("./logger");
The same error comes if try to run directly the index.js file, meaning
when I try
./index.js
instead of
node index.js
Ok the issue was, I need to add an indicator in the begining of the file,
also known as shebang for Unix like systems like as follows
#!/usr/bin/env node
in the beginning of my index.js file & it started working after that.

Why devDependencies is not installed

I developed a package for example: testlab, and its package.json is:
{
"devDependencies": {
"mocha": "^2.0.0"
},
"name": "#aab/testlab",
"version": "2.6.0",
"description": "example for npm",
"main": ".\\dest\\main.js",
"dependencies": {
"gulp": "^3.9.1",
"gulp-changed": "^1.3.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"test"
],
"author": "aab <aab#exmaple.com>",
"license": "ISC"
}
Say I included mocha in devDendencies, and then I created a local directory called e.g. c:\example and used
npm install #aab/testlab --only=dev
to get my package under c:\example, but when I looked at c:\example\node_modules, I did not find mocha package is installed. I also tried other command like
npm install #aab/testlab
still no luck. I used NodeJS v4.6.0 and npm 4.0.2. Although nodeJS seems a little old, could any one help me that?
I have seen this happen only when NODE_ENV is set to PRODUCTION. Something else might be setting it.

Resources