0
TypeScriptで記述されたバックエンドNodeJSの設定と、webpackを使用してそれをコンパイルしています。ノードjsの非jsファイルをtypesciptとwebpackの組み合わせで読み取る
テキストファイルを読み込もうとすると、source/test.txtがビルドフォルダにコピーされている間にこのエラーが発生します。
エラー:
fs.js:640
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open '/source/test.txt'
at Error (native)
at Object.fs.openSync (fs.js:640:18)
at Object.fs.readFileSync (fs.js:508:33)
マイプロジェクト構造
project
-- build
-- server
---- server.tsx
---- source
------ test.txt
server.tsx
import * as express from 'express'
import * as Path from 'path'
import * as fs from 'fs'
const app = express()
const textFile = fs.readFileSync(Path.join(__dirname, "./source/test.txt"))
app.listen(3000,() => console.log(`running on port 3000`))
$ WebPACKの& &ノードビルド/ server.js
tsconfig.json
{
"compilerOptions": {
"outDir": "./build",
"sourceMap": false,
"noImplicitAny": false,
"module": "commonjs",
"target": "es5",
"strictNullChecks": true
},
"include": [
"./server/**/*"
],
"exclude": [
"node_modules"
]
}
webpack.config.js
const CopyWebpackPlugin = require('copy-webpack-plugin')
const Path = require('path')
module.exports = {
entry: "./server/server.tsx",
target: 'node',
output: {
filename: "server.js",
path: __dirname + "/build"
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".json"]
},
module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
{ test: /\.tsx?$/, loader: "awesome-typescript-loader" },
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
]
},
plugins: [
new CopyWebpackPlugin([
{ from: Path.join(__dirname, './server/source'), to: Path.join(__dirname, './build/source') }
])
]
};
__dirname