gpt4 book ai didi

Angular 4 : "Cannot find name ' require'

转载 作者:太空狗 更新时间:2023-10-29 16:54:48 27 4
gpt4 key购买 nike

我正在使用 Angular 4webpack 构建应用。

我的一个组件中包含以下内容:

ngOnInit() {
require('/assets/js/regular-expresions.js');
}

当我尝试编译时,我得到:

ERROR in C:/SRC/Sandbox/eat-sleep-code.com/src/app/content.component.ts (21,9): Cannot find name 'require'.

我已经运行了 npm install @types/node --save-dev 并将我的 tsconfig.json 更新为如下所示:

{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"types": [
"node"
],
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2016",
"dom"
]
}
}

但是,唉,同样的错误不断被抛出。有什么想法吗?


这是我的 package.json:

{
"name": "eat-sleep-code.com",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^4.0.1",
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/forms": "^4.0.0",
"@angular/http": "^4.0.0",
"@angular/material": "^2.0.0-beta.3",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
"@types/node": "^6.0.69",
"angularfire2": "^2.0.0-beta.8",
"core-js": "^2.4.1",
"firebase": "^3.7.8",
"jquery": "^3.2.1",
"jquery-validation": "^1.16.0",
"ng2-gist": "^1.0.0",
"promise-polyfill": "^6.0.2",
"requirejs": "^2.3.3",
"rxjs": "^5.1.0",
"typescript": "^2.2.2",
"web-animations-js": "^2.2.4",
"webpack": "^2.4.1",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@angular/cli": "1.0.0",
"@angular/compiler-cli": "^4.0.0",
"@types/jasmine": "2.5.38",
"@types/node": "^6.0.69",
"codelyzer": "~2.0.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.0.0",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^0.2.0",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"tslint": "~4.5.0",
"typescript": "^2.2.2",
"webpack": "^2.4.1"
}
}

最佳答案

尝试在顶部添加 declare var require: any;

关于 Angular 4 : "Cannot find name ' require',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43476135/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com