Situation: you have a typescript project that's configured to also output JSON files. You have the correct tsconfig.json
setup and the correct dependencies. You have also read this related Q&A and made sure your typescript files import the json files. Yet, when you run tsc
, you notice that json files are missing.
Then you copy the project to a separate location, with the exact same file layout, exact same binaries, and run tsc
. Now, the json
files get generated. WTFNODE?
Here's a minimal repro, using node v16.13.2 and yarn v1.22.7
reference project
suppose you have nodejs
and yarn
installed at the ready. If using Nix
, you can achieve this via nix-shell -p yarn nodejs
. Then:
pwd
# /tmp/foolib
yarn init -y # blank project
yarn add typescript # this is all we need for the repro
export PATH=$(yarn bin):$PATH # make sure we see `tsc` in the path
the remaining files:
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 run rm -rf dist;export PATH=$(yarn bin):$PATH; tsc; find dist
you should see this output:
dist
dist/index.d.ts
dist/index.d.ts.map
dist/moveme.json
dist/index.js
another project
suppose you're importing the project as a library somewhere else. Let's create a fake one this way:
mkdir /tmp/fooproject
cd /tmp/fooproject
yarn init -y # this creates package.json, yarn.lock, node_modules
cp -R /tmp/foolib ./node_modules/ # create a local copy
cd node_modules/foolib
tsc
now, the output of find dist
dist
dist/index.d.ts
dist/index.d.ts.map
dist/index.js
why is there no moveme.json