在生产模式下访问羽毛客户端插件中的nuxt.js env变量

问题描述 投票:1回答:2

我似乎无法从feathers客户端插件访问process.env.baseUrl变量。 该变量返回一个空字符串。 我可以在服务器端使用该变量。 我可以通过在env.baseUrl中的env.baseUrl中对URL进行硬编码来解决它,但我宁愿不这样做。 奇怪的是,它在proddev模式下都在Windows 10计算机上运行时没有任何问题。 但是,在Google Cloud中的Ubuntu vm上,它不起作用。 从docker容器运行时,它也不起作用。 请指教。 我是否缺少明显的东西?

组态

系统

Ubuntu 16.04.3 LTS
Node 9.4.0

package.json

{
  "name": "my-app",
  "description": "",
  "version": "0.0.0",
  "main": "src",
  "keywords": [
    "feathers"
  ],
  "contributors": [],
  "bugs": {},
  "directories": {
    "lib": "src",
    "test": "test/"
  },
  "engines": {
    "node": "^8.0.0",
    "npm": ">= 3.0.0"
  },
  "scripts": {
    "build": "nuxt build",
    "dev": "cross-env HOST=localhost PORT=3000 BASE_URL=http://localhost:3000 DEBUG=@feathersjs* nodemon --watch src/ --watch config/ src/index.js",
    "prestart": "npm run build",
    "start": "cross-env HOST=0.0.0.0 PORT=8080 NODE_ENV=production BASE_URL=https://example.com node src/index.js",
    "test": "mocha test/services/",
    "dev-debug": "node --inspect node_modules/.bin/nuxt"
  },
  "dependencies": {
    "@feathersjs/authentication": "^2.1.1",
    "@feathersjs/authentication-jwt": "^1.0.2",
    "@feathersjs/authentication-local": "^1.1.0",
    "@feathersjs/client": "^3.4.0",
    "@feathersjs/configuration": "^1.0.2",
    "@feathersjs/errors": "^3.2.2",
    "@feathersjs/express": "^1.2.0",
    "@feathersjs/feathers": "^3.1.1",
    "@feathersjs/socketio": "^3.2.0",
    "@nuxtjs/axios": "^5.0.1",
    "@nuxtjs/font-awesome": "^1.0.3",
    "@nuxtjs/pwa": "^2.0.5",
    "@sendgrid/mail": "^6.2.1",
    "babel-eslint": "^8.2.1",
    "buefy": "^0.6.3",
    "compression": "^1.7.1",
    "cookie-storage": "^3.1.0",
    "cors": "^2.8.4",
    "feathers-authentication-management": "^2.0.0",
    "feathers-mongodb": "^2.9.1",
    "feathers-stripe": "^0.4.1",
    "feathers-vuex": "^1.1.4",
    "helmet": "^3.10.0",
    "izitoast": "^1.2.0",
    "mongodb": "^3.0.2",
    "node-ses": "^2.1.0",
    "nuxt": "^1.4.0",
    "nuxt-stripe-module": "^2.0.0",
    "pug": "^2.0.0-rc.4",
    "serve-favicon": "^2.4.5",
    "socket.io-client": "^2.0.4",
    "vee-validate": "^2.0.5",
    "vue-bulma-rating": "^1.0.1",
    "vue-no-ssr": "^0.2.2",
    "vue-notifications": "^0.9.0",
    "vue-observe-visibility": "^0.3.1",
    "vue-smoothscroll": "^0.1.1",
    "vue-social-sharing": "^2.3.3",
    "vue-stripe-elements": "^0.2.3",
    "vue2-animate": "^1.0.4",
    "winston": "^2.4.0"
  },
  "devDependencies": {
    "babel-plugin-module-resolver": "^3.1.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "cross-env": "^5.1.3",
    "eslint": "^4.17.0",
    "mocha": "^4.1.0",
    "node-sass": "^4.7.2",
    "nodemon": "^1.14.12",
    "request": "^2.83.0",
    "request-promise": "^4.2.2",
    "sass-loader": "^6.0.6"
  }
}

feathers.js

import feathers from '@feathersjs/client'
import io from 'socket.io-client'
import { CookieStorage } from 'cookie-storage'

console.log('logging in the client')
console.log(process.env.baseUrl)

const socket = io(process.env.baseUrl)


const feathersClient = feathers()
  .configure(feathers.socketio(socket))
  .configure(feathers.authentication({ storage: new CookieStorage() }))

export default feathersClient

nuxt.config.js

const path = require('path');

module.exports = {
  /*
  ** Headers of the page
  */
  head: {
  },
  loading: false,
  loadingIndicator: 'circle',
  plugins: [
    {src: '~plugins/buefy'},
    { src: '~/plugins/vue-observe-visibility' },
    { src: '~/plugins/feathers' },
  ],
  manifest: {
    theme_color: '#3B8070'
  },
  /*
  ** Modules
  */
  modules: [
    '@nuxtjs/pwa'
  ],
  rootDir: path.resolve(__dirname),
  srcDir: path.resolve(__dirname, 'client'),
  dev: process.env.NODE_ENV !== 'production',
  build: {
    vendor: [
      '@feathersjs/client',
      'socket.io-client',
    ],
    postcss: {
      plugins: {
        'postcss-custom-properties': false
      }
    }
  },
  env: {
    baseUrl: process.env.BASE_URL
  }
}
javascript node.js ubuntu-16.04 feathersjs nuxt.js
2个回答
0
投票

这与nuxt.js无关。 与此相关的是,无论交叉环境如何,都不会设置package.json中的环境变量。 唯一的选择是从Ubuntu本身的shell中导出环境变量。 该应用程序按设计工作。


0
投票

问题是由于您的buildprestart阶段。 当您在package.json以下时

"scripts": {
    "build": "nuxt build",
    "dev": "cross-env HOST=localhost PORT=3000 BASE_URL=http://localhost:3000 DEBUG=@feathersjs* nodemon --watch src/ --watch config/ src/index.js",
    "prestart": "npm run build",
    "start": "cross-env HOST=0.0.0.0 PORT=8080 NODE_ENV=production BASE_URL=https://example.com node src/index.js",
    "test": "mocha test/services/",
    "dev-debug": "node --inspect node_modules/.bin/nuxt"
  },

buildprestart启动作为独立的命令运行,它们将看不到HOST=0.0.0.0 PORT=8080 NODE_ENV=production envs。 所以你应该像下面这样

"scripts": {
    "build": "nuxt build",
    "dev": "cross-env HOST=localhost PORT=3000 BASE_URL=http://localhost:3000 DEBUG=@feathersjs* nodemon --watch src/ --watch config/ src/index.js",
    "prestart": "cross-env HOST=0.0.0.0 PORT=8080 NODE_ENV=production BASE_URL=https://example.com npm run build",
    "start": "cross-env HOST=0.0.0.0 PORT=8080 NODE_ENV=production BASE_URL=https://example.com node src/index.js",
    "test": "mocha test/services/",
    "dev-debug": "node --inspect node_modules/.bin/nuxt"
  },

或使用npm-run-all

© www.soinside.com 2019 - 2024. All rights reserved.