Reco-X Reco-X - 1 month ago 18
Node.js Question

Install npm dependencies webtorrent - Error: Can't resolve 'fs' - Fountain-Js (Yeoman)

Hello Im in trouble trying to install a NPM Dependencie in my code.

I installed the module with this command:
npm install --save webtorrent

This is my package.json

./package.json

{
"dependencies": {
"angular": "^1.5.0",
"angular-ui-router": "1.0.0-beta.1",
"webtorrent": "^0.97.2"
},
"devDependencies": {
"angular-mocks": "^1.5.0-beta.2",
"gulp-angular-templatecache": "^1.8.0",
"del": "^2.0.2",
"gulp": "gulpjs/gulp#4ed9a4a3275559c73a396eff7e1fde3824951ebb",
"gulp-hub": "frankwallis/gulp-hub#d461b9c700df9010d0a8694e4af1fb96d9f38bf4",
"gulp-filter": "^4.0.0",
"gulp-util": "^3.0.7",
"gulp-angular-filesort": "^1.1.1",
"gulp-htmlmin": "^1.3.0",
"gulp-insert": "^0.5.0",
"gulp-ng-annotate": "^1.1.0",
"gulp-sass": "^2.1.1",
"browser-sync": "^2.9.11",
"browser-sync-spa": "^1.0.3",
"karma": "^1.3.0",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.0.2",
"karma-junit-reporter": "^1.1.0",
"jasmine": "^2.4.1",
"es6-shim": "^0.35.0",
"karma-angular-filesort": "^1.0.0",
"karma-ng-html2js-preprocessor": "^0.2.0",
"karma-phantomjs-launcher": "^1.0.0",
"karma-phantomjs-shim": "^1.1.2",
"phantomjs-prebuilt": "^2.1.6",
"babel-plugin-istanbul": "^2.0.1",
"karma-webpack": "^1.7.0",
"webpack": "2.1.0-beta.20",
"html-webpack-plugin": "^2.9.0",
"style-loader": "^0.13.0",
"css-loader": "^0.23.1",
"postcss-loader": "^0.8.0",
"autoprefixer": "^6.2.2",
"json-loader": "^0.5.4",
"extract-text-webpack-plugin": "^2.0.0-beta.3",
"ng-annotate-loader": "^0.0.10",
"html-loader": "^0.4.3",
"sass-loader": "^3.1.2",
"node-sass": "^3.4.2",
"eslint": "^3.2.2",
"eslint-config-xo-space": "^0.12.0",
"eslint-config-angular": "^0.5.0",
"eslint-plugin-angular": "^1.3.0",
"eslint-loader": "^1.3.0",
"babel-loader": "^6.2.0",
"babel-eslint": "^6.0.2",
"eslint-plugin-babel": "^3.1.0",
"babel-core": "^6.13.0",
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.2.0"
},
"scripts": {
"build": "gulp",
"serve": "gulp serve",
"serve:dist": "gulp serve:dist",
"test": "gulp test",
"test:auto": "gulp test:auto"
},
"eslintConfig": {
"globals": {
"expect": true
},
"root": true,
"env": {
"browser": true,
"jasmine": true
},
"extends": [
"xo-space/esnext"
]
}


}

This is the index.js file

./index.js

import angular from 'angular';

import {techsModule} from './app/techs/index';
import {playlistModule} from './app/playlist/index';
import {webtorrent} from 'webtorrent';


import 'angular-ui-router';
import routesConfig from './routes';

import {main} from './app/main';
import {header} from './app/header';
import {title} from './app/title';
import {footer} from './app/footer';

import './index.scss';

angular
.module('app', [techsModule, playlistModule, 'ui.router'])
.config(routesConfig)
.component('app', main)
.component('fountainHeader', header)
.component('fountainTitle', title)
.component('fountainFooter', footer);


This is my module where I want to use webtorrent:

./src/app/playlist/playlist.js

import Webtorrent from 'webtorrent';

class PlaylistController {
contructor() {
this.TorrentId = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d&dn=sintel.mp4&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel-1024-surround.mp4';
this.Client = new Webtorrent();
}
}

export const playlist = {
templateUrl: "app/playlist/playlist.html",
controller: PlaylistController,
bindings: {
playlist: '<'
}
};


So I have this error:

ERROR in ./~/create-torrent/index.js
Module not found: Error: Can't resolve 'fs' in '/Users/macbook-lucas/refreex/node_modules/create-torrent'
@ ./~/create-torrent/index.js 22:9-22
@ ./~/webtorrent/index.js
@ ./src/index.js

ERROR in ./~/parse-torrent/index.js
Module not found: Error: Can't resolve 'fs' in '/Users/macbook-lucas/refreex/node_modules/parse-torrent'
@ ./~/parse-torrent/index.js 7:9-22
@ ./~/webtorrent/index.js
@ ./src/index.js

ERROR in ./~/is-file/index.js
Module not found: Error: Can't resolve 'fs' in '/Users/macbook-lucas/refreex/node_modules/is-file'
@ ./~/is-file/index.js 3:9-22
@ ./~/create-torrent/index.js
@ ./~/webtorrent/index.js
@ ./src/index.js

ERROR in ./~/pump/index.js
Module not found: Error: Can't resolve 'fs' in '/Users/macbook-lucas/refreex/node_modules/pump'
@ ./~/pump/index.js 3:9-22
@ ./~/webtorrent/lib/torrent.js
@ ./~/webtorrent/index.js
@ ./src/index.js

ERROR in ./~/webtorrent/lib/torrent.js
Module not found: Error: Can't resolve 'fs' in '/Users/macbook-lucas/refreex/node_modules/webtorrent/lib'
@ ./~/webtorrent/lib/torrent.js 13:9-22
@ ./~/webtorrent/index.js
@ ./src/index.js


But the problem is with this seeder of JS because i tried to install webtorrent in a clean proyect and it works perfect, so I don't know why Yeoman is using fs wrong.

I know that "fs" is a core-module of NODE.js but I tried to install fs, but it only install a dummy package just for security reasons, so I unistall it.

Also i tried to update the NPM with this command:

npm update -g npm

But nothing.

With info of fs module i have this information.
npm info fs -v
3.10.8

Answer

You should take a look at the webpack section https://github.com/feross/webtorrent. It explains you need to add couple of lines of configuration to make it work.

{
  target: 'web',
  node: {
    fs: 'empty'
  },
  module: {
    loaders: [
      // make sure to install the 'json-loader' package: npm install json-loader
      {
        test: /\.json$/,
        loader: 'json'
      }
    ]
  }
}