Javascript 为什么我不能使用SystemJS直接从node_模块导入?

Javascript 为什么我不能使用SystemJS直接从node_模块导入?,javascript,angular,typescript,systemjs,Javascript,Angular,Typescript,Systemjs,虽然SystemJS上有很多and,但我仍然不理解导入语法。 具体来说,为什么typescript不能使用以下代码找到ng boostrap.js: import { createPlatform } from '../../node_modules/@ng-bootstrap/ng-bootstrap/bundles/ng-bootstrap', 直接导入文件,但此代码有效: import {createPlatform } from './node_modules/@angular/cor

虽然SystemJS上有很多and,但我仍然不理解导入语法。 具体来说,为什么typescript不能使用以下代码找到
ng boostrap.js

import { createPlatform } from '../../node_modules/@ng-bootstrap/ng-bootstrap/bundles/ng-bootstrap',
直接导入文件,但此代码有效:

import {createPlatform } from './node_modules/@angular/core/bundles/core.umd.js';
其中
systemjs.config.js
中的my
map
包含以下行:

'@angular/core': 'npm:@angular/core/bundles/core.umd.js'.

为什么我不能使用systemJS直接从
节点\u模块导入?

注意:尽管下面的解决方案有效,但有些信息是不正确的。请参阅下面评论中的讨论。

首先,TypeScript对JS文件一无所知。它知道如何生成它们,但不知道如何针对它们进行编译。所以我不知道你是怎么得到的

import {createPlatform } from './node_modules/@angular/core/bundles/core.umd.js';
在TypeScript代码中编译

我们能够做到

import {createPlatform } from '@angular/core';
在TypeScript中,因为TypeScript已经在
节点\u模块中查找。而
@angular/core
,如果您查看
节点_模块
,则目录为
@angular/core
,带有
索引.d.ts
文件。这是我们的TypeScript代码编译所依据的文件,而不是JS文件。JS文件(上面第一个代码段中的那个)仅在运行时使用。TypeScript应该对该文件一无所知

使用上面的第二个代码段,当TypeScript编译成JS时

var createPlatform = require('@angular/core').createPlatform;
在运行时,SystemJS看到这一点,然后查看
映射
配置,将
@angular/core
映射到绝对文件位置,并能够加载该文件

'@angular/core': 'npm:@angular/core/bundles/core.umd.js'
这是ng引导应该遵循的模式。使用指向TypeScript定义文件的导入,以便它可以编译

import { ... } from '@ng-bootstrap/ng-bootstrap';
如果查看
node\u modules/@ng bootstrap/ng bootstrap
目录,应该会看到
index.d.ts
文件。这就是TypeScript将用来编译的对象。当它被编译成JS时,它被编译成如下

var something = require('@ng-bootstrap/ng-bootstrap').something;
在SystemJS配置中,我们需要将
@ng bootstrap/ng bootstrap
映射到模块文件的绝对路径,否则SystemJS将不知道如何解析它

'@ng-bootstrap/ng-bootstrap': 'npm:@ng-bootstrap/ng-bootstrap/bundles/ng-bootstrap.js'

其中一个关键的收获是理解编译时和运行时之间的区别。编译类型是TypeScript,它对JS文件一无所知,因为这些是运行时文件。SystemJS是需要了解运行时(JS)文件的人。

除了开头和结尾,您的答案几乎是正确的。如果您指定
“allowJs”:true
typescript将直接将此类导入解析为JavaScript文件,但它更喜欢
.d.ts
.ts
文件(如果存在)。@AluanHaddad我想我不知道所有事情:-)@AluanHaddad我假设如果导入JS文件,就会丢失编译时键入。对吗?不,不对。JavaScript文件将由相同的语言服务进行解析,类型推断将完全生效,并由适用的JSDoc注释进行补充,以提供尽可能多的类型信息。主要区别在于,在JS文件中,推理失败不会导致类型错误。混合使用JS和TS项目是完全可行的。我想投票支持这个答案,你的问题是对的,但问题不是TypeScript理解一种文件,SystemJS理解另一种,实际上这两种工具都理解两种类型的文件。问题在于TypeScript和SystemJS使用不同的解析算法。