question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Node 14 + TS + Import: Compilation Error

See original GitHub issue

Package’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:

  1. npm install string-strip-html@latest --save
  2. import { stripHtml } from "string-strip-html";
  3. 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:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
giovannijoubertcommented, Sep 20, 2021

Can also confirm, 8.3.0 working fine for now in the same setup.

3reactions
gh-andrecommented, Nov 20, 2021

I ran into the same problem in a TypeScript project. I can use the latest version of string-strip-html in a standalone project with type set as module in package.json and import statements in plain .js files, but in a larger TypeScript project the same doesn’t work and I’m seeing the ERR_REQUIRE_ESM error when module is commonjs in tsconfig.json or it just won’t work at all if module is set to es6 because packages like mongodb won’t load as ES6 modules.

The only working configuration for my setup with Node v12.22.7 is this:

  • package.json doesn’t have type set to module
  • tsconfig.json has module set to commonjs (needed for some 3rd-party packages, like mongodb)
  • tsconfig.json has moduleResolution set to node
  • string-strip-html v8.3.0 is installed

I 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found