import - Webpack/typescript 需要工作但导入不

标签 import typescript webpack

我正在尝试将 node_module 导入 TypeScript 文件。我正在使用 angular2-webpack-starter 的改编版对于我的项目结构。

import ace from 'brace';

在我的一个文件中给我

Cannot find module 'brace'.

但是

var ace = require('brace');

工作正常。根据this issue我不应该有任何问题。

我的 webpack 配置文件是从 angular2-webpack-starter 复制的。 我的tsconfig.json如下

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true
  },
  "files": [
    "src/bootstrap.ts",
    "src/vendor.ts",
    "src/polyfills.ts"
  ],
   "filesGlob": [
      "./src/**/*.ts",
      "!./node_modules/**/*.ts"
   ],
   "compileOnSave": false,
   "buildOnSave": false
}

为什么一个有效而另一个无效?

最佳答案

答案与this issue有关.如果模块没有可用的类型数据(通过 .d.ts 文件),ES6 模块语法

import name from "module-name";
import * as name from "module-name";
import { member } from "module-name";
//etc

将不起作用,必须使用 CommonJS 替代方案,

var name = require("module-name");

关于import - Webpack/typescript 需要工作但导入不,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34563578/

相关文章:

php - 使用 php 脚本导入 Mysql xlsx 文件 大文件(以 MB 为单位)

java - 如何测试我的Android应用程序是否正在接收数据?

javascript - 试图将 "zone.js"编译为外部模块,但它看起来像一个全局模块

javascript - 将 firestore DB 设置为窗口对象有多安全?

reactjs - 如何配置 package.json 来运行 eslint 脚本

import - 来自 CocoaPods 的 Google 分析和 @import 错误

javascript - 未使用的 ES6 模块会影响性能吗?

javascript - 如何使图标与 Angular 5/6 中的按钮类似?

typescript - 棱镜 : how can I get all children on a condition?

javascript - 为什么 'npm install canvas'没有创建javascript文件?