ashtuchkin / iconv-lite

Convert character encodings in pure javascript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ERROR in ./node_modules/safer-buffer/safer.js

shuangovo opened this issue · comments

when I require iconv-lite and build ,I get the error,

ERROR in ./node_modules/safer-buffer/safer.js 5:13-30
Module not found: Error: Can't resolve 'buffer' in 'C:\Users\007\Desktop\mmd-play\node_modules\safer-buffer'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "buffer": require.resolve("buffer/") }'
        - install 'buffer'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "buffer": false }
resolve 'buffer' in 'C:\Users\007\Desktop\mmd-play\node_modules\safer-buffer'
  Parsed request is a module
  using description file: C:\Users\007\Desktop\mmd-play\node_modules\safer-buffer\package.json (relative path: .)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      C:\Users\007\Desktop\mmd-play\node_modules\safer-buffer\node_modules doesn't exist or is not a directory
      C:\Users\007\Desktop\mmd-play\node_modules\node_modules doesn't exist or is not a directory
      looking for modules in C:\Users\007\Desktop\mmd-play\node_modules
        single file module
          using description file: C:\Users\007\Desktop\mmd-play\package.json (relative path: ./node_modules/buffer)
            no extension
              C:\Users\007\Desktop\mmd-play\node_modules\buffer doesn't exist
            .js
              C:\Users\007\Desktop\mmd-play\node_modules\buffer.js doesn't exist
            .json
              C:\Users\007\Desktop\mmd-play\node_modules\buffer.json doesn't exist
            .wasm
              C:\Users\007\Desktop\mmd-play\node_modules\buffer.wasm doesn't exist
        C:\Users\007\Desktop\mmd-play\node_modules\buffer doesn't exist
      C:\Users\007\Desktop\node_modules doesn't exist or is not a directory
      C:\Users\007\node_modules doesn't exist or is not a directory
      C:\Users\node_modules doesn't exist or is not a directory
      C:\node_modules doesn't exist or is not a directory
 @ ./node_modules/iconv-lite/lib/index.js 5:13-43
 @ ./src/component/model-upload.js 65:12-33
 @ ./src/views/contribute/index.js 70:0-55 303:42-53
 @ ./src/router/index.js
 @ ./src/App.js 39:0-39 57:42-51
 @ ./src/index.js 3:0-24 4:50-53

so I add resolve.fallback: { "buffer": require.resolve("buffer/") } to webpack.config.js,but

[webpack-cli] Failed to load 'C:\Users\007\Desktop\mmd-play\webpack.config.js' config
[webpack-cli] Error: Cannot find module 'buffer/'
Require stack:
- C:\Users\007\Desktop\mmd-play\webpack.config.js
- C:\Users\007\Desktop\mmd-play\node_modules\webpack-cli\lib\webpack-cli.js
- C:\Users\007\Desktop\mmd-play\node_modules\webpack-cli\lib\bootstrap.js
- C:\Users\007\Desktop\mmd-play\node_modules\webpack-cli\bin\cli.js
- C:\Users\007\Desktop\mmd-play\node_modules\webpack\bin\webpack.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15)
    at Function.resolve (C:\Users\007\Desktop\mmd-play\node_modules\v8-compile-cache\v8-compile-cache.js:164:23)
    at Object.<anonymous> (C:\Users\007\Desktop\mmd-play\webpack.config.js:25:31)
    at Module._compile (C:\Users\007\Desktop\mmd-play\node_modules\v8-compile-cache\v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (C:\Users\007\Desktop\mmd-play\node_modules\v8-compile-cache\v8-compile-cache.js:159:20)
    at WebpackCLI.tryRequireThenImport (C:\Users\007\Desktop\mmd-play\node_modules\webpack-cli\lib\webpack-cli.js:32:22) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\Users\\007\\Desktop\\mmd-play\\webpack.config.js',
    'C:\\Users\\007\\Desktop\\mmd-play\\node_modules\\webpack-cli\\lib\\webpack-cli.js',
    'C:\\Users\\007\\Desktop\\mmd-play\\node_modules\\webpack-cli\\lib\\bootstrap.js',
    'C:\\Users\\007\\Desktop\\mmd-play\\node_modules\\webpack-cli\\bin\\cli.js',
    'C:\\Users\\007\\Desktop\\mmd-play\\node_modules\\webpack\\bin\\webpack.js'
  ]
}

I honestly don't know what's going on here. Maybe you can ask in safer-buffer package or webpack?

There are some old pr/issues attempting to remove safe-buffer and replace buffer with uint8array... this would be a good thing

From the error log, this is not sure caused by iconv-lite, but may be caused by webpack,or the version of iconv-lite.
by the way,it is recommended that you make a JSFiddle/JSBin/Codepen/CodeSandbox to demonstrate your issue.

@yosion-p It was build success in CodeSandbox.I used the webpack template there.I found it just have one more config about babel,it's '@babel/plugin-transform-runtime'

oh,It was build success just for its webpack version is 4, I got the error in version 5

oh,It was build success just for its webpack version is 4, I got the error in version 5

I would say that it was caused by webpack5.Some people have encountered this problem in Google.

The problem is resolved.I'm stupid,I always thought the error Error: Cannot find module 'buffer/' ask me to npm i buffer/ ,however,the correct is npm i buffer.So in webpack5,we should npm i buffer and add a config into webpack.config.js

resolve: {
  fallback: {
    'buffer': require.resolve('buffer/')
  }
}