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).
Related
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
I am trying to publish my electron app to GitHub releases but I cannot get it to work.
This is what I have:
package.json
....
"scripts": {
"start": "electron . --disable-gpu",
"test": "echo \"Error: no test specified\" && exit 1",
"win": "electron-builder --windows nsis:ia32",
"linux": "electron-builder --linux",
"publish":"electron-builder -p onTag"
},
....
"build": {
"publish": [
"github"
],
....
}
when I run npm run publish it creates the executable but it does not publish on my GitHub releases repository:
$ npm run publish
> Inventory-pro#1.0.1 publish /home/xander/Develop/Electron/InventoryV2
> electron-builder -p onTag
• electron-builder version=21.2.0 os=5.3.0-26-generic
• loaded configuration file=package.json ("build" field)
• writing effective config file=dist/builder-effective-config.yaml
• packaging platform=linux arch=x64 electron=7.1.3 appOutDir=dist/linux-unpacked
• building target=deb arch=x64 file=dist/Inventory-pro_1.0.1_amd64.deb
I want to publish it so that I can include auto-update on my app on every release.
Don't specify the -p option and it will always publish a release because your script in the package.json is named release.
But please consider using automatic rules instead of explicitly specifying publish:
If npm script named release, — always.
Source: https://www.electron.build/configuration/publish#how-to-publish
...
"scripts": {
...
"publish":"electron-builder"
},
...
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.
running the release script without publish option tries to publish the build to GitHub ( and fails while complaining about not being able to find GHToken! )
Error: GitHub Personal Access Token is not set, neither programmatically, nor using env "GH_TOKEN"
Setting "publish": "never" will fail also complaining about not being able to find module electron-publisher-never!
Error: Cannot find module 'electron-publisher-never'
It all happens while the project is built but build scripts exits non-zero!
I'm using the latest version of electron-builder.
my build script:
"build": {
"appId": "eu.armand.[****]",
"copyright": "Copyright © 2017 mim_Armand",
"productName": "[****]",
"mac": {
"publish": "never",
"category": "public.app-category.[****]",
"icon": "assets/icons/mac/icon.icns"
}
Any idea what's going on or if I'm doing it wrong?!
,m
try building with
"build": "electron-builder --publish never"
to never publish.
rename your script to something else.
if the script name is release → publish is set to always
the documentation states this:
CLI --publish option values:
...
If npm script named release, — always.
Add to scripts in the development package.json:
"release": "build"
and if you run yarn release, a release will be drafted (if doesn’t
already exist) and artifacts published.
I solved it this way, because I didn't need to put it in any repository
"build":{
"appId": "XXX",
"productName": "XXX",
"directories":{
"output": "build"
},
"win":{
"target": "nsis",
"publish" : []
}
}
https://www.electron.build/configuration/publish
I'm having an issue getting my react /rails app to work on heroku. I've managed to get it deployed and the rails server starts but I'm not seeing my react app. I feel like I'm close but can't figure out what's missing.
So my process is currently to run npm run build locally from the client directory which builds a 'build' directory in client. I then commit the results of the build and push to Heroku with git push heroku master.
I then navigate to the heroku app in a browser where I'm only getting a blank white page which is an index file the I manually copied from the build dir to public. I'm not sure if the index file is correct but I just wanted to make sure i could hit something.
Ultimately, I would like to just push the repo and it automatically run the build. I've seen this mentioned various places but I always get a react-script does not exist error when I run npm run build on the server.
My configuration is as follows:
basic structure of app
/app - root of Rails app
/client - root of React app
/public - does display index page
root/client/package.json
{
"name": "qc_react",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-scripts": "^0.8.4"
},
"dependencies": {
"react": "^15.4.1",
"react-dom": "^15.4.1",
"react-router": "^3.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"cacheDirectories": [
"node_modules",
"client/node_modules"
],
"proxy": "${API_URL}:${PORT}/v1/"
}
root/package.json
{
"name": "web",
"version": "1.0.0",
"description": "This repo contains a web application codebase. Read instructions on how to install.",
"main": "index.js",
"scripts": {
"build": "cd client" # not sure what to put here but this gets me past build failure
},
"repository": {
"type": "git",
"url": "git+ssh://myrepo.git"
},
"author": "",
"license": "ISC",
"homepage": "https://myhomepage#readme"
}
Procfile
api: bundle exec puma -C config/puma.rb
Buildpacks
1. https://github.com/mars/create-react-app-buildpack.git
2. heroku/ruby
config/puma.rb
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3001
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Worker specific setup for Rails 4.1+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
ActiveRecord::Base.establish_connection
end
That's because Rails is serving static files (including index.html) from /public. But your React app bundle is located inside /client/build. After build you need to copy these files back to Rails folder. The easiest way to do this is to add some scripts to your package.json:
"scripts": {
...
"deploy": "cp -a build/. public",
"heroku-postbuild": "npm run build && npm run deploy"
},
heroku-postbuild is executed automatically by Heroku after all dependencies are installed, just like normal postinstall hook.
Be careful with the paths for cp command. Your setup with 2 different package.json files is too complex. I recommend to use a single package.json inside root and set all paths accordingly.
So I finally found the solution I was looking for. My code base has the following structure.
app/
client/
public/
...
I'm currently building my client solution as #yeasayer suggested above. After building and deploying my react project to the public folder in my rails api, I added the following to my routes:
... # api routes ...
get '/*path', to: 'react#index'
Then I created a react_controller and added the following contents:
class ReactController < ActionController::Base
def index
render :file => 'public/index.html', :layout => false
end
end
Now any routes not caught by the api routes, will render the react app. I'm not sure why others don't use this structure instead of using react_on_rails or some other plugin to achieve the same result. This setup is a lot simpler than dealing with these other solutions but I'd like to hear any thoughts on why this solution is not a good idea.
I see you are using the create-react-app-buildpack, but I think your issue is that your react app is in a subdirectory. Buildpacks only get executed if heroku can detect a matching application in the directory and since your package.json in your root does not match create-react-app-buildpack I don't think it is being used.
What you might try is removing the root package.json and using this sub directory buildpack so you can specify the locations of each buildpack directory