2016-11-29 60 views
1

我无法将nodejs的核心模块加载到浏览器。如何使用webpack加载Nodejs核心模块

xlsx库取决于fs这是一个核心模块,并加载我的应用程序产生cannot find module "fs"错误。

我做了一些谷歌上搜索一再结束了here - 说我要补充target:nodewebpack.config.js文件,但是,做了他认为会产生另一个错误 - >process is not defined

我想知道是否有任何模块装载机那需要?我找不到任何。

webpack.config.js

module.exports = { 
    entry: "./app/boot", 
    output: { 
    path: __dirname, 
    filename: "./dist/bundle.js" 
    }, 
    resolve: { 
    extensions: ['', '.js', '.ts'] 
    }, 
    module: { 
    loaders: [ 
     { test: /\.ts/, loaders: ["ts-loader"], exclude: /node_modules/ } 
    ], 
    preLoaders: [ 
     // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'. 
     { test: /\.js$/, loader: "source-map-loader", exclude: ['node_modules', 'ext-*', 'lib', 'tools'] } 
    ] 
    }, 
    debug: true, 
    devtool: 'source-map' 
}; 

任何见解将大大apreciated,在此先感谢,并请让我知道如果任何其他信息将是有益的。

我不使用babel也不gulp

回答

1

你不能。

没有相当于fs,可以在浏览器中正常工作。在浏览器中执行的JavaScript出于安全原因正在沙箱中运行,并且您对客户端文件系统which is what the module fs is used for in NodeJS的访问权限非常有限。

+0

我明白了,这很有道理。那么我应该怎么做''使用'fs'来使用'xlsx'? –

+0

这取决于图书馆。你可以在没有'fs'的情况下使用它吗?通过来自服务器的HTTP请求手动加载文件?那就这样做。否则,你将不得不寻找一个不同的库。 – Timo

+0

这可能超出了问题的范围,但更确切地说,我使用'ng2-file-uploader'从客户端获取文件并使用'node-xlsx'将文件解析为对象,所有这些在没有使用服务器的客户端上,有什么建议? –

相关问题