I am using a specific task runner to execute my node scripts called Yoshi. I want to run a script from package.json, for example, yoshi test but with a custom environment variable (for example special_variable):
"scripts": {
"start": "yoshi start",
"test": "special_variable=value yoshi test // this case"
}
Apparently, I can pass an environment variable using the ENV prefix in my npm-script like this:
"scripts": {
"start": "yoshi start",
"test": "ENV special_variable=value yoshi test"
}
I think you can create .env file and add your variables there.
Related
I want to make an electron exe file, when launched, the WebdriverIO test is executed and the Allure-report is generated with the output of the result, at the moment I am doing the test with the npm test command, then I am generating the report with the allure generate --clean allure-results command and then I am making the exe file with the command electron builder --win, is it possible to do what I want or not? If possible please tell me how
My wdio.conf:
{
"name": "electron",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"start": "electron .",
"test": "wdio"
},
I have a .sh file with the follow code:
react-scripts start HOST=0.0.0.0
And my yarn start scripts is:
"scripts": {
"start": "./run.sh",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
The problem is that when I access to process.env object on a react component, I don't see this variable. How can I do this to work?
This seem to work for me: Setting environment variables in a shell script for a React app
So the solution is to add REACT_APP as prefix of your variables.
My .sh file ended like this:
#!/bin/sh
export REACT_APP_PYTHON_API_IP="localhost"
yarn start
Below is the code from package.json file
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
},
Below is my next.config.js file, here console.log always undefined
require("dotenv").config();
console.log(process.env.BASE_URL)
module.exports = {
env: {
API_BASE_URL: process.env.BASE_URL
},
reactStrictMode: true,
}
And this is in the .env.development
BASE_URL: 'http://localhost:3000'
When I ran the npm run dev command,
it prints on terminal "Loaded env from /var/www/html/next/next-SC/.env.development"
So, why the console prints undefined always.
I'm using next js version "10.0.4"
I assume you are using React with nextjs. If not, then please disregard this answer. I am doing the same thing. React has built in support for env vars. All you need to do is to prefix REACT_APP to your environment vars. So, in your .env.development or .env.staging, etc., you can have REACT_APP_BASE_URL=https://blah.com. You can then access them in your app using process.env.REACT_APP_BASE_URL. Then to build based on environment, I have (I am using craco, you would just use your normal build command)
"build:nightly": "env-cmd -f .env.nightly craco build",
"build:nightly": "env-cmd -f .env.staging craco build",
"build:nightly": "env-cmd -f .env.beta craco build",
...
For development environment, name the file .env.development, for production .env.production.
Next.js has built-in loader for environment variables. So dotenv or similar packages aren't needed. Just add the files. It will be loaded automatically (see documentation).
I'd like to achieve something like this:
npm run build --path custom
in order to set a different output.path that will be used in webpack.config.js
Does something like this exists?
I managed to achieve something similar to my goal like using a config field like this:
"name": "foo",
"config": {
"dist": "bar"
},
"scripts": {
"build": "webpack --watch",
"custom": "node test.js"
}
...
I managed to modify the value of dist ("bar") with:
npm config set foo:dist apple
And I can access the value of dist with:
process.env.npm_package_config_dist
Probably not ideal, but it works. I'm open to better suggestions.
The solution used in the end uses this command:
DIST=[YOUR-DIRECTORY] npm run build
The webpack.config handles the value of DIST like:
const target = process.env.DIST
? process.env.DIST
: 'dist';
and in the output we go with:
path: path.resolve(__dirname, target)
You may try this:
"name": "npm-help",
"scripts": {
"build": **"sass --watch"**,
}
I am using browserify to merge all the .js files of my app into a dist/main.js. My package.json looks like:
"main": "./dist/main.js",
"scripts": {
"start": "electron ./dist/main.js",
},
"bin": {
"electron": "./node_modules/.bin/electron"
}
and I can correctly run my application with npm run start.
However if in main.js I use app.getAppPath() I get:
/home/myuser/projects/electronProject/node_modules/electron/dist/resources/default_app.asar
I would expect this to be
/home/myuser/projects/electronProject/dist/main.js
Did I misunderstood the usage of this method? How can I get the path of the Electron program entrypoint? What is the role of default_app.asar?
Thanks
Why aren't you using __dirname (node.js) or process.resourcesPath (electron)?
https://github.com/electron/electron/blob/master/docs/api/process.md
https://nodejs.org/docs/latest/api/globals.html#globals_dirname
It returns the current application directory:
app.getAppPath()
Returns String - The current application directory.
From the docs.
An asar file is a simple archive format that just appends the files to each other. I'm not sure exactly how you're building the application but tools like electron-packager and electron-builder output the files into a resources/app.asar archive and run the files from there. That means that your current application directory is going to be something/resources/app.asar. From there your main file is located at something/resources/app.asar/main.js.
For whom may ran into the same problem...
It's maybe a problem with your electron configuration field main in package.json
The script specified by the main field is the startup script of your
app, which will run the main process.
The example code from offical websites:
{
"name": "your-app",
"version": "0.1.0",
"main": "main.js",
"scripts": {
"start": "electron ."
}
}
app.getAppPath() output:
YOUR_PATH_TO/electron-quick-start
If you change the code snippet to
{
"name": "your-app",
"version": "0.1.0",
"main": "main.js",
"scripts": {
"start": "electron YOUR_PATH_TO/main.js"
}
}
Then app.getAppPath() output:
YOUR_PATH_TO/electron-quick-start/node_modules/electron/dist/resources/default_app.asar
So the consolution is : If you want to change the startup script, change it in the main field, not just change it in scritps field...