ts-node seems can not require json file
See original GitHub issueone 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:
- Created 6 years ago
- Comments:9 (2 by maintainers)
Top 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 >
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 Free
Top 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

I fixed my demo by adding
"resolveJsonModule": true,totsconfig.json, see: https://github.com/freewind-demos/ts-node-import-json-file-issue-demo/commit/0f9e472a462425d4324b1f911f6781de7dd20cb7PS:
resolveJsonModuleis supported by typescript 2.9+ only, so old versions still have such issueThis worked for me to prefer .ts over .json for same-named files: