我已经使用angular 5.2.10设置了一个角度通用应用程序。当我尝试使用angular cli和webpack为server.ts构建它时,似乎找不到我的模块。
什么有效:
ng build -prod --build-optimizer --app 0
&安培;&安培;
ng build --aot --app 1 --output-hashing=false
什么不是:
webpack --config webpack.config.js --progress --colors
错误:
ERROR in /src/app/util/wnumb.ts
[tsl] ERROR in /src/app/util/wnumb.ts(1,26)
TS2307: Cannot find module '@angular/core'.
ERROR in /src/app/terms-and-conditions/terms-and-conditions.component.ts
[tsl] ERROR in /src/app/terms-and-conditions/terms-and-
conditions.component.ts(1,27)
TS2307: Cannot find module '@angular/core'.
ERROR in /src/app/terms-and-conditions/terms-and-conditions.component.ts
[tsl] ERROR in /src/app/terms-and-conditions/terms-and-
conditions.component.ts(2,24)
TS2307: Cannot find module '@angular/router'.
我的webpack.config.js文件:
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: { server: './src/server.ts' },
resolve: { extensions: ['.js','.ts'] },
target: 'node',
// this makes sure we include node_modules and other 3rd party libraries
externals: [/(node_modules|main\..*\.js)/],
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js'
},
module: {
rules: [{ test: /\.ts$/, loader: 'ts-loader' }]
},
plugins: [
new webpack.ContextReplacementPlugin(
/(.+)?angular(\\|\/)core(.+)?/,
path.join(__dirname, 'src'), // location of your src
{} // a map of your routes
),
new webpack.ContextReplacementPlugin(
/(.+)?express(\\|\/)(.+)?/,
path.join(__dirname, 'src'),
{}
)
]
};
我的tsconfig.json:
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"mapRoot": "./src/",
"removeComments": true,
"sourceMap": true,
"target": "es2017",
"typeRoots": [
"./node_modules/@types"
]
}
}
注意:非常感谢任何帮助。
答案 0 :(得分:1)
如果有其他人遇到此问题,看起来像是将以下内容添加到我的tsconfig.json文件中修复它:
"moduleResolution": "node",
tsconfig.json:
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"mapRoot": "./src/",
"moduleResolution": "node",
"removeComments": true,
"sourceMap": true,
"target": "es2017",
"typeRoots": [
"./node_modules/@types"
]
}
}