The package linked to from here is now pure ESM. It cannot be require()'d from CommonJS.
This means you have the following choices:
- Use ESM yourself. (preferred)
Useimport foo from 'foo'instead ofconst foo = require('foo')to import the package. Follow the below guide. - If the package is used in an async context, you could use
await import(…)from CommonJS instead ofrequire(…). - Stay on the existing version of the package until you can move to ESM.
I would strongly recommend moving to ESM. ESM can still import CommonJS packages, but CommonJS packages cannot import ESM packages synchronously.
ESM is natively supported by Node.js 12 and later.
You can read more about my ESM plans.
My repos are not the place to ask ESM/TypeScript/Webpack/Jest/ts-node support question.
- Add
"type": "module"to your package.json. - Replace
"main": "index.js"with"exports": "./index.js"in your package.json. - Update the
"engines"field in package.json to Node.js 12:"node": ">=12". - Remove
'use strict';from all JavaScript files. - Replace all
require()/module.exportwithimport/export. - Use only full relative file paths for imports:
import x from '.';→import x from './index.js';. - If you have a TypeScript type definition, update it to use ESM imports/exports.
- Add
"type": "module"to your package.json. - Replace
"main": "index.js"with"exports": "./index.js"in your package.json. - Update the
"engines"field in package.json to Node.js 12:"node": ">=12". - Add
"module": "ES2020"to your tsconfig.json. - Use only full relative file paths for imports:
import x from '.';→import x from './index.js';. - You must use a
.jsextension in relative imports even though you're importing.tsfiles.
If you use ts-node, follow this guide.
The problem is either Webpack or your Webpack configuration. Please don't open an issue on my repo. Try asking on Stack Overflow or open an issue the Webpack repo.
Read this first. The problem is either Jest or your Jest configuration. Please don't open an issue on my repo. Try asking on Stack Overflow or open an issue the Jest repo.
If you have decided to make your project ESM ("type": "module" in your package.json), make sure you have "module": "ES2020" in your tsconfig.json and that all your import statements to local files use the .js extension, not .ts or no extension.
Follow this guide.
Follow this guide.