【问题标题】:Node.js crypto gives webpack compile errorNode.js 加密给 webpack 编译错误
【发布时间】:2018-01-28 12:40:24
【问题描述】:

我正在使用 Node.js、react、yarn 和 webpack 开发一个应用程序,但我对这些技术不是很有经验。

我需要使用加密,我尝试使用内置 Crypto 模块。问题是即使在我的 js 文件中使用简单的命令:crypto = require('crypto');,webpack 编译也会失败并显示以下消息:

@ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/index.js

ERROR in ./node_modules/parse-asn1/aesid.json
Module build failed: SyntaxError: Unexpected token, expected ; (1:25)

> 1 | {"2.16.840.1.101.3.4.1.1": "aes-128-ecb",
    |                          ^
  2 | "2.16.840.1.101.3.4.1.2": "aes-128-cbc",
  3 | "2.16.840.1.101.3.4.1.3": "aes-128-ofb",
  4 | "2.16.840.1.101.3.4.1.4": "aes-128-cfb",

我曾尝试使用其他加密库,例如 node-simple-encryptor 和 sjcl,但我总是遇到同样的错误!

我猜应该是webpack(3.5.2版)的问题,因为当我在一个测试javascript文件中使用相同的库并从nodejs交互环境运行时,它运行正常。

谁能帮帮我?

我的 webpack.config.js 文件:

const path = require('path');
module.exports = {
   entry: path.resolve(__dirname, 'src', 'index.js'),
   output: {
      path: path.resolve(__dirname, 'output'),
      filename: 'bundle.js',
      publicPath: '/'
   },
   resolve: {
      extensions: ['.js', '.jsx']
   },
   module: {
      rules: [
         {
             test: /\.js/,
             use: {
                loader: 'babel-loader',
                options: { presets: ['react', 'es2015'] }
             }
         },
         {
            test: /\.scss/,
            use: ['style-loader', 'css-loader', 'sass-loader']
         }
      ]
   },
   devServer: {
     contentBase: './src',
     publicPath: '/output',
     historyApiFallback: true,
   },
   devtool: 'source-map' //Maps source files with bundle.js for debugging in the browser

};

【问题讨论】:

    标签: node.js webpack cryptography


    【解决方案1】:

    我认为问题在于 babel / webpack 将该 JSON 文件视为 js 尝试使用这是第一个 module.rules 测试键 test: /\.(js)$/, 而不是 test: /\.js/,

    【讨论】:

    • 或者只是/\.js$/(或者可能/\.jsx?$/,如果他们也使用JSX文件)
    • 就是这样,非常感谢!它适用于 /\.(js)$/,
    猜你喜欢
    • 2012-04-19
    • 2017-08-21
    • 2016-10-20
    • 2023-03-13
    • 2016-07-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2020-09-08
    相关资源
    最近更新 更多