I'm trying to use groupBy from lodash in a .ts file like this:
import { groupBy } from 'lodash';
But after tsc .\index.ts -w the resulting index.js turns that import into var lodash_1 = require("lodash"); and the following error appears:
index.js:28 Uncaught ReferenceError: require is not defined
Here is my package.json:
{
"name": "final_exercise",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"lodash-es": "^4.17.21",
"require": "^0.4.4",
"typescript-require": "^0.3.0"
}
The error happens with and without require and typescript-require being installed. Installing
require needed the npm audit fix --force, not sure if that's normal or not.
tsconfig.json
{
"compilerOptions": {
"target": "ES2019",
"module": "commonjs",
"lib": ["ES2019", "dom", "dom.iterable"],
"strict": true,
"sourceMap": true,
}
}
I also have this on the end of index.html, else the error would be ReferenceError: exports is not defined:
<script> var exports = {}; </script>
<script src="index.js"></script>
Since I'm new to Typescript, to modules and everything associated, I must be doing something wrong or I'm missing something. I've checked all similar questions with answers but nothing so far has worked.
Thanks for any help.