kripken / ammo.js

Direct port of the Bullet physics engine to JavaScript using Emscripten
Other
4.19k stars 559 forks source link

Cannot find module 'fs' using Webpack #298

Open interstates21 opened 4 years ago

interstates21 commented 4 years ago

I'm trying to import Ammo on next.js using https://github.com/donmccurdy/aframe-physics-system/blob/master/AmmoDriver.md

How could 'fs' be used on a none-node project? Error:

./node_modules/ammo.js/builds/ammo.wasm.js
Module not found: Can't resolve 'fs' in '/Users/oleksiikupin/Desktop/xrchat-client/node_modules/ammo.js/builds'
Error: Cannot find module '/Users/oleksiikupin/Desktop/xrchat-client/.next/build-manifest.json'
Require stack:

Next.js webpack config

 webpack(config, options) {
      config.resolve.alias.utils = path.join(__dirname, 'utils')
      // config.node = { fs: 'empty' } #doesn't compile
      config.module.rules.push({
        test: /\.(wasm)$/,
        type: 'javascript/auto',
        use: {
          loader: 'file-loader',
          options: {
            outputPath: 'assets/wasm', // set this whatever path you desire
            name: '[name]-[hash].[ext]'
          }
        }
      })
      return config
    }

Import

      require('aframe')
      require('aframe-particle-system-component')
      const Ammo = require('ammo.js/builds/ammo.wasm.js')
      const AmmoWasm = (require('ammo.js/builds/ammo.wasm.wasm')(
        window as any
      ).Ammo = Ammo.bind(undefined, {
        locateFile(path: any) {
          if (path.endsWith('.wasm')) {
            return AmmoWasm
          }
          return path
        }
      }))
      require('aframe-physics-system')
wmcmurray commented 4 years ago

@interstates21 Try adding this to your webpack config file :

node: {
    fs: 'empty'
}

I took that trick from an old project of mine, but I can't remember exaaactly what it does lol :grimacing:
....but it solves the issue ! :man_shrugging:

etiennepinchon commented 3 years ago

Hey @interstates21 were you able to find a solution to this problem? I am having the same issue :(