Answer the question
In order to leave comments, you need to log in
Why can't access the environment variable?
raised the project with a command vue init webpack project-name
figured out how to add pug and stylus
I want to set up environment variables: I used the plugin , created a file .env
, added a variable API_SECRET_KEY=efb9erfdf6fd9vfd98vd8ssv
(the value of the variable is fictitious for example) and sent it .env
to .gitignore
corrected /config/index.js
below what I added to the template configuration
const path = require('path')
const Dotenv = require('dotenv-webpack')
module.exports = {
...
...
...
module: {
rules: [
{
test: /.vue$/,
loader: 'vue-loader',
options: {
loaders: {
css: 'stylus-loader'
}
}
}
]
},
plugins: [
new Dotenv({ path: path.join(process.cwd(), '.env') })
]
}
App.vue
output a variable to the console, process.env.API_SECRET_KEY,
but it gives out undefined
.env
in the component's code?
Answer the question
In order to leave comments, you need to log in
With webpack 2, I solved the issue like this:
Using DefinePlugin, I defined variables in the global scope:
const GLOBALS = {
'process.env': {
'NODE_ENV': JSON.stringify('development')
},
API_SECRET_KEY: JSON.stringify('efb9erfdf6fd9vfd98vd8ssv'),
};
module.exports = {
plugins: [
new webpack.DefinePlugin(GLOBALS)
],
externals: {
API_SECRET_KEY: 'API_SECRET_KEY'
},
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question