私はPostgresまで私のアプリケーションをフックするためにノードpostgresを使用しようとしています。Webpackは__dirnameを使用できません。
import React from 'react';
import pg from 'pg';
import fs from 'fs';
var cn = {
host: 'localhost', // server name or IP address;
port: 5432,
database: 'my_db',
user: 'myname',
password: 'mypass'
};
、それはエラーを生成します。私が使用コードは
index.js:5 Uncaught ReferenceError: __dirname is not defined
up @ index.js:5
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function) @ client.js:4
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function) @ index.js:3
console.EventEmitter._events @ index.js:81
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function) @ app.js:26957
__webpack_require__ @ bootstrap 539ecc7…:19
content @ bootstrap 539ecc7…:39
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function) @ bootstrap 539ecc7…:39
(anonymous function) @ bootstrap 539ecc7…:39
webpackUniversalModuleDefinition @ universalModuleDefinition:7
(anonymous function) @ universalModuleDefinition:10
コンソールに設けindex.js上のパスはWebPACKのである。///./~/pg/~/ pgpass/lib/index.js:5
@Renzo Poddigheソリューションをhow to write file with node webkit js?で試しましたが、同じエラーが表示されます。私は、それがhttps://github.com/nwjs/nw.js/wiki/Differences-of-JavaScript-contexts#resolving-relative-paths-to-other-scriptsとhttps://github.com/nwjs/nw.js/issues/264の議論と関係していると思います。彼らは
// __dirname is not defined in webkit context, this is only node.js thing
console.log(__dirname); // undefined
と
__dirnameが必要と呼ばれる()されたJavaScriptコードでのNode.jsモジュール、すなわちで動作することを言います。 __dirnameは、WebKitスクリプト、つまりHTMLで呼び出されたJavaScriptコード、またはjQueryの$ .getScript()などの他の同様のメソッドでのみ機能します。
アイデア?私が含める必要がある他の情報を教えてください。
編集
私は私の目標は、私のwebpack.config.jsがどのように見える
var config = {
entry: { app: './src/index.jsx'},
output: {
libraryTarget: 'umd',
path: path.join(__dirname, 'dist'),
filename: '[name].js'
}, ...
だと思います。その場合は
...
node: {
console: true,
__dirname: true,
dns: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty'
}
...
あなたのウェブパックのターゲットは何ですか? web/nodeまたは他の何かhttps://webpack.github.io/docs/configuration.html#target – thangngoc89
私が見る 'target'の唯一のものは' libraryTarget: 'umd'、 'です。これを明示的に設定すべきですか?ドキュメントへのリンクによれば、私のターゲットはデフォルトでウェブであるようです。 – neallred
webpackの設定で 'target: 'node''を指定しようとしてください。 –