0

I am using the following package: https://www.npmjs.com/package/macos-release

Here is my code:

import macosRelease from 'macos-release';

import { asyncExec } from '../../helpers/os-util';
import OsReleaseService from './os-release.service';

export default class OsReleaseDarwinController extends OsReleaseService {
    protected async getReleaseImpl() {
        const releaseName = macosRelease().name;

        const [releaseVersion, osBuildVersion] = await Promise.all([
            asyncExec('/usr/bin/sw_vers -productVersion').catch(() => ''),
            asyncExec('/usr/bin/sw_vers -buildVersion').catch(() => ''),
        ]);

        return `MacOS ${releaseName} ${releaseVersion} ${osBuildVersion}`;
    }
}

When I run the code, I get an error:

......../macos-release/index.js:1
import os from 'node:os';
^^^^^^

SyntaxError: Cannot use import statement outside a module

Here is my tsconfig:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "target": "ES2018",
        "module": "CommonJS",
        "lib": ["dom", "esnext"],
        "declaration": true,
        "declarationMap": true,
        "noEmit": true,
        "jsx": "react",
        "strict": false,
        "pretty": true,
        "sourceMap": true,
        "typeRoots": ["./node_modules/@types", "./@types"],
        "moduleResolution": "node",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "resolveJsonModule": true,
        "allowJs": true,
        "noImplicitAny": true,
        "skipLibCheck": true
    }
}

Is it possible to overcome this error without changing any configuration? I don't want to change tsconfig file nor package.json file

1 Answers1

0

It appears this lib doesn't support common js. It seems like a pretty small lib, so maybe you could either create your own implementation or use a larger package

DOZBORNE
  • 560
  • 3
  • 13
  • I don't even have this line in the code. It's from the package itself – Seydou Doumbia Aug 28 '22 at 08:48
  • my apologies. i didn't read that initially – DOZBORNE Aug 28 '22 at 08:57
  • potentially this might help as well https://stackoverflow.com/questions/57383049/typescript-esnext-compiler-option-destroys-es6-import-from-external-lib/57383664#57383664 – DOZBORNE Aug 28 '22 at 09:00
  • this would be the solution here @SeydouDoumbia – DOZBORNE Aug 28 '22 at 17:38
  • How so? Look at my `tsconfig.json` config file. I did provide all said in the provided thread – Seydou Doumbia Aug 29 '22 at 06:00
  • You're module is still set to `module: "commonjs"` The library you are trying to use isn't backwards compatible with older versions of node. So you would either have to convert to using "esnext" syntax or either write your own modified version of the package using commonJs syntax – DOZBORNE Aug 29 '22 at 06:06