2017-04-01 1 views
0

TypeScript로 작성되고 webpack을 사용하여 백엔드 NodeJS 설정을 컴파일했습니다.typescipt와 webpack의 조합으로 노드 js의 non-js 파일 읽기

텍스트 파일을 읽으려고 할 때 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`)) 

$ 웹팩 & & 노드 구축/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') } 
     ]) 
    ] 
}; 

답변

1

__dirname/ 웹팩 의해 설정되고있다. 현재 /source/test.txt을 찾으려고하는데 오류 메시지가 표시되면 파일 시스템의 루트입니다. 당신은 주입하지 웹팩 말할 수는 정확하게 Node.js를 사용합니다, 그래서 falsenode.dirname을 설정하여 __dirname__dirname :

node: { 
    __dirname: false 
}