Webpack/打字稿要求有效,但导入不起作用 [英] Webpack / typescript require works but import doesn't
问题描述
我正在尝试将node_module导入TypeScript文件.我在我的项目结构中使用了 angular2-webpack-starter 的改编.>
I'm trying to import a node_module into a TypeScript file. I'm using an adaptation of angular2-webpack-starter for my project structure.
import ace from 'brace';
在我的一个文件中给了我
In one of my files gives me
找不到模块括号".
Cannot find module 'brace'.
但是
var ace = require('brace');
工作正常.根据此问题,我应该不会有任何问题.
Works fine. According to this issue I shouldn't be having any problems.
我的webpack配置文件是从angular2-webpack-starter复制的.我的tsconfig.json如下
My webpack config file is copied from angular2-webpack-starter. My tsconfig.json is as follows
{
"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
}
为什么一个起作用而另一个不起作用?
Why would one work but not the other?
推荐答案
答案与这有关问题.如果模块没有可用的类型数据(通过 .d.ts
文件),则ES6模块语法
The answer is related to this issue. If a module doesn't have type data available (via a .d.ts
file), the ES6 module syntax
import name from "module-name";
import * as name from "module-name";
import { member } from "module-name";
//etc
将不起作用,并且必须使用CommonJS替代方式,
will not work and one must use the CommonJS alternative,
var name = require("module-name");
这篇关于Webpack/打字稿要求有效,但导入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!