Angular2-Seed + Typings + D3:导入错误,“找不到模块 'd3'”
Angular2-Seed + Typings + D3: Import error, 'Cannot find module 'd3''
我正在尝试导入 D3 以便我可以在 Angular2 模块中使用它。
一些背景信息:
- 我正在用 TypeScript 编写 Angular2。
- 我正在使用Angular2-seed
我做了什么:
我安装了 NPM D3 包:
npm install d3 --save
我使用 Typings 安装了 D3 类型描述,因为这就是 Angular2-Seed 用于它已经安装的库的方式。
typings install d3 --save
然后,在我的 Angular2 模块文件中,我添加了 import 语句
import * as d3 from 'd3';
结果是 TSC 给我错误信息 "Cannot find module 'd3'"。我错过了什么或做错了什么?
因此,如果在 package.json 中,您已经对 like:
有了依赖
"dependencies": {
...
"d3": "^3.5.17",
...
}
然后您可以进入 /tools/config/seed.config.ts 并添加
'd3': '${this.NPM_BASE}d3/d3.min.js' in SYSTEM_CONFIG_DEV对象,如:
protected SYSTEM_CONFIG_DEV: any = {
defaultJSExtensions: true,
packageConfigPaths: [
`${this.NPM_BASE}*/package.json`,
`${this.NPM_BASE}**/package.json`,
`${this.NPM_BASE}@angular/*/package.json`
],
paths: {
[this.BOOTSTRAP_MODULE]: `${this.APP_BASE}${this.BOOTSTRAP_MODULE}`,
'@angular/core': `${this.NPM_BASE}@angular/core/bundles/core.umd.js`,
'@angular/common': `${this.NPM_BASE}@angular/common/bundles/common.umd.js`,
'@angular/compiler': `${this.NPM_BASE}@angular/compiler/bundles/compiler.umd.js`,
'@angular/forms': `${this.NPM_BASE}@angular/forms/bundles/forms.umd.js`,
'@angular/http': `${this.NPM_BASE}@angular/http/bundles/http.umd.js`,
'@angular/router': `${this.NPM_BASE}@angular/router/index.js`,
'@angular/platform-browser': `${this.NPM_BASE}@angular/platform-browser/bundles/platform-browser.umd.js`,
'@angular/platform-browser-dynamic': `${this.NPM_BASE}@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js`,
'rxjs/*': `${this.NPM_BASE}rxjs/*`,
'd3': '${this.NPM_BASE}d3/d3.min.js',
'app/*': `/app/*`,
'*': `${this.NPM_BASE}*`
},
packages: {
rxjs: { defaultExtension: false }
}
如果有帮助请告诉我。谢谢!
我有同样的问题,上面的答案有助于调试我的解决方案 - 因为它确定这是一个配置问题,但是使用 angular2@2.1.0 我不得不更新 {root}/e2e/tscconfig .json(通过添加:
"types": [
"d3"
]
如下:
{
"compileOnSave": false,
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "commonjs",
"moduleResolution": "node",
"outDir": "../dist/out-tsc-e2e",
"sourceMap": true,
"target": "es5",
"typeRoots": [
"../node_modules/@types"
],
"types": [
"d3"
]
}
}
请记住,在 {root}/src/ 中也有一个 tscconfig.json。我对此进行了更新,但仍然存在依赖性问题:
import * as D3 from 'd3';
在我的组件中,通过错误。希望这对至少一个人有帮助!
对于Angular 5+
你可以这样做:
npm i -g typings
npm i d3@3.5.5 --save
typings install d3=github:DefinitelyTyped/DefinitelyTyped/d3/d3.d.ts#6e2f2280ef16ef277049d0ce8583af167d586c59 --global --save
我正在尝试导入 D3 以便我可以在 Angular2 模块中使用它。
一些背景信息:
- 我正在用 TypeScript 编写 Angular2。
- 我正在使用Angular2-seed
我做了什么:
我安装了 NPM D3 包:
npm install d3 --save
我使用 Typings 安装了 D3 类型描述,因为这就是 Angular2-Seed 用于它已经安装的库的方式。
typings install d3 --save
然后,在我的 Angular2 模块文件中,我添加了 import 语句
import * as d3 from 'd3';
结果是 TSC 给我错误信息 "Cannot find module 'd3'"。我错过了什么或做错了什么?
因此,如果在 package.json 中,您已经对 like:
有了依赖"dependencies": {
...
"d3": "^3.5.17",
...
}
然后您可以进入 /tools/config/seed.config.ts 并添加 'd3': '${this.NPM_BASE}d3/d3.min.js' in SYSTEM_CONFIG_DEV对象,如:
protected SYSTEM_CONFIG_DEV: any = {
defaultJSExtensions: true,
packageConfigPaths: [
`${this.NPM_BASE}*/package.json`,
`${this.NPM_BASE}**/package.json`,
`${this.NPM_BASE}@angular/*/package.json`
],
paths: {
[this.BOOTSTRAP_MODULE]: `${this.APP_BASE}${this.BOOTSTRAP_MODULE}`,
'@angular/core': `${this.NPM_BASE}@angular/core/bundles/core.umd.js`,
'@angular/common': `${this.NPM_BASE}@angular/common/bundles/common.umd.js`,
'@angular/compiler': `${this.NPM_BASE}@angular/compiler/bundles/compiler.umd.js`,
'@angular/forms': `${this.NPM_BASE}@angular/forms/bundles/forms.umd.js`,
'@angular/http': `${this.NPM_BASE}@angular/http/bundles/http.umd.js`,
'@angular/router': `${this.NPM_BASE}@angular/router/index.js`,
'@angular/platform-browser': `${this.NPM_BASE}@angular/platform-browser/bundles/platform-browser.umd.js`,
'@angular/platform-browser-dynamic': `${this.NPM_BASE}@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js`,
'rxjs/*': `${this.NPM_BASE}rxjs/*`,
'd3': '${this.NPM_BASE}d3/d3.min.js',
'app/*': `/app/*`,
'*': `${this.NPM_BASE}*`
},
packages: {
rxjs: { defaultExtension: false }
}
如果有帮助请告诉我。谢谢!
我有同样的问题,上面的答案有助于调试我的解决方案 - 因为它确定这是一个配置问题,但是使用 angular2@2.1.0 我不得不更新 {root}/e2e/tscconfig .json(通过添加:
"types": [
"d3"
]
如下:
{
"compileOnSave": false,
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "commonjs",
"moduleResolution": "node",
"outDir": "../dist/out-tsc-e2e",
"sourceMap": true,
"target": "es5",
"typeRoots": [
"../node_modules/@types"
],
"types": [
"d3"
]
}
}
请记住,在 {root}/src/ 中也有一个 tscconfig.json。我对此进行了更新,但仍然存在依赖性问题:
import * as D3 from 'd3';
在我的组件中,通过错误。希望这对至少一个人有帮助!
对于Angular 5+
你可以这样做:
npm i -g typings
npm i d3@3.5.5 --save
typings install d3=github:DefinitelyTyped/DefinitelyTyped/d3/d3.d.ts#6e2f2280ef16ef277049d0ce8583af167d586c59 --global --save