1

I am working on a project using AngularJS and NodeJS. I am new to both frameworks and need some help for reading in (text) files.

I am trying to read files using npm's read-file (https://www.npmjs.com/package/read-file).

I put var read = require('read-file') into one of my controllers Javascript files, but I keep getting an error message Cannot find module 'read-file'

Below is the current project structure:

my_project
   |--app
       |--bower_components
       |--components
       |--scripts
           |--controllers
               |--my_controller.js
       |--styles
       |--views
           |--my_page.html
       |--app.js
       |--index.html

   |--node_modules
       |--read-file
           |--index.js
           |--LICENSE
           |--package.json
           |--README.md

Clearly I installed the read-file module, but I think there is an issue locating it. What should I do to make sure that require('read-file') correctly locates the read-file module?

Any help will be appreciated. Thanks!

1
  • read-file this is module for server-side. Thin wrapper around fs.readFile and fs.readFileSync Commented Jun 30, 2016 at 21:16

2 Answers 2

1

It seems that you are trying to read the read-file module within your Angular.js application controller. The controllers within Angular.js are front end files and are run from within the Browser environment and not Node.js. Therefore, you cannot use utilities such as require within the browser.

I think you may be able to achieve what you are trying to accomplish by running your front end application through a tool like Browserify. This will allow you to use Node.js to do stuff like require which will load the file during compilation time and allow you to require files within the browser. You can also use Node.js tools such as fs to load the contents of files and include them within your front end application. I think this should get you pretty close.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are trying to load the read-file module in the client js part, where is not defined at all.

You should create your node server here, and create an api you should call from your front-end controller that load get the file.

From the node api handler you read the file and send back to the client when requested.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.