0

NOTE: This worked until I changed type to module to fix a different problem.

I have this prebuild ts script for my Vue app:

// Adds the version as an environment variable
import { existsSync, readFileSync, writeFileSync } from 'fs';

import { version } from './package.json';

const versionVariable = 'VITE_APP_VERSION = ';
const versionRegExp = new RegExp(`^${versionVariable}.*$`, 'gm');

let envContent = existsSync(".env") ? readFileSync(".env", 'utf-8') : '';

if (envContent.match(versionRegExp)) {
  envContent = envContent.replace(versionRegExp, `${versionVariable}${version}`);
} else {
  envContent += `\n${versionVariable}${version}`;
}

writeFileSync(".env", envContent);

However, I get this error: Cannot find module './package.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.

However, I already have resolveJsonModule set:

{
  "files": [],
  "compilerOptions": {
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
  },
  "references": [
    {
      "path": "./tsconfig.node.json"
    },
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.vitest.json"
    }
  ]
}

Here is my package.json:

{
  "type": "module",
  "scripts": {
    ...
    "build": "run-p type-check build-only",
    "prebuild": "tsc prebuild.ts && node prebuild.js && rm prebuild.js"
  }
  ...
}
Jacob Miller
  • 562
  • 4
  • 16
  • Does this answer your question? [How to get the version from the package.json in Typescript?](https://stackoverflow.com/questions/41123631/how-to-get-the-version-from-the-package-json-in-typescript) – yoduh Jul 03 '23 at 17:21
  • @yoduh No, I can get the package version, I just can't run the file. – Jacob Miller Jul 03 '23 at 18:46

0 Answers0