I'm trying to read environmental variables in ES6.
import dotenv from "dotenv";
dotenv.config()
I did the following however when I attempt to use
process.env.example
Like I've always done in common JS I get an error message stating that process is not defined. Can someone help me ?
in it's written in their docs
import * as dotenv from 'dotenv'
dotenv.config()
Here is an explanation ; you can read more here => https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
When you run a module containing an import declaration, the modules it
imports are loaded first, then each module body is executed in a
depth-first traversal of the dependency graph, avoiding cycles by
skipping anything already executed.
– ES6 In Depth: Modules
What does this mean in plain language? It means you would think the following would work but it won't.
// errorReporter.mjs
import { Client } from 'best-error-reporting-service'
export default new Client(process.env.API_KEY)
// index.mjs
import dotenv from 'dotenv'
dotenv.config()
import errorReporter from './errorReporter.mjs'
errorReporter.report(new Error('documented example'))
process.env.API_KEY will be blank.
Instead the above code should be written as..
// errorReporter.mjs
import { Client } from 'best-error-reporting-service'
export default new Client(process.env.API_KEY)
// index.mjs
import * as dotenv from 'dotenv'
dotenv.config()
import errorReporter from './errorReporter.mjs'
errorReporter.report(new Error('documented example'))
Uncaught ReferenceError: process is not defined is the message you get when running process.env.example in the browser console. You will only be able to access this environment variable on the server side, and not the client side.
Maybe do
import * as dotenv from 'dotenv'
As indicated by that library docs in case you are using ES6 instead of the old require() method.
See also: https://www.npmjs.com/package/dotenv
Related
I'm mixing some imports and I would like to have all of them using ES6.
import express from 'express';
import dotenv from 'dotenv';
import { PasswordItemDTO } from './api/clients/PasswordCheck/model/passwordItemDTO';
import { PasswordErrorsDTO } from './api/clients/PasswordCheck/model/passwordErrorsDTO';
const config = require('config');
I would like to use the same way, ES6, for the config module which I used commonJS.
I'm using it to retrieve some information: config.get('item'),
There is a possibility to do that? Thanks!
There is not other way for now. I checked in lot of websites. Thank you all.
Just add this line dotenv.config(); and you can then use the values in the .env file using destructuring to get your env values.
I'm using ES6 with meteor's ECMAScript package. I'm trying to develop my own package an have run into an issue that I don't know how to approach debugging.
In my package I have a module that is the main entry point main.js. In the module I import another moduleimport '../imports/startup/client/index.js';
In index.js I export a function a function. Right before I export this I check that it is defined and everything:
console.log(ExportedFunction)
export const Exportedfunction = ExportedFunction;
console returns:
ExportedFunction(opts) {
_classCallCheck(this, ExportedFunction);
Then in my example app I try to use that export:
import { Exportedfunction } from 'meteor/packageauthor:packagename'
console.log(Exportedfunction)
console returns:
undefined
So somewhere the export/import breaks but I don't know how to figure out why.
What are general steps to debug this type of failure?
Edit: It seems that exporting within a nested module is the issues. If I bring the same export const Exportedfunction = ExportedFunction; into main.js instead of importing it in index.js then everything works.
Why is this and/or how do I fix that so I can export from a module imported by another module?
I am pretty new to JavaScript and have been struggling with imports recently. There has been one thing I cannot wrap my head around.
In older node modules (mostly those which came to see the light prior to ES6), which may be installed using the npm, such as express, usually no default export is defined.
My IDE (WebStorm) marks the following line with the Default export is not declared in the imported module notification.
import express from 'express';
This message may be circumvented by trying to import the whole module as an alias using
import * as express from 'express';
implicitly telling my IDE to just import everything and name it express, however doing so then leads to an express is not a function error when trying to instantiate the application on the following line.
const app = express();
Peculiarly the original import (without the alias) works.
What exactly is imported using the import statement without the alias, when no default export is defined? I would think it is the whole module, but it does not seems so.
What does import Module from 'module' import when no default export is defined?
Nothing. In fact, instantiating the module will throw a SyntaxError when something is imported that is not exported or exported multiple times from the imported module.
Why is it different from import * as Module?
Because import * just imports a module namespace object that has the exports as properties. If you don't export anything, it'll be an empty object.
In older, pre-ES6 node modules usually no default export is defined.
Which means that you cannot import them as an ES6 module. Your IDE seems to expect that though and puts up the warning.
So what happens if you refer to them in an import declaration? Your module loader might do anything with it, and HostResolveImportedModule will return a module record that is not an source text "ES6 module" record - i.e. it might do anything implementation-dependent with CommonJS modules.
I really don't know how to do this and not sure how to google either.
Right now I have this
let source = require('vinyl-source-stream');
I would like to change to be import but this doesn't work
import {'vinyl-source-stream' as source} from 'vinyl-source-stream';
If that module even supports the ES6 import/export system, then what you want is this:
import source from 'vinyl-source-stream';
Your version is attempting to import an exported value named vinyl-source-stream from the module; instead, you just want the module itself to be imported (into an object named source in this case).
If you want everything in the module imported, instead of just the default exports, use this instead:
import * as source from 'vinyl-source-stream';
But neither of those will work if the module isn't actually written to use the new system.
This library doesn't use the ES2015 module system. It doesn't export at all, so you can't import it or from it.
This library uses the CommonJS module pattern (as can be seen in the source) and is meant to be requireed.
You could import the library with:
import form 'vinyl-source-stream';
which will cause the code to be executed, but that will be useless in this case since nothing (useful) will happen - in fact, you'll probably get a runtime exception due to undefined module.
I'm doing this in TypeScript, but tried it in vanilla JS as well with the same error. I've pulled down two modules: express and hbs. I'm trying to use the ES6 import syntax like this:
import * as http from 'http';
import * as express from 'express';
import hbs from 'hbs';
The last line gives me an error saying it can't find module hbs. I'm looking right at it... I can see it just fine. However when I replace the line with the older CommonJS syntax:
var hbs = require('hbs');
It works fine... what gives? Still on the learning curve with ES6...
Observation 1... as you have in your other exports, you should either import the whole module with an alias:
import * as hbs from 'hbs';
Or you can choose to import specific exports:
import {thing} from 'hbs';
Observation 2... is hbs a TypeScript module, or a JavaScript one? If it is a JavaScript one (as I believe it may be) you will need to pair it with a definition file, for example hbs.d.ts that describes the JavaScript file. TypeScript won't recognise a plain JavaScript module without the definition.
I was experiecing a similar problem. The syntax is correct ES6 indeed.
Good news is that the problem seems to have been fixed already in the development version of the typescript compiler 0.8: try 'npm install typescript#next -g' and then running the compiler again.
you should use default as the imported module name .
import {default as hbs} from "hbs";
this works same as
var hbs = require('hbs');
because require("hbs") imports default module exported by hbs.