How can I replace $INIT_CWD in a Node.js run script with something "generic" that also works on Windows?
package.json in root
{
"name": "foo",
"version": "2.0.0",
...
"scripts": {
..
"start": "live-server $INIT_CWD/foobar --port=8080"
}
}
Test
This works fine on Linux and macOS; serves files from test/foobar.
$ cd test
$ npm start
However, on Windows it would have to be %INIT_CWD% instead of $INIT_CWD.
How can I modify package.json to be OS-agnostic?
The/one solution is to use cross-env-shell from https://www.npmjs.com/package/cross-env.
Run scripts that set and use environment variables across platforms
"devDependencies": {
"cross-env": "^6.0.3"
},
"scripts": {
"start": "cross-env-shell live-server $INIT_CWD/foobar --port=8080"
}
Related
I have the following package.json and I'd like to run the bins "build" and "run":
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"bin": {
"build": "./bin/build.js",
"dev": "./bin/dev.js"
}
}
I've tried:
yarn run build
and I get
error Command "build" not found.
I've also tried:
yarn build
but the same thing happens:
error Command "build" not found.
It's propably not the right way to run bins. But then again, what is the right way to run bins with yarn?
Your package isn't installed.
When Yarn (and NPM) installs your package, it adds the commands under node_modules/.bin/, e.g. node_modules/.bin/build. Running yarn build would (if it doesn't find a matching script in the current package) look for a build in this .bin, then traverse upwards through the filesystem, looking for other node_modules/.bin/build's.
If your build script is only meant to be run while developing that specific package, add it as a script (see example here). It would more or less look like this:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": {
"build": "node ./bin/build.js",
"dev": "node ./bin/dev.js"
}
}
Do not need relative path added:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": {
"build": "build.js",
"dev": "dev.js"
}
}
The hashbang comment specifies the path to a specific JavaScript interpreter that you want to use to execute the script.
For example, helloWorld.js in ./node_modules/.bin:
#!/usr/bin/env node
console.log("Hello world");
You have a typo in your package.json. Where it says bin: it should say scripts:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": { // <-- here
"build": "./bin/build.js",
"dev": "./bin/dev.js"
}
}
I want to run another command after running a Node.js server from a shell script on an Ubuntu box, but the second command never launch.
node server.js
xdg-open index.html
How can i fix it?
You can add into start in package.json >>
{
"name": "example",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node server.js && xdg-open index.html"
},
"dependencies": {
},
"devDependencies": {
}
}
and run with npm start
see: npm script
How would it be possible to set environment variables for Mocha tests under windows OS? I'm only able to add only 1 variable but not more, example:
"name": "node-app",
"version": "1.0.0",
"description": "some app",
"main": "index.js",
"scripts": {
"integration-test": "SET TEST_MODE=handler&mocha tests/test_cases/*.js --reporter spec"
},
"author": "",
This can be done under windows using cross-env without changing the source code, we only need to install it as a dev dependency and then add it to the script line. But still under other linux we can simply do this :
"scripts": {
"integration-test": "env KEY1=YOUR_KEY1 KEY2=YOUR_KEY2 mocha test"
},
I wonder if it is possible to make it happen for windows without additional libraries?
There's a package on npm solving this, called cross-env.
From the documentation:
{
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
}
}
You can also set multiple variables easily.
No extra library:
before(function (): void {
process.env.YOUR_VAR = 'yourVarValue';
});
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
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