webpack 不能使用 __dirname 吗? [英] Webpack can not use __dirname?

查看:56
本文介绍了webpack 不能使用 __dirname 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 node-postgres 将我的应用程序连接到 Postgres.我使用的代码是:

I am trying to use node-postgres to hook my app up to Postgres. The code I use is:

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

The path on index.js provided in the console is webpack:///./~/pg/~/pgpass/lib/index.js:5

我在 how to write file with node 尝试了@Renzo Poddighe 解决方案webkit js? 但我仍然遇到同样的错误.我认为这可能与 https://github.com/nwjs/nw.js/wiki/Differences-of-JavaScript-contexts#resolving-relative-paths-to-other-scriptshttps://github.com/nwjs/nw.js/issues/264.他们说

I tried @Renzo Poddighe solution at how to write file with node webkit js? but I still get the same error. I think it may have something to do with the discussions at https://github.com/nwjs/nw.js/wiki/Differences-of-JavaScript-contexts#resolving-relative-paths-to-other-scripts and https://github.com/nwjs/nw.js/issues/264. They say that

// __dirname is not defined in webkit context, this is only node.js thing
console.log(__dirname); // undefined

__dirname 在 Node.js 模块中工作,即在用 require() 调用的 JavaScript 代码中.__dirname 不仅在 WebKit 脚本中起作用,即在使用 HTML 调用的 JavaScript 代码中,或 jQuery 的 $.getScript() 或任何其他类似方法中.

__dirname works in Node.js modules, i.e. in JavaScript code that was called with require(). __dirname doesn't work only in WebKit scripts, i.e. in JavaScript code that was called with HTML , or jQuery's $.getScript(), or any other similar method.

有什么想法吗?让我知道我需要包含哪些其他信息.

Any ideas? Let me know what other information I need to include.

编辑

我想我的目标是

var config = {
  entry: { app: './src/index.jsx'},
  output: {
     libraryTarget: 'umd',
     path: path.join(__dirname, 'dist'),
     filename: '[name].js'
  }, ...

我的 webpack.config.js 看起来像:

My webpack.config.js looks like:

...
  node: {
    console: true,
    __dirname: true,
    dns: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
  }
...

推荐答案

在这种情况下,将其添加到您的 webpack 配置中:

In that case, add this to your webpack config:

{
  node: {
    __dirname: true
  }
}

这将告诉 webpack 用模块的路径替换 __dirname 实例.此路径相对于 context

This will tell webpack to replace __dirname instances with the path of the module. This path relative to context

这篇关于webpack 不能使用 __dirname 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆