A2Hosting: npm works in terminal, but not for cron worker - node.js

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

Related

Powershell throws error when running npm start / npx tsc

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.

The command moved into a separate package: #webpack-cli/serve

I looked at and executed almost every single answer on this post:
The CLI moved into a separate package: webpack-cli
It has not helped.
Allow me to present my case:
So I have developed a container folder, a separate application where I ran npm init -y and then installed the following:
npm install html-webpack-plugin#4.5.0 nodemon webpack#5.3.2 webpack-cli#4.1.0 webpack-dev-server#3.11.0
Then I went into my package.json file and added the start script:
{
"name": "container",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack serve"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"html-webpack-plugin": "^4.5.0",
"nodemon": "^2.0.6",
"webpack": "^5.3.2",
"webpack-cli": "^4.1.0",
"webpack-dev-server": "^3.11.0"
}
}
When I go to terminal and run npm start, this is why I get every single time:
➜ container npm start
> container#1.0.0 start /Users/luiscortes/Projects/ecommRS/container
> webpack serve
[webpack-cli] The command moved into a separate package: #webpack-cli/serve
? Would you like to install #webpack-cli/serve? (That will run npm install -D #webpack-cli/serve) (Y/n) › true
And yes even if I choose Y or true and it runs its npm install -D #webpack-cli/serve, when I go back to running npm start it just gives me the same error over and over again.
Apparently, this is an issue with webpack-cli#4.1.0. I upgraded to webpack-cli#4.2.0 and now it works.

How do I use nodemon with docker in my node.js application?

I am new to Docker and coding. I have already added it as dev-dependency but still I have to build the image every time I make a change to the code. I have tried looking this up but have not found a solution that is suitable/working because I am using process.json file.
My Dockerfile :
FROM node:12.14.1-alpine
# app name
ENV APP_NAME=mock-api
ENV WORK_DIR /deploy/${APP_NAME}
# Create app directory
RUN mkdir -p ${WORK_DIR} && \
chown node:node ${WORK_DIR}
RUN apk add --update gnupg
WORKDIR ${WORK_DIR}
COPY ["yarn.lock", "package.json", "./"]
RUN yarn global add pm2 && yarn install --prod --frozen-lockfile && yarn cache clean
COPY --chown=node:node . .
EXPOSE 3000
USER node
CMD ["pm2-runtime", "--no-daemon", "--raw", "process.json"]
Process.json :
{
"apps": [
{
"name": "mock-api",
"script": "./app.js"
}
]
}
package.json:
{
"name": "mock-api",
"version": "1.0.0",
"description": "Mock API for the testing environment",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "arorasannidhya#gmail.com",
"license": "ISC",
"dependencies": {
"koa": "2.12.0",
"koa-joi-router": "^6.0.2",
"koa-logger": "3.2.1",
"koa-router": "9.0.1",
"openpgp": "4.10.4",
"pm2": "^4.2.3",
"uuid": "^7.0.2"
},
"devDependencies": {
"nodemon": "^2.0.4"
}
}
Nodemon is the utility to run the node application (it watches file in the directory and when changed runs index.js or app.js (whatever is your root file))
It cannot be used to build a docker image, you will need to do something like this https://vsupalov.com/rebuilding-docker-image-development/#:~:text=In%20Conclusion,see%20the%20results%20right%20away!

npm plugin "onchange" does not recognize scss files changes

I am npm newbee and trying to have a very simple build proccess with pure npm (without grunt, gulp, etc). All my package json scripts works fine execpt the one responsible to watch SCSS files and run compilers on file change.
Here is my Package.json files which should be self explaining:
{
"name": "test-site.com",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"scss": "node-sass --indent-type tab --indent-width 1 --output-style expanded -o dist/css src/scss",
"autoprefixer": "postcss -u autoprefixer -r dist/css/app.css",
"build:css": "npm run scss && npm run autoprefixer",
"serve": "browser-sync start --server --files 'dist/css/*.css, **/*.html, !node_modules/**/*.html'",
"reload": "browser-sync reload",
"watch:css": "onchange 'src/scss/*.scss' -- npm run build:scss",
"build:all": "npm run build:css",
"watch:all": "npm-run-all -p serve watch:css",
"postinstall": "npm run build:all && npm run watch:all"
},
"author": "Homam",
"license": "ISC",
"devDependencies": {
"autoprefixer": "latest",
"browser-sync": "latest",
"node-sass": "latest",
"npm-run-all": "latest",
"onchange": "latest",
"postcss-cli": "latest"
}
}
The problematic script is "watch:css".
When i change my "index.html" it changes the web page accordingly but there is no change effect when I change any of my SCSS files.
Are you on Windows?
If so, try to replace the single quotes with escaped double quotes as said in the onchange README.
"watch:css": "onchange \"src/scss/*.scss\" -- npm run build:scss",
Might be because
"watch:css": "onchange 'src/scss/*.scss' -- npm run build:scss",
Is referencing build:scss which is not defined? You've defined "scss" and "build:css". Try with:
"watch:css": "onchange 'src/scss/*.scss' -- npm run build:css",

running a command after install dependencies using npm install

I have a package.json file like this
{
"name": "E2E",
"version": "1.0.0",
"description": "AngularJS E2E testing",
"main": "conf.js",
"scripts": {
"postinstall": "node_modules/protractor/bin/webdriver-manager update",
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC",
"devDependencies": {
"protractor": "^2.2.0"
}
}
when running command npm install after protractor is installed its throwing error
node_modules/protractor/bin/webdriver-manager update
'node_modules' is not recognized as an internal or external command, operable program or batch file
Ok found the fix, I need to run it as node command like this
"postinstall": "node node_modules/protractor/bin/webdriver-manager update",
Try prepending the path to executable with a dot followed by a slash:
./node_modules/protractor/bin/webdriver-manager update
The problem is that you need to be in the folder where that command is installed before you call it. Assuming you are using Windows, this can be solved by running a simple batch file:
#echo off
call npm install -g protractor
call npm install
cd C:/Users/%USERNAME%/AppData/Roaming/npm/node_modules/protractor/selenium/
call webdriver-manage update
You should be able to run a batch file from anywhere. In fact, the entire Protractor testing process can be automated with a batch file. You just need to add Grunt, load-grunt-tasks, grunt-protractor-runner, jasime, and protractor-jasmine2-html-reporter to your package.json:
{
"name": "yourproject",
"version": "0.0.1",
"dependencies": { },
"devDependencies": {
"grunt": "~0.4.1",
"load-grunt-tasks": "~1.0.0",
"grunt-protractor-runner": "~2.1.0",
"jasmine": "~2.3",
"protractor-jasmine2-html-reporter": "~0.0.5"
},
"engines": {
"node": ">=0.12.0"
}
}
After you configure Protractor and writing some tests, you can then call the whole process with one simple batch file:
#echo off
cd %CD%
#echo running tests
call grunt
#echo Opening test results in browser
start "" %CD%\tests\reports\index.html

Resources