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

Access nuxt.js env variable in feathers client plugin in production mode

我似乎无法从 feathers 客户端插件访问 process.env.baseUrl 变量。变量 returns 一个空字符串。我可以在服务器端使用变量。我可以通过硬编码 nuxt.config.jsenv.baseUrl 中的 url 来解决它,但我不想这样做。奇怪的是它在 windows 10 机器上以 proddev 模式运行没有任何问题。但是在 Ubuntu 云中的 Ubuntu 虚拟机上它不起作用。当来自 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
  }
}

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

问题是因为您的 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 环境。所以你应该像下面那样做

"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