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.

ts-node seems can not require json file

See original GitHub issue

one of my config module like this


export interface ConfigSetting {
// some code 
}
export const config : ConfigSetting  = require('./config.json')

tsc compiles well.

when i use ts-node, it has some error like

let db = new Sequelize(config.database)
                             ^
TypeError: Cannot read property 'database' of undefined

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
freewindcommented, Aug 22, 2018

I fixed my demo by adding "resolveJsonModule": true, to tsconfig.json, see: https://github.com/freewind-demos/ts-node-import-json-file-issue-demo/commit/0f9e472a462425d4324b1f911f6781de7dd20cb7

PS: resolveJsonModule is supported by typescript 2.9+ only, so old versions still have such issue

1reaction
donaldoakescommented, Jul 15, 2022

This worked for me to prefer .ts over .json for same-named files:

export TS_NODE_PREFER_TS_EXTS=true
Read more comments on GitHub >

github_iconTop Results From Across the Web

Import JSON file in node application with TypeScript
I've tried: import * as config from '../config.json' . Should work when JSON is handled like a module I guess... – ppulwey. Jul...
Read more >
Documentation - What is a tsconfig.json - TypeScript
The tsconfig.json file specifies the root files and the compiler options required to compile the project. JavaScript projects can use a jsconfig.json file...
Read more >
cannot use import statement outside a module ts-node
Option 1. In the nearest parent package.json file, add the top-level "type" field with a value of "module" . This will ensure that...
Read more >
Configuration - Quokka.js
Quokka uses ts-node to run your TypeScript files. Overriding Quokka's TypeScript Dependencies. Quokka is installed with three dependencies that are required to ...
Read more >
How to import JSON files in ES modules (Node.js) - Stefan Judis
Learn about the ways to read and import JSON files in Node.js ECMAscript modules (ESM).
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