I'm trying to build a simple app that makes use of the google cloud vision api for image analysis using node, though can't workout how to build the auth step into a simple npm script.
To access my account I currently set my auth credentials in the terminal (a bash terminal though I'm on a windows pc) as below, and can then run my server.js file which calls the google api.
export GOOGLE_APPLICATION_CREDENTIALS="C:\Users\tim\01_animal_snap\my-key.json"
This clearly doesn't take that long to write out and works fine, but I'd love to simply run a command like npm run auth instead.
I tried putting the following into my package.json file:
"scripts": {
"auth": "export GOOGLE_APPLICATION_CREDENTIALS=\"C:\\Users\\tim\\01_animal_snap\\my-key.json\"",
But when I run the script I get the following error:
'export' is not recognized as an internal or external command,
operable program or batch file.
I thought this might be an issue with running the 'export' command in a bash environment on windows (maybe npm scrips get executed in cmd or powershell by default?) and so installed cross-env and changed my script to:
"scripts": {
"auth": "cross-env export GOOGLE_APPLICATION_CREDENTIALS=\"C:\\Users\\tim\\01_animal_snap\\my-key.json\"",
Though I'm still getting the same error about export not being recognised. How can I run this command using an npm script?
export is not required when cros-env is used.
Try with
"auth": "cross-env GOOGLE_APPLICATION_CREDENTIALS="c://..."
Related
Developing locally on a Mac, I have this as an npm script in package json:
npx #svgr/cli --template path/to/template.js --ext .tsx my-icon.svg
This works as expected.
However, the Azure DevOps pipeline build fails when this script is run with the message Unknown command line option: '--ext'
It's not running in a container, so I assume it's running in a windows environment and that has something to do with this error.
--ext tsx is a valid command line argument; like i said it works in a unix environment (*macbook pro)
Any idea how to get around it?
So far I've tried the extension to the config file, but that particular option is only available through the cli.
I had a similar problem, but with GitHub actions, and my solution was to use
./node_modules/.bin/svgr --template path/to/template.js --ext .tsx my-icon.svg
Not sure if it will work in your case, though.
I'm using Stencil.js to create a web component library and I'm heavily relying on E2E tests. As they're rather slow it becomes more and more cumbersome to run the entire test suite (using the Stencil.js CLI) while developing new components.
However, I'm not able to run single tests in my IDE (IntelliJ IDEA) or via command line. It works perfectly fine for unit tests though.
My Jest config looks like this:
module.exports = {
"roots": [
"<rootDir>/src"
],
"preset": "#stencil/core/testing"
}
When I try to run tests in a single file (jest --config jest.config.js --testPathPattern src/components/button/button.e2e.ts$)
it fails, because
newE2EPage() is only available from E2E tests, and ran with the --e2e cmd line flag.
newE2EPage() comes with Stencil.js and I don't know what Stencil.js's CLI does in the background. Furthermore, I cloned the Stencil.js repository, just to see if it is working with their E2E tests (https://github.com/ionic-team/stencil/tree/master/test/end-to-end) but it doesn't work either.
Any idea how I can configure Jest so that it's able to run Stencil.js-E2E tests from the command line?
The --e2e flag is used for the npm script in the package.json. To start e2e tests, you can add this in your package.json:
"scripts": {
"test:e2e": "stencil test --e2e"
}
And run npm run test:e2e. For a specific file, you add it at the end like this:
npm run test:e2e src/components/button/button.e2e.ts
For more info, see the StencilJS doc: https://stenciljs.com/docs/end-to-end-testing
i have the same problem. IntelliJ and 'Run' single 'it' didnt work.
newE2EPage() is only available from E2E tests, and ran with the --e2e cmd line flag.
when i run 'npm run test' everything will work fine. the difference is that npm run stencil before and only jest dont work.
here is the stencil jest dir https://github.com/ionic-team/stencil/tree/master/src/testing/jest aswell a config.
i found in here https://stenciljs.com/docs/testing-overview a VS-CODE run jest code but no Intellij setup.
im on the run to get the path of the current file to run stencil via npm and the path the e2e file. but i cant find the correct variable for the run config.
i hope we got this solved soon.
cheers
I am not a VS Code user, but in contrast to IntelliJ there is a launch.json for VSC to run single tests: https://github.com/ionic-team/stencil-site/pull/480
I want to integrate some kind of code linting for node.js in webstorm so I installed standard to my node.js project using:
npm instal standard --save-dev
It was installed and listed in the "devDependencies" section of package.json but when I run the command:
standard
in the console I get
'standard' is not recognized as an internal or external command
if you want to use it locally you have to include it in you scripts first in package.json
"scripts": {
"standard": "standard",
"standard::fix": "standard --fix"
}
and use npm run standard to run it. or if you are using yarn type yarn standard
The scripts are in node_modules\.bin.
So, either:
Add this to PATH before running standard, e.g.:
set PATH=%PATH%;node_modules\.bin
Run it in using node_modules\.bin\standard
Use #tarek's approach using package.json: https://stackoverflow.com/a/49026837/122441
"scripts": {
"test": "standard middlewares/validations.js"
}
Add above lines in package.json.
Here middlewares/validations.js is the path of the file to check.
Run -> npm test
If this file have any error you will get.
I noticed this strange behavior which is not a big deal, but bugging the heck out of me.
In my package.json file, under the "scripts" section, I have a "start" entry. It looks like this:
"scripts": {
"start": "APPLICATION_ENV=development nodemon app.js"
}
typing npm start on a Mac terminal works fine, and nodemon runs the app with the correct APPLICATION_ENV variable as expected. When I try the same on a Windows environment, I get the following error:
"'APPLICATION_ENV' is not recognized as an internal or external command, operable program or batch file."
I have tried the git-bash shell and the normal Win CMD prompt, same deal.
I find this odd, because typing the command directly into the terminal (not going through the package.json script via npm start) works fine.
Has anyone else seen this and found a solution? Thanks!!
For cross-platform usage of environment variables in your scripts install and utilize cross-env.
"scripts": {
"start": "cross-env APPLICATION_ENV=development nodemon app.js"
}
The issue is explained well at the link provided to cross-env. It reads:
Most Windows command prompts will choke when you set environment variables with NODE_ENV=production like that. (The exception is Bash on Windows, which uses native Bash.) Similarly, there's a difference in how windows and POSIX commands utilize environment variables. With POSIX, you use: $ENV_VAR and on windows you use %ENV_VAR%.
I ended up using the dotenv package based on the 2nd answer here:
Node.js: Setting Environment Variables
I like this because it allows me to setup environmental variables without having to inject extra text into my npm script lines. Instead, they are using a .env file (which should be placed on each environment and ommitted from version control).
You should use "set" command to set environment variables in Windows.
"scripts": {
"start": "set APPLICATION_ENV=development && nodemon app.js"
}
Something like this.
I am very new to node.js and npm. I was trying to setup an open source project locally on my windows and I got this error
'NODE_ENV' is not recognized as an internal or external command,
operable program or batch file.
So I edited build:bundle in package.json, SET NODE_ENV=xyz from NODE_ENV=xyz. It installed the project properly then.
My question is, Is there a way to tell npm to use SET NODE_ENV=xyz on windows and NODE_ENV=xyz on linux/mac, so that I can create a fix without breaking the entire project.
Install and use the package named cross-env
$ npm install --save-dev cross-env
Run scripts that set and use environment variables across platforms
You can then use one syntax, so the build:bundle script will be something like this:
...
"scripts": {
"build:bundle": "cross-env NODE_ENV=xyz ...",
...
}