running a command after install dependencies using npm install - node.js

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

Related

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.

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

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.

Watcher automation in Mocha

I would like to achieve automation of motcha --watcher feature using package.json file without globally installing mocha.
One of npm features is to allow add custom scripts into npm command. Previously I configured test runner successfully and I can type in bash now:
npm test
Everything works fine, so I would like also do something similar because
./node_modules/mocha/bin/mocha --watch app.js test.js"
is not too efective.
My goal is to run mocha watcher by typing in bash:
npm watch
Unfortunately watcher doesn't run - instead I see standard output of npm command without parameters. It looks like my custom script wasn't registered by npm.
Here is my actual package.json file
{
"name": "screencast",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha test.js", // works properly after typing 'npm test'
"watch": "mocha --watch app.js test.js" // Syntax looks ok, but command 'npm watch' d
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.10.6"
},
"devDependencies": {
"mocha": "^2.0.1",
"supertest": "^0.15.0"
}
}
Anyone had this issue before?
For a 'custom' script like watch, you have to do npm run watch instead of npm watch

Resources