簡體   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