I have a module that creates and exports a config object. When running in a browser environment, its string values must be JSON.stringified. Otherwise, they are interpreted as bare identifiers.
In node, JSON.stringify seems to add an extraneous set of double quotes.
What solution would work in both browser and node?
Most of the node modules I've checked out, searching for JSON.stringify deal with String representations/translations of complex objects.
works in browser + webpack 2.2.4, fails in node:
module.exports = {
url: JSON.stringify(process.env.SOME_URL),
// in node, resolves to '"https://..."'
headers: {
Authorization: JSON.stringify(basicAuthString),
// in node, resolves to '"Basic ...="'
'Content-Type': JSON.stringify('application/json'),
}
};
works in node, fails in browser + webpack 2.2.4:
module.exports = {
url: process.env.SOME_URL,
// in node, resolves to bare https://...
// Uncaught SyntaxError: Unexpected token :
headers: {
Authorization: basicAuthString,
// in browser, resolves to bare Basic ...=
// actions.js:15 Uncaught SyntaxError: Unexpected identifier
'Content-Type': JSON.stringify('application/json'),
}
};
The process.env values, in the case of webpack, are captured at build time, using this approach: https://github.com/AngularClass/angular2-webpack-starter/wiki/How-to-pass-environment-variables
via Monte Hayward
No comments:
Post a Comment