ts-node: error TS1259: Module '"path"' can only be default-imported using the 'esModuleInterop' flag

Since this problem is happening across the project and not only native node, I guess this is a problem with typescript or bad cofiguration, and not an issue with ts-node.

Expected Behavior

import path from 'path' OR const path = require('path')

Actual Behavior

<./knexfiles.ts>

knexfile.ts:1:8 - error TS1259: Module '"path"' can only be default-imported using the 'esModuleInterop' flag

1 import path from 'path'
         ~~~~

  node_modules/@types/node/path.d.ts:152:5
    152     export = path;

It stops the warning when i use js syntax to import using: const path = require('path') But, I have other projects using the import syntax and working just fine so I threw It’s configuration on these other project, but still have the same error

Same error other file <./src/server.ts>:

npx tsc src/server.ts 
src/server.ts:1:8 - error TS1259: Module '"path"' can only be default-imported using the 'esModuleInterop' flag

1 import path from 'path'
         ~~~~

  node_modules/@types/node/path.d.ts:152:5
    152     export = path;

Steps to reproduce the problem

npm init npm i express knex pg npm i -D @types/express @types/node ts-node ts-node-dev typescript

Minimal reproduction

GIT REPO It’s just a study project from a bootcam using localhost config, not much of a deal. By the way, the problem is happening on the server folder, If you go to server2 folder, there is the typescript project with similar structure that is working as expected. My server/tsconfig.json is kinda messes up atm cause I was trying to fix the issue, but I’m gonna keep looking into that, so it might change.

Specifications

<package.json> "@types/express": "^4.17.6", "@types/node": "^14.0.27", "ts-node": "^8.10.2", "ts-node-dev": "^1.0.0-pre.44", "typescript": "^3.9.3"

  • Operating system and version:

OS: Ubuntu 20.04.1 LTS node: v12.18.3

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15

Most upvoted comments

Closing since, as you say, this is not a ts-node issue.

I’m confused why you posted this here? Can you clarify your motivation at all? If you encountered a TypeScript bug, presumably you would post this on the TypeScript issue tracker. If you had a general TypeScript question, you would have asked on StackOverflow, the official TypeScript Discord, reddit, or any number of other places. Did someone tell you to post here? Or is there something confusing in our README that leads people to post unrelated issues here?

set "esModuleInterop": true in your tsconfig.json

I’ve also realized using import * as thing from 'module' also works.

Ran into a similar issue.

still didn’t work for me, but I learned today about import thing = require("thing") and my mind is blown 😄 this way you get typing and it just works

I’ve added a few helpful links to our “new issue” page. https://github.com/TypeStrong/ts-node/issues/new/choose

Kind of a dick…be nice to him.

set "esModuleInterop": true in your tsconfig.json

This solved the issue. Although I had to add it to “tsconfig.node.json”

I’ve added a few helpful links to our “new issue” page. https://github.com/TypeStrong/ts-node/issues/new/choose

still didn’t work for me, but I learned today about import thing = require("thing") and my mind is blown 😄 this way you get typing and it just works

This worked perfectly

I had to update my tsconfig.json to exclude the “node_modules” folder and then restarted my TypeScript server.

This was it for me, thanks!

I had to update my tsconfig.json to exclude the “node_modules” folder and then restarted my TypeScript server.