Scenario: You have a typescript project set up to generate JSON files. The tsconfig.json
is properly configured and all dependencies are in place. You've even referred to this related Q&A and ensured that your typescript files are importing the json files. However, when you run tsc
, the json files are not being generated.
In an attempt to troubleshoot, you replicate the project in a different location with identical file structure and dependencies, and upon running tsc
, the json
files are successfully created. Puzzling situation!
Below is a simplified demonstration using node v16.13.2 and yarn v1.22.7
Reference Project
If you have nodejs
and yarn
installed, you can follow these steps:
pwd
# /tmp/foolib
yarn init -y # initialize a new project
yarn add typescript # only dependency needed for reproduction
export PATH=$(yarn bin):$PATH # ensure `tsc` is in the path
Additional files required:
src/index.ts
import Thing from './moveme.json'
console.log("Hello")
src/moveme.json
{ "foo": "bar" }
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2015",
"declarationMap": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"outDir": "./dist",
"skipLibCheck": true,
"declaration": true,
"jsx": "react"
},
"include": [
"src/**/*"
]
}
If you execute
rm -rf dist; export PATH=$(yarn bin):$PATH; tsc; find dist
, you should observe the following output:
dist
dist/index.d.ts
dist/index.d.ts.map
dist/moveme.json
dist/index.js
Another Project
Considering that you might be utilizing the project as a library elsewhere, let's mimic this scenario:
mkdir /tmp/fooproject
cd /tmp/fooproject
yarn init -y # generates package.json, yarn.lock, node_modules
cp -R /tmp/foolib ./node_modules/ # create a local copy
cd node_modules/foolib
tsc
Upon running find dist
, you'll notice:
dist
dist/index.d.ts
dist/index.d.ts.map
dist/index.js
Why isn't moveme.json
present in the output?