convert module.exports[something] to es6 syntax - node.js

I am migrating my code from commonJS to es6 as I have to upgrade my node version to 16+.
But, I got stuck here.
module.exports[path.parse(file).name] = sequelize.import(dir+'/'+path.parse(file).name);
I need to convert it to es6 syntax. Tried to google it, but couldn't find anything.

Related

weird Nodejs: I never use `require()`, but got an error

Please see Issue
I'm very sure that neither the package and I used no require(), but still got an error that tells me don't use require()
How weird it is!
code with error:
import stripAnsi from 'strip-ansi';
error:
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\13931\Desktop\ucon\node_modules\strip-ansi\index.js from C:\Users \13931\Desktop\ucon\src\utty.ts not supported.
Instead change the require of index.js in C:\Users\13931\Desktop \ucon\src\utty.ts to a dynamic import() which is available in all commonjs modules.
And the most confused thing is that:
import statement is helpful everywhere except importing strip-ansi!
Make sure that your TypeScript config is set for an appropriate target version of Javascript and appropriate target module type that supports and will use import so that the TypeScript compiler will generate code that uses import. If not, then it will generate code that uses require().
You can always look at your compiled (plain Javascript) code and see what it is generating.

why is "export default" not working in my node back end code?

Does anybody know why I can do some es6 things like arrow functions in my node back-end code (without babel), but I can't do other es6 things like "export default"? (get an error " Unexpected token 'export'")
I'm using node 12.14 btw.
You definitely can. Add "type": "module" to your package.json.
Check out the Node.js documentation for ECMAScript modules
The reason it took them so long to implement this is that...it's complicated. Read more about the challenges here.

Transpiling TypeScript to Node

I am creating a Node Project with Typescript and I have set the target option to es6 in tsconfig.json
Node version 8 supports the async/await syntax but Typescript converts that to a generator function
How can I tell typescript not to convert es6 features already present in Node?
async/await is supported in ES2017, so u may need to set your target in tsconfig.json to ES2017.
P.S. You may find meseret an interesting library to work with, if you're into TypeScript, async/await and Node.

Make Typescript node modules work together

Has anybody got a setup working in which module 1 using typescript is referencing another module 2 also with typescript and both use tsd types such as node.d.ts ?
I have no problem compiling both and using them, as long as i simply require module 2. But as soon as i use import instead, i get into duplicate identifier hell, due to the fact that source files in both modules import e.g. node.d.ts typings from obviously different paths. Classes in both projects use e.g 'streams' or 'lodash' thus both use them in typings and thus use the /// reference syntax. The tsconfig in both projects excludes typings.
Typescript has come a long way since this was asked, and it's now much easier. You can link the proper files in package.json:
{
"main": "library-commonjs.js",
"module": "library-es6-module.js",
"typings": "library-commonjs.d.ts"
}
main is used by packages using CommonJS and Node.js module resolution. module is used by packages supporting ES6 imports. And typings is followed by Typescript (currently Typescript 2.2) to resolve type definitions for the package.
After struggling with this, I spent some time creating a typescript boilerplate/starter project that demonstrates how to do it properly:
https://github.com/bitjson/typescript-starter
That project includes a lot more documentation, and several examples of how to import code from typescript projects.
EDIT: With TS 2.2 this has gotten quite a lot better. See the accepted answer.
It seems this is not really possible yet in typescript 1.8.x. But they seem to work on it via https://github.com/Microsoft/TypeScript/issues/7156.
Also the problem is supposedly mitigated by using a jsconfig.json which should be used by VScode (see https://blogs.msdn.microsoft.com/vscode/2015/07/06/visual-studio-code-es6/). Sadly i didnt get it working yet.

How do I compile Typescript to with nodejs 4?

now that nodejs4 support classes and arrow functions, how do I tell typescript not to polyfill it?
now that nodejs4 support classes and arrow functions, how do I tell typescript not to polyfill it
You might think you can use target es6, but don't. E.g. the following in TypeScript :
function test(bar = 123){
}
Compiles to JavaScript with target es6:
function test(bar = 123) {
}
But default parameters aren't supported by node yet (reference)
Till the compatibility table of Node exceeds that of TypeScript ... be very careful! or just target es5.
Assuming you're using TypeScript now for node, you are likely specifying that your target output is ES5. Which means that it will polyfill/transpile ES6/7 features into the paradigm of ES5 in order to run in today's browsers and previous versions of node.
In order to use those features of ES6 today in node v4 you would just need to change your build process to output ES6 via the target option.
Note: this is true if you are using command line arguments or a tsconfig.json

Resources