0

I got a problem when I try to access the variable:process.env. It told me I got a syntax error and I'm 100% sure that my code is correct. I want to know what problem with it

I tried check my codes over and over again. I can't find any syntax error.By the way, it's ok when I access the proceess varible. But when I try to access the process.env, it started to execute eval, which raised the error I mentioned before.

My code goes that :

src/global.vue

console.log(process.env);

The error I got:


Uncaught SyntaxError: Unexpected token :
    at Object../node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue (app.js:1170)
    at __webpack_require__ (app.js:679)
    at fn (app.js:89)
    at eval (App.vue?9e04:1)
    at Object../src/App.vue (app.js:5245)
    at __webpack_require__ (app.js:679)
    at fn (app.js:89)
    at eval (main.js:5)
    at Object../src/main.js (app.js:5357)
    at __webpack_require__ (app.js:679)

The app.js:1170 goes that:

eval("/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__components_headers__ = __webpack_require__(\"./src/components/headers.vue\");\n/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__components_blogfooter__ = __webpack_require__(\"./src/components/blogfooter.vue\");\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n\n// import axios from 'axios'\n\n\n/* harmony default export */ __webpack_exports__[\"a\"] = ({\n  name: 'App',\n  components: { 'headers': __WEBPACK_IMPORTED_MODULE_0__components_headers__[\"a\" /* default */],\n    'blog-footer': __WEBPACK_IMPORTED_MODULE_1__components_blogfooter__[\"a\" /* default */]\n  },\n  created: function created() {\n    this.$store.dispatch('GET_BASIC');\n    console.log(Object({\"NODE_ENV\":\"development\",\"SERVER\":http://localhost:8000}));\n  }\n});//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9ub2RlX21vZHVsZXMvYmFiZWwtbG9hZGVyL2xpYi9pbmRleC5qcyEuL25vZGVfbW9kdWxlcy92dWUtbG9hZGVyL2xpYi9zZWxlY3Rvci5qcz90eXBlPXNjcmlwdCZpbmRleD0wIS4vc3JjL0Fwc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QUFDQTtBQUNBO0FBQ0E7QUFSQSIsInNvdXJjZVJvb3QiOiIifQ==\n//# sourceURL=webpack-internal:///./node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue\n");

I solve it by adding double quotes on the value of the variable SERVER:

Old code

module.exports = merge(prodEnv, {
  NODE_ENV: '"development"',
  SERVER: 'http://localhost:8000'
})

New code

module.exports = merge(prodEnv, {
  NODE_ENV: '"development"',
  SERVER: '"http://localhost:8000"'
})

I don't know why it has no hint to tell me that thing. The grammar of config file is so weird that you need to put a quote outside an existed quote.

Can anyone tell me why?

12
  • If your code is 100% correct then you did not come there :) Commented Jul 5, 2019 at 12:11
  • I know that, I just want to express my worry.So it's exaggerating . I stuck in the problem for a long time. Commented Jul 5, 2019 at 12:13
  • You should reduce your code to a MWP. I.e. the smallest state where the error still occurs. I can only say that in my vue application where I tried it, running console.log(process.env) works fine. Commented Jul 5, 2019 at 12:17
  • Are you using vue-cli or a custom webpack config? What's your webpack config? Commented Jul 5, 2019 at 12:32
  • @DecadeMoon Vue-cli. Commented Jul 5, 2019 at 13:39

2 Answers 2

1

After instpecting the error in app.js:1170 one can see that your process.env.serversomehow misses " - why that is no clue but i think this should be fixable now^^

currently:

{server: http://localhost:8000}

but should be

{server:"http://localhost:8000"}
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know where can I edit it. Every address used in my config file has been added single quotes.
Oh I fixed it. The value in the config file should add single quotes and double quotes like the NODE_ENV did.
0

I had a similar problem when working in a project with webpack, I made a configuration of the .env to node and to the test environment, but when running in the web environment, this same error message, as if the process. envdid not exist. In my case I was using the dotenv lib, so it was pretty simple to solve, I just saw that theJSON.stringifysetting inDefinePlugin` was missing.

Resolved configuring it this way:

new webpack.DefinePlugin({
  "process.env": JSON.stringify(dotenv.parsed),
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.