node.js - 将 Node 模块导入 typescript/systemjs

标签 node.js reactjs typescript systemjs

我正在尝试使用 react blueprint library ,所以我npm install然后我尝试导入它,如下所示:

import { Spinner } from "@blueprintjs/core"

我得到system.src.js:1051 GET http://localhost:8888/@blueprintjs/core 404 (Not Found)

我认为这与打字有关,因为有 ts我尝试过的模块上的文件

/// <reference path="../node_modules/@blueprintjs/core/dist/index.d.ts" />

/// <reference path="node_modules/@blueprintjs/core/dist/index.d.ts" />

我仍然遇到同样的错误。 我是 Typescript 新手,如何使用这样的 Node 模块?

最佳答案

您需要配置 SystemJS,以便它可以在浏览器中查找并加载所有必需的模块。请参阅this answer作一般性解释。这是创建蓝图旋转器的完整最小示例:

安装先决条件

npm i @blueprintjs/core
npm i react
npm i react-dom
npm i react-addons-css-transition-group
npm i @types/react
npm i @types/react-dom
npm i @types/dom4
npm i typescript
npm i systemjs

test.tsx 中的示例代码

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { Spinner } from "@blueprintjs/core";

const mySpinner = <Spinner/>;

ReactDOM.render(mySpinner, document.body);

示例网页

<!doctype html>
<html>
<head>

<link href="node_modules/@blueprintjs/core/dist/blueprint.css" rel="stylesheet" />

<script src="node_modules/systemjs/dist/system.src.js"></script>

<script>
    window.process = { env: {}};
    System.config({
        map: {
            'react': 'node_modules/react',
            'react-dom': 'node_modules/react-dom',
            'react-addons-css-transition-group': 'node_modules/react-addons-css-transition-group/index.js',
            'fbjs': 'node_modules/fbjs',
            'tether': 'node_modules/tether/dist/js/tether.js',
            'dom4': 'node_modules/dom4/build/dom4.max.js',
            '@blueprintjs/core': 'node_modules/@blueprintjs/core/dist',
            'classnames': 'node_modules/classnames/index.js',
            'object-assign': 'node_modules/object-assign/index.js',
            'pure-render-decorator': 'node_modules/pure-render-decorator/index.js'
        },
        packages: {
            'react': { main: 'lib/React.js' },
            'react-dom': { main: 'lib/ReactDOM.js' },
            'fbjs': {},
            '@blueprintjs/core': { main: 'index.js' },
            '@blueprintjs/core/common': { main: 'index.js' },
            '@blueprintjs/core/components': { main: 'index.js' }
        }
    });
    System.import('./test.js').then(function(t) {
    }).catch(function(e) {
        console.error(e);
    });
</script>


</head>
<body>

</body>
</html>

注意:看起来 SystemJS 无法使用 node_modules/react/dist/react.jsnode_modules/react-dom/dist/react-dom.js 中提供的包加载 React 和 React-dom。但是,它可以从 node_modules/react/libnode_modules/react-dom/lib 的各个源文件中加载所有内容,前提是您在浏览器中定义了 process 变量。

关于node.js - 将 Node 模块导入 typescript/systemjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40697587/

相关文章:

reactjs - Rx fromPromise 无法将 then 识别为函数?

node.js - NestJS 中同一 Controller (路由别名)的两个端点

node.js - 使用 vim 和 CoffeeScript 的 Node 检查器

javascript - 使用 webpack 目标 Node 从 node_modules 导入 css

node.js - 使用路径参数定义多个 Express.js 路由

javascript - 无法为 typescript 字典赋值

reactjs - 在社交媒体上分享,URL 不呈现任何元数据

angularjs - 使用 Angularjs 上传图像

javascript - 从AngularJS到Flux- react 方式

javascript - 其余参数在我的 react 引导组件中产生错误?