Node 14 + TS + Import: Compilation Error
See original GitHub issuePackage’s name string-strip-html
Describe the bug
I added the string-strip-html
to a Node 14 server solution that works with TypeScript. The compilation failed with a mention to no use require
. However, I was using import
.
To fix the issue, I followed what @revelt mentioned in this post.
To Reproduce Steps to reproduce the behavior:
npm install string-strip-html@latest --save
import { stripHtml } from "string-strip-html";
- Run tsx on the project
TypeScript configuration has:
- target to es2016
- module to commonjs
Expected behavior To be able to consume the latest version with TypeScript
Issue Analytics
- State:
- Created 2 years ago
- Reactions:8
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Bug: TS compilation error because of missing URL type #794
1 and I'm running it via Docker container (using image node:14-alpine3.13 ).
Read more >Typescript module not found error after compilation
I ran into this problem when compiling TS to ES modules with Node 15. Turns out that TS does not add the ".js"...
Read more >ts-node - npm
This error is thrown by node when a module is require() d, but node believes it should execute as native ESM. This can...
Read more >TypeScript errors and how to fix them
ts ' cannot be compiled under '–isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export...
Read more >How to use TypeScript in Node.js - Execute Program
ts test.ts:1:14 - error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Can also confirm, 8.3.0 working fine for now in the same setup.
I ran into the same problem in a TypeScript project. I can use the latest version of
string-strip-html
in a standalone project withtype
set asmodule
inpackage.json
andimport
statements in plain.js
files, but in a larger TypeScript project the same doesn’t work and I’m seeing theERR_REQUIRE_ESM
error whenmodule
iscommonjs
intsconfig.json
or it just won’t work at all ifmodule
is set toes6
because packages likemongodb
won’t load as ES6 modules.The only working configuration for my setup with Node v12.22.7 is this:
package.json
doesn’t havetype
set tomodule
tsconfig.json
hasmodule
set tocommonjs
(needed for some 3rd-party packages, likemongodb
)tsconfig.json
hasmoduleResolution
set tonode
string-strip-html
v8.3.0 is installedI guess if one can find all of their dependencies written as ES modules, the latest version of
string-strip-html
can be used, but otherwise v8.3.0 seems to be the only solution for the time being.