question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow whole file javascript variable exports and check for typeof function

See original GitHub issue

This is a Feature Proposal

Allow whole file javascript variable exports.

Description

  1. Currently entire yml and json files can be referenced in the serverless.yml file like so:

custom: ${file(../myCustomFile.yml)} # You can reference the entire file

However, there are some specific limitations and requirements when it comes to javascript files I find unnecessary.

Attempting to reference a javascript file standalone like…

custom: ${file(../myCustomFile.js)}

…throws an obscure error about split not being defined. This is because the currently implementation assumes that : followed by a property will exist.

While this can be overcome by exporting a named module in your javascript file, it is inconsistent with other supported forms of referencing variables.

  1. The other expectation for referencing variables in javascript files that is not well documented is that it needs to export a function. A simple typeof check with a reasonable error message can make this more clear.

This issue requests that referencing entire javascript files be supported and that a reasonable error message be shown if that file does not export a function as expected.

Similar or dependent issues:

  • NA

Additional Data

  • Serverless Framework Version you’re using: 1.16.0
  • Operating System: macOS
  • Stack Trace:
TypeError: Cannot read property 'split' of undefined
    at Variables.getValueFromFile (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:235:26)
    at Variables.getValueFromSource (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:173:19)
    at property.match.forEach (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:95:40)
    at Array.forEach (native)
    at Variables.populateProperty (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:86:43)
    at deepMapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:66:45)
    at deepMapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:61:14)
    at deepMapValuesIteratee (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:51:25)
    at /home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13379:38
    at /home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:4944:15
    at baseForOwn (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:3001:24)
    at Function.mapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13378:7)
    at deepMapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:59:39)
    at deepMapValuesIteratee (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:51:25)
    at /home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13379:38
    at /home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:4944:15
    at baseForOwn (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:3001:24)
    at Function.mapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13378:7)
    at deepMapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:59:39)
    at Variables.populateObject (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:64:5)
    at Variables.populateService (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:40:17)
    at Serverless.run (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/Serverless.js:92:27)
    at serverless.init.then (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/bin/serverless:30:50)
From previous event:
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)
From previous event:
    at __dirname (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/bin/serverless:18:28)
    at Object.<anonymous> (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/bin/serverless:34:4)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:390:7)
    at startup (bootstrap_node.js:150:9)
    at bootstrap_node.js:505:3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
infinitelukecommented, Jun 25, 2017

Yes. It still works and wouldn’t be a breaking change. Just added a branch to handle the additional case

0reactions
pmuenscommented, Jun 26, 2017

Yes. It still works and wouldn’t be a breaking change. Just added a branch to handle the additional case

Thanks for confirming @infiniteluke 👍 That’s great! We’ll take a look at the PR ASAP.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typeof - JavaScript - MDN Web Docs - Mozilla
The typeof operator returns a string indicating the type of the operand's value.
Read more >
Documentation - Type Checking JavaScript Files - TypeScript
How to add type checking to JavaScript files using TypeScript. ... Similarly, require function calls are recognized as module imports. For example: js....
Read more >
JavaScript TypeOf – How to Check the Type of a Variable or ...
The JavaScript typeof Operator​​ Here is how you use it when you're evaluating the type of a number, 007. In the above example,...
Read more >
Typeof behaves differently in normal and export functions
I want to check in JS with typeof, if function is declared. The code, where this check takes place, is in another file,...
Read more >
JavaScript: Check if Variable is a Number - Stack Abuse
In this tutorial, we'll be covering how to check if a variable is a number in JavaScript. We'll use the isNan(), typeOf() and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found