Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JS cannot import npm module

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 🙁

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading