Using Typescript 2.9 I am able to successfully import the local package.json file and use the values. However I now have the issue where the package.json file is included in the compiled output, when I just want the resulting JS files to use the actual package.json.
My package has the following files:
src/
index.ts
package.json
tsconfig.json
My index.ts imports the package.json:
import pkg = require("../package.json");
My tsconfig.json looks like this:
{
"compilerOptions": {
"outDir": "./lib",
"module": "commonjs",
"resolveJsonModule": true,
"esModuleInterop": true
},
"include": [
"src/**/*"
]
}
Ideally the result would be:
lib/
index.js
package.json
However what I actually get is:
lib/
src/
index.js
package.json
package.json
Typescript should be able to do it as follows: import * as pack from "../package. json" // access name and version like this: console. log(pack.name);
In the TypeScript file which is to be imported must include an export form and the main file where the class is imported must contain an import form, by which TypeScript can identify the file which is used.
tsconfig include The include property specifies the files to be included in the TypeScript project. You can either include files or file paths like ./src/** to specify what should be included.
🚩 This approach won't work for ESM targets. If you trying to create an ES6 Module, you'll have to use a different solution.
I am using TypeScript 3.5.1 and if I import the package.json with the require()
function then it is not copied over to the compile output so the output looks like your ideal result.
My tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2015", "dom"],
"sourceMap": true,
"strict": true,
"moduleResolution": "node",
"downlevelIteration": true
"outDir": "./ts-out",
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": false
},
"include": [
"./src"
]
}
And I just import it like this:
const pkg = require('../package.json');
I have found this solution in Facebook's Jest framework source code (they are using Lerna + TypeScript): https://github.com/facebook/jest/blob/master/packages/jest-core/src/version.ts
You want a few somewhat contradictory things:
package.json
, meaning you are treating it as sourceoutDir
as is done for sourcerootDir
point to the project root rather than the src
root, which is the only way you can get the import statement to work.These contradictions won't be contradictions if you logically break your project into separate sub-projects. You will need Typescript 3's new Project References feature.
We'll treat the src
directory and the root directory containing package.json
as separate projects. Each will have its own tsconfig
file:
Give the src
dir its own project.
./src/tsconfig.json
:
{
"compilerOptions": {
"rootDir": ".",
"outDir": "../lib/",
"resolveJsonModule": true
},
"references": [ // this is how we declare a dependency from
{ "path": "../" } // this project to the one at the root dir`
]
}
Give the root dir its own project.
./tsconfig.json
:
{
"compilerOptions": {
"rootDir": ".",
"outDir": ".", // if out path for a file is same as its src path, nothing will be emitted
"resolveJsonModule": true,
"composite": true // required on the dependency project for references to work
},
"files": [ // by whitelisting the files to include, TS won't automatically
"package.json" // include all source below root, which is the default.
]
}
Because for the root sub-project's output is the self-same directory, and because the source path and output path for package.json
end up the same, tsc
won't do anything.... effectively a no-op.
run tsc --build src
and voilà ! The result will be:
lib/
index.js
package.json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With