我在 webpack 捆绑后遇到了动态需要 js 文件的问题。
环境:
webpack、ts-loader、打字稿。
src/index.ts:
require(path.resolve(__dirname, './test.js'));
dist/test.js:
console.log('I should be printed after require @ index');
我不知道为什么,但是 webpack 认为没有文件:
1) 运行时警告webpack -p
WARNING in ./src/index.ts
5:0-43 Critical dependency: the request of a dependency is an expression
@ ./src/index.ts
2)运行脚本时出错:
Error: Cannot find module "C:\Users\user\path\to\dist\test.js".
3)我的 webpack 配置是:
const nodeExternals = require('webpack-node-externals');
module.exports = {
entry: {
index: "./src/index.ts"
},
output: {
filename: "[name].js"
},
target: "node",
externals: [ nodeExternals() ],
node: {
"__dirname": false
},
resolve: {
extensions: [".ts", ".tsx", ".js"]
},
module: {
rules: [
{ test: /\.tsx?$/, loader: "ts-loader" }
]
}
}
预期的:
NodeJS 只是在 index.js 脚本执行时动态地需要路径。
请帮助正确设置。
谢谢!