0

I can't import npm module using for example import fs from 'fs'; in my main.js file that linked with index.html. Script tag that connect JS file has attribute type="module". Error in console of browser throws error: Uncaught TypeError: Failed to resolve module specifier "fs". Relative references must start with either "/", "./", or "../".. BUT npm modules must be connected by pointing only module name, without path and I already did that in another project and it worked correctly.

Then I tried to point relative path to fs module. But node_modules doesn't have folder 'fs'. Instead it contains several folders with 'fs' in start of each folder name.

At that moment I was completely confused :(

2 Answers2

0

fs is a module available when running on a Node server, not in the browser.

Michael Flores
  • 696
  • 4
  • 12
0

You need to import it as import * as fs from 'fs';. You may also have problems with fs import in the web, but that's a different issue.

More details can be found here

EDIT: The question was "Cannot import npm module". As pointed out by Michael Flores in comment and suggested in the original answer, this won't work anyway in web as fs is not module you can install - it's part of Node.JS

SimProch
  • 96
  • 3
  • You *will* have problems attempting an `fs` import in code running in the browser, because the module comes from Node. Changing his import to `import * as fs from 'fs'` will do nothing for his error. – Michael Flores May 09 '22 at 07:35