Home > Enterprise >  JS cannot import npm module
JS cannot import npm module

Time:05-09

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 :(

CodePudding user response:

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

CodePudding user response:

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

  • Related