繁体   English   中英

如何在不更改 Typescript 的基本配置的情况下从 NPM 库导入 function

[英]How to import a function from NPM library without changing base configuration of Typescript

我正在使用以下 package: https://www.npmjs.com/package/macos-release

这是我的代码:

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}`;
    }
}

当我运行代码时,我得到一个错误:

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

SyntaxError: Cannot use import statement outside a module

这是我的 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
    }
}

是否可以在不更改任何配置的情况下克服此错误? 我不想更改 tsconfig 文件,也不想更改 package.json 文件

看来这个库不支持常见的js。 它看起来像一个很小的库,所以也许您可以创建自己的实现或使用更大的 package

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM