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.
Related
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
I have this in my app with the #types/express dependency installed
import express = require('express');
It is pointing to the express and saying this is an unexpected identifier when I run my server. I believe this is correct TS syntax and the regular JS way of const express = .. has the same error.
Do I need regular express? or wouldn't I need the one I already installed, which should be for TS specifically?
To replace require statement with import statement, for example:
const express = require('express');
You can convert it to this:
import * as express from "express";
And yes, you need both, regular express as dependency and #types/express as dev-dependency to have TypeScript type definitions working.
The syntax you want will be
import express from "express";
and it shouldn't result in a duplicate identifier error unless its simply a IDE bug. You can look into a common setup most people use to work with NodeJS/Typescript here.
https://github.com/microsoft/TypeScript-Node-Starter
I had a particularly difficult situation because I was using esmodules with our production/development, but commonjs with our testing tools.
I ended up getting it to work by using both imports.
app.ts
import express, * as express_test from "express"
const app = express ? express() : express_test()
this article helped me a bunch, primarily w/r to the module: "commonjs" package.json entry
I am trying to use import instead of require for all modules in my project, but some older npm modules only have instructions for require.
In the case of 'isomorphic-fetch' I can't find the proper way to use import:
works
require('isomorphic-fetch')
fails
import 'isomporphic-fetch'
import Something from 'isomorphic-fetch'
// error Can't resolve 'isomporphic-fetch' from Project/src/js/
Converting to import does work with the es6-promise module.
works
require('es6-promise').polyfill()
works
import Something from 'es6-promise'
Something.polyfill()
Since import does work with other modules, and require('isomorphic-fetch') works, it's probably a named export problem.
Try import * as Something from 'isomorphic-fetch'
If that works, it's because isomorphic-fetch does not do export deafult so you have to pull in the imports by name, or use the notation I wrote above. Take a look at the MDN link I put on top.
I'm trying to work with TypeScript in my node project, but I have some issues with that.
This is my index.ts file:
import express from 'express';
const app = express();
I'm running:
tsc --module commonsjs -d index.ts
My output is index.js:
var express_1 = require('express');
var app = express_1["default"]();
Where did this ["default"] came from? It is making my code not to run properly:
var app = express_1["default"]();
^
TypeError: express_1.default is not a function
As far as I understand, I should have got the code without the "default" brackets and it would have worked fine - I tried removing the brackets and it worked.
What am I missing here?
I solved this by adding the following to tsconfig.json:
{
"compilerOptions": {
...
"module": "commonjs",
"esModuleInterop": true,
...
}
}
The esModuleInterop flag is described as: "Emit __importStar and __importDefault helpers for runtime babel ecosystem compatibility and enable --allowSyntheticDefaultImports for typesystem compatibility."
https://www.typescriptlang.org/docs/handbook/compiler-options.html
The safest solution would be:
import express = require('express');
This transpiles to:
var express = require('express');
The official documentation for import require declarations can be found here.
I believe TypeScript expects an export named "default" to function as your code above, judging from the final paragraph here.
Side note: It looks like TypeScript's newest version (typescript#1.8.0-dev.20151229 at the time of writing) will throw a warning on a compile attempt which would attempt to use a missing default:
index.ts(1,8): error TS1192: Module '"express"' has no default export.
Side note 2: An example from Microsoft using the import * as express from 'express'; syntax can be found here. When targeting a module of commonjs (as they are in this example), this will also transpile to var express = require('express');.
If you have at least TypeScript 2.7 and are targeting CommonJS, you can use esModuleInterop, as well.
From the link:
To give users the same runtime behavior as Babel or Webpack, TypeScript provides a new --esModuleInterop flag when emitting to legacy module formats.
Under the new --esModuleInterop flag, these callable CommonJS modules must be imported as default imports like so:
import express from "express";
let app = express();
We strongly suggest that Node.js users leverage this flag with a module target of CommonJS for libraries like Express.js, which export a callable/constructable module.
If you are trying to use the default export of a non-ES6 module like Express.js, you need to use the legacy import syntax import express = require('express').
In ES6 modules, there is no default value export like the module.exports of Node.js modules or the return of AMD modules; the default export of an ES6 module is just the default key. This is why, when you use an ES6 default import as you are attempting to do, TypeScript generates JavaScript with an access to the default property.
More information about this is available at New es6 syntax for importing commonjs / amd modules i.e. `import foo = require('foo')`.
If you still want to use the import keyword then use it like:
import express from "express";
// If the above is not supported by your project environment then follow as below
import * as express from "express";
In file tsconfig.json
{
"compilerOptions": {
...
"module": "commonjs"
...
}
}
Thanks to Josh Dando.
Another solution that could work for you is do it like this:
import * as express from express;
const app = express();
It should work in the same way.
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.