DieProduktMacher / serverless-env-generator

A Serverless 1.x plugin to manage environment variables with YAML and turn them into a .env file on deployment. Supports encryption with KMS, multiple stages and custom profiles.
MIT License
31 stars 11 forks source link

TypeError: Cannot convert undefined or null to object #9

Open gfrileux opened 5 years ago

gfrileux commented 5 years ago

Hi,

I'm currently trying to use serverless-env-generator but must be doing something wrong, as I'm running into this error

TypeError: Cannot convert undefined or null to object
    at Function.assign (<anonymous>)
    at helper.getEnvVars.then.envFiles (/Users/frix00/Sites/betility/betility-graphql/node_modules/serverless-env-generator/src/index.js:120:41)
From previous event:
    at PluginManager.invoke (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:464:22)
    at PluginManager.run (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:496:17)
    at variables.populateService.then (/usr/local/lib/node_modules/serverless/lib/Serverless.js:116:33)
    at runCallback (timers.js:693:18)
    at tryOnImmediate (timers.js:664:5)
    at processImmediate (timers.js:646:5)
    at process.topLevelDomainCallback (domain.js:121:23)
From previous event:
    at Serverless.run (/usr/local/lib/node_modules/serverless/lib/Serverless.js:103:74)
    at serverless.init.then (/usr/local/lib/node_modules/serverless/bin/serverless.js:52:28)
    at /usr/local/lib/node_modules/serverless/node_modules/graceful-fs/graceful-fs.js:111:16
    at /usr/local/lib/node_modules/serverless/node_modules/graceful-fs/graceful-fs.js:45:10
    at FSReqWrap.oncomplete (fs.js:139:20)
From previous event:
    at initializeErrorReporter.then (/usr/local/lib/node_modules/serverless/bin/serverless.js:52:6)
    at runCallback (timers.js:693:18)
    at tryOnImmediate (timers.js:664:5)
    at processImmediate (timers.js:646:5)
    at process.topLevelDomainCallback (domain.js:121:23)
From previous event:
    at Object.<anonymous> (/usr/local/lib/node_modules/serverless/bin/serverless.js:38:39)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)

Apparently, the culprit is here: this.options.environment = Object.assign(this.serverless.service.provider.environment, environment, dotenv.config({ path: path.join(config.servicePath, '.env.local') }).parsed)

and this.serverless.service.provider.environment is undefined...


Here my package.json:

{
  "name": "betility-graphql",
  "version": "1.2.0",
  "description": "Grpah QL Layer for Betility",
  "main": "graphql.js",
  "lint-staged": {
    "*.{json,css,md}": [
      "prettier --write",
      "git add"
    ],
    "*.js": [
      "prettier --write",
      "eslint --fix",
      "git add"
    ]
  },
  "scripts": {
    "precommit": "lint-staged",
    "lint": "eslint .",
    "startcache": "serverless offline start --skipCacheInvalidation",
    "start": "serverless offline start",
    "pretest": "npm run lint",
    "test": "jest --coverage",
    "test:watch": "npm test -- --watch"
  },
  "repository": {
    "type": "git",
    "url": "git+ssh://git@bitbucket.org:ccinvest/betility-graphql.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "homepage": "https://bitbucket.org/monamourdecomptable/backend-guideline#readme",
  "dependencies": {
    "apollo-server-lambda": "^2.8.1",
    "axios": "^0.19.0",
    "dotenv": "^8.0.0",
    "graphql": "^14.4.2"
  },
  "devDependencies": {
    "aws-sdk": "^2.454.0",
    "eslint": "^5.5.0",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-config-prettier": "^3.0.1",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jest": "^21.21.0",
    "eslint-plugin-prettier": "^2.6.2",
    "husky": "^0.14.3",
    "jest": "^23.5.0",
    "lint-staged": "^7.2.2",
    "prettier": "^1.14.2",
    "serverless": "^1.30.1",
    "serverless-domain-manager": "^3.2.6",
    "serverless-env-generator": "^1.4.1",
    "serverless-offline": "^5.9.0",
    "serverless-plugin-warmup": "^4.7.0-rc.1"
  }
}

I currently have a .env : USER_BACKEND_DOMAIN=http://

and my environment.yml : dev: USER_BACKEND_DOMAIN: 'http:///'

After intialising the project, I run sls env --attribute USER_BACKEND_DOMAIN --value "http://<IP-address>/" --stage dev Then: sls env create

Then I create my .env again (since it gets deleted by the "create" command

Finally, I run sls offline start

and get the error mentioned. I also tried sls offline start --stage dev but I get the same output.

Where am I going wrong ?

Thanks

tsamaya commented 5 years ago

Hi,

the trouble is the plugin is assuming the environnent section is defined for the provider

at that line node_modules/serverless-env-generator/src/index.js:120:41 the undefined value is this.serverless.service.provider.environment

could it be a good idea to default everything to {} ?

gfrileux commented 5 years ago

ah ! I can confirm that my serverless.yml does not set this variable:

# serverless.yml
service: my-api
provider:
  name: aws
  runtime: nodejs8.10
  stage: dev
  region: eu-west-3
  iamRoleStatements:
    - Effect: Allow
      Action:
        - lambda:InvokeFunction
        - lambda:InvokeAsync
      Resource: '*'

plugins:
  - serverless-domain-manager
  - serverless-env-generator
  - serverless-offline

# Plugin config goes into custom:
custom:
  envFiles: #YAML files used to create .env file
    - environment.yml
  basePath: api
  apiVersion: v1
... 
rest of the file
tsamaya commented 4 years ago

May 2020, PR #10 is still not merged. For now my solution is to stick to exact version 1.3.0.

Please, can you comment here. Would you consider fixing this issue for serverless offline users ?