N
N
NazarJs2019-12-28 18:00:31
JavaScript
NazarJs, 2019-12-28 18:00:31

How to fix index.js:2 Uncaught ReferenceError: exports is not defined?

How to fix index.js:2 Uncaught ReferenceError: exports is not defined ?
5e076c3de1f1c926377967.png
5e076c8e91be7727510159.png
5e076d247814b781674713.png
5e076dc8bece1628675158.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2019-12-28
@disappearedstar

You probably compiled your TS to CommonJS and tried to open the resulting files in a browser.
It won't work, the browser doesn't know anything about the Node.js module system (from here exports is not defined).
Offhand, 2 ways to solve the problem:
1) build a bundle (look towards webpack, rollup, parcel)
2) change module to es2015 or esnext in tsconfig so as not to transform modules (for example, study this )

C
centwor10n, 2022-02-18
@centwor10n

Oh my god... I spent about two hours trying to solve it. And I really want to help somebody. The following works for me:
install:
@babel/plugin-transform-runtime
next step:
.babelrc

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "useBuiltIns": "usage",
                "corejs": 3
            }
            
        ]
    ],
    "plugins": ["@babel/plugin-transform-runtime"]
}

And allow modules:
ts.config
"target": "ES5",                         
"module": "CommonJS",

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question