我正在尝试将 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/