Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
0 votes
0 answers
15 views

Commonjs and ES6. Import doesn't work on module

I'm on react, I have a file I need to run on a command like this "runFunction": "npx ts-node ./src/seedParkStreetSalesData.js" And this is the file /* import { Op } from '...
alexgarciaalcuadrado's user avatar
0 votes
2 answers
780 views

Dynamically load ESM module in CJS package in TypeScript

I am trying to load a single ESM module in my TypeScript CJS project. All examples I find are for JavaScript. // example.ts export const example = async () => { const module = await import("...
myol's user avatar
  • 9,780
2 votes
2 answers
970 views

Trying to import KcAdminClient

I’m trying to use the Keycloak Admin Client library in my Nodejs (Typescript) application, but there's a problem about ES6/CommonJs stuff, which I never really understood (import vs require and mixing ...
Lucio Crusca's user avatar
  • 1,507
0 votes
1 answer
295 views

SheetJS: Error: require() of ES Module /node_modules/xlsx/dist/cpexcel.full.mjs not supported

I am using NestJS/TypeScript and one of my service files has the following imports which were taken from their (SheetJS) documentation page. /* load the codepage support library for extended support ...
Antonio Pavicevac-Ortiz's user avatar
0 votes
1 answer
442 views

async import() function from CommonJS to load ESM asynchronously:

I am trying to use one npm package which is ESM https://www.npmjs.com/package/key-did-resolver I can not require the 'key-did-resolver' package after installation as it's ESM , So I am trying to load ...
Mostafa0707's user avatar
-2 votes
1 answer
148 views

Why import can be used in this commonjs module?

Code is here: https://github.com/timewalker08/wechaty-test/tree/master According to nodejs document: If the nearest parent package.json lacks a "type" field, or contains "type": &...
timewalker's user avatar
0 votes
0 answers
747 views

Reference Error when Importing TypeScript Module into JS File

I am currently facing an issue while introducing TypeScript into a portion of my project. My project uses CommonJS for module management and Pug as a template engine. I'm attempting to import a class ...
Hugo's user avatar
  • 73
1 vote
0 answers
707 views

How to set-up a .mjs file when importing from cjs?

I'm using a third party library that provides an index.js and index.mjs file. The package.json has this: "exports": { "import": "./index.mjs", "require": &...
user1087973's user avatar
  • 1,108
0 votes
0 answers
68 views

How to correctly import a module dynamically and use a function from it?

I am importing got dynamically, like this: const { default: moduleGot } = await import('got'); moduleGot(); Then, I want to execute a function from it: const response = await got(url, { method: ...
Falcon Stakepool's user avatar
0 votes
1 answer
813 views

Include a few ES style npm modules into a project with mostly commonJS modules

I have browsed a lot of questions in this space, but I haven't found a clear answer. I recently updated many of my project's npm dependencies to their latest. The project is entirely built around ...
farhadf's user avatar
  • 1,968
1 vote
1 answer
769 views

Unable to import/require ES module from common JS

I'm using electron to build a UI for a specific project, the problem is that I wrote the core code in ES while apparently ES breaks electron. What I am trying to do is to export some callback ...
PastaLover's user avatar
-3 votes
1 answer
593 views

I cannot find a way to use 'require' in html

I am now developing JS in html, but I'd got following error: Uncaught (in promise) ReferenceError: require is not defined. The code is just simple subs = require("./subscriptions.json").subs;...
dbld's user avatar
  • 1,087
0 votes
1 answer
436 views

How it is possible to import express from 'express' without a default export?

Using typescript and installing @types/express it is possible to import the express as a default, but there's no export default on express index.d.ts. I know there's the flag ...
João Paulo Marinho Izar's user avatar
42 votes
2 answers
12k views

What is the difference between `main` and `module` vs `exports` in package.json?

I'm trying to write a library that can be both required and imported.'ve found different approaches online, which are as follows: { "main": "mylib-cjs.js", "module": &...
EnderShadow8's user avatar
  • 1,030
1 vote
0 answers
211 views

Typescript/CommonJS Cannot use import statement outside a module

quick typescript/commonjs question: I’m stuck on this error and have tried changing type:module in config.json and various module:es2020, esnext, … but am still stuck with this. Any known quick fixes? ...
Kentaro T. Vadney's user avatar
1 vote
1 answer
495 views

How can Include both commonJs require syntax and es modules import syntax in the same bundle - webpack

before tagging as a duplicate, I searched for an answer before I asked and I found this one: Is it possible to use both "require" and "import" together with Webpack? The problem is ...
Ran Marciano's user avatar
  • 1,501
0 votes
1 answer
130 views

Rollupjs Leave imports unchanged

My input file looks like this: import * as chalk from 'chalk' const chalkInstance = new chalk.Instance({ level: 1 }) My output file looks like this: import { Instance } from 'chalk'; const ...
LionKing's user avatar
  • 141
0 votes
1 answer
4k views

ReferenceError: Can't find variable: require when importing Node module in <script> tag

Having trouble importing a Node Module using the <script> tag from the HTML. Folder structure: . ├── dist/home.html ├── src/html/home.html └── node_modules └── @group ├── sub1/... ...
Dan K.'s user avatar
  • 102
0 votes
2 answers
496 views

Is it possible to use ES6 import without creating a variable and execute in a single line, similar to CommonJS?

Just wondering, am I able to import ES6 modules without creating a "placeholder" variable and run it immediately? for instance, instead of ES6's import that creates an unused express variable: ...
Jamie Phan's user avatar
0 votes
1 answer
327 views

What is the type of a commonjs module imported into a typescript file?

I am importing a CommonJS module in a Typescript source. As a result I receive an object containing exported features of the module. In my concrete use case the declarations for NodeJS's fs module ...
Michael's user avatar
  • 250
1 vote
3 answers
2k views

How to export a TypeScript module so that can be imported both from TypeScript and Node?

Let's say a have the following TypeScript module: function foo () { return 123; } //TODO: Export code here I want to export it in such a way that can be imported in these ways from TypeScript: ...
Fabio Spampinato's user avatar
0 votes
1 answer
319 views

Problems importing a shared script in Babel & Node

I have a javascript module that needs to be imported in two different codebases: nodejs (8.11.3 and up NOT transpiled using babel) vuejs 2.5.16 (bundled by webpack & transpiled by babel) I'm ...
askmike's user avatar
  • 1,960
0 votes
0 answers
27 views

ES6 import still within an object after import - I've tried a dozen configurations. What am I overlooking?

I have quite a lot of experience with ES6 and imports, but I usually don't do imports and exports this way. I'm trying to understand what I'm overlooking here. I've done a lot of searching here, but ...
Slbox's user avatar
  • 13k
0 votes
0 answers
876 views

Webpack 4 replace import

I have javascript file with import, for example import React from 'react'; My webpack 4 config have ... target: 'node', module: { rules: [ { test: /\.js$/, exclude: /node_modules/,...
akomkov's user avatar
  • 73
2 votes
1 answer
1k views

Import module components on demand

I'm using a lib in my project and loading the components of it that I preset through the commonjs module system. The code looks like this: let echart = require('echarts/lib/echarts'); require('...
Sávio Raires's user avatar
1 vote
3 answers
1k views

Disadvantage of ES6 and CommonJS export convention

For the main file of an npm module, as defined by the package.json, I was considering a pattern like this: import Struct from './src/struct' module.exports = Struct module.exports.default = Struct ...
Patrick Roberts's user avatar
4 votes
2 answers
4k views

TypeScript: Avoid require statements in compiled JavaScript

In my TypeScript code I am using a third-party library called bunyan like so: private logger: bunyan.Logger = bunyan.createLogger({name: "MyClass"}); Because TypeScript cannot resolve the variable ...
Benny Code's user avatar
  • 54.6k
21 votes
1 answer
15k views

CommonJS imports vs ES6 imports

Here are two examples of using a non-default export. The first uses commonjs syntax, and the second uses es6. Why does the first example work, but not the second? // commonjs --- works! var ...
Jonny's user avatar
  • 1,041
0 votes
1 answer
506 views

Use types (interfaces) from other commonjs modules in Visual Studio using TypeScript without having to import that module?

I'm trying to simply use types (interfaces) from other commonjs modules. I'm wondering if there is a way to do this without having to import the module each time I need just the types (no ...
user3667122's user avatar
5 votes
1 answer
3k views

How would I translate this require to ES6 import style [duplicate]

I would like to do this var debug = require('debug')('myapp'); ... in ES6 without creating an extra variable. Can it be done?
Thijs Koerselman's user avatar
112 votes
4 answers
153k views

module.exports in typescript

does somebody know how to do a module.exports? I tried some different ways ending up with export class Greeter {} which will compile to exports.Greeter = Greeter; But what I really want is this: ...
Kersten's user avatar
  • 1,722