decentraland / sdk-issues

issues, features requests and discussions related to sdk.
4 stars 4 forks source link

Error: Cannot find module 'fs' #3

Closed dexploneer closed 3 years ago

dexploneer commented 6 years ago

hello guys, i've started a very simple project with decentraland sdk but if i try to import a native module like 'fs' i get this error.

screenshot from 2018-07-17 23-17-18

i see errors also with other modules but at moment i think this is the strangest. because it is a native module.

my scene.tsx code is this

import { createElement, ScriptableScene } from 'metaverse-api'

const fs = require('fs');

console.log(fs);

export default class BasicViewer extends ScriptableScene<any, { texture: string }> {
  state = { texture: "" };

  sceneDidMount() {  

  }

  sceneWillUnmount() {
  }

  async render() {
    return (
      <scene>
        <material id="screen_texture_material" albedoTexture={ this.state.texture } roughness={1} />

        <box position={{ x: 5, y: 0, z: 5 }} color="#D4AF37" scale={{ x: 2, y: 6, z: 1 }}>
          <plane id="screen" position={{ x: 0, y: 0, z: -0.51 }} material="#screen_texture_material" scale={{ x: 1, y: 1, z: 1 }}/>
        </box>
      </scene>
    )
  }
}

my package.json

{
  "name": "dcl-project",
  "version": "1.0.0",
  "description": "My new Decentraland project",
  "scripts": {
    "build": "metaverse-compiler build.json",
    "watch": "metaverse-compiler build.json --watch"
  },
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "metaverse-api": "^4.0.0",
    "typescript": "^2.7.2"
  }
}

what wrong? thanks in advance

matiasbargas commented 6 years ago

fs is a node module. The scene is running in a web worker in a browser.

If you want your scene to run in a server you should use a remote scene. Here is an example of it https://github.com/decentraland/sample-scene-server

There, on the server, you can use fs.

dexploneer commented 6 years ago

thats true. and what about main elements like window? if i use this simple code

sceneDidMount() {
     console.log(window);
  }

i get the error 'window is not defined'

dexploneer commented 6 years ago

guys, i understand that the scene run in a web worker but in this webworker there isnt direct access to DOM object. If for example i install web3 following the tutorial guide

https://docs.decentraland.org/sdk-reference/blockchain-operations/

i receive this error

Uncaught Error: EventDispatcher: Unhandled "error" event at _.emit (preview.js:8) at transport.onError.transport.onError.e (preview.js:8) at Worker.e.addEventListener.e (preview.js:8) emit @ preview.js:8 transport.onError.transport.onError.e @ preview.js:8 e.addEventListener.e @ preview.js:8 Uncaught ReferenceError: window is not defined

finally the problem is this

Uncaught ReferenceError: window is not defined

matiasbargas commented 6 years ago

We are working on fixing the docs, thanks for the feedback.

belohlavek commented 6 years ago

Please make sure you:

import Web3 = require("web3");
const provider = await this.getEthereumProvider();
const web3 = new Web3(provider);

And that you are using the correct web3 version:

"dependencies": {
    "web3": "^0.20.6"
}