Node.js: Which module to "require('..')" to use socket.io? - javascript

In https://github.com/socketio/socket.io/blob/master/test/socket.io.js
The code:
What is the module name to require?

It requires the module from the parent directory - in this case, socket.io

A folder can be used as a module if that folder contains, index.js or package.json files etc.
So in this case it is requiring the socket.io.js file in the above folder.
Also if a package.json and index.js file are in the same folder the package.json will get look up priority.

Related

How to deal with require() files inside a node_module?

I am quite new to node_moduels.
For instance, say you have this structure:
The app directory
your_app/node_modules/your_module
your_app/app.js
The module directory
your_module/index.js
lib/a_file_you_require_in_index.js
When you require the file inside the index.js. How do you make sure the path is correct, when the code is executed inside the app.js file, where the module is required?
your_module is a npm module

How does project with nested node_modules behave?

We are trying to separate our project into "sub modules" within single repo, but keep things like webpack, express server global, so assume structure like this
package.json
server.js
index.js
node_modules/
public/
index.html
dashboard.js
onboarding.js
dashboard/
index.js
package.json
node_modules/
components/
assets/
onboarding/
index.js
package.json
node_modules/
...
idea here is to keep build / routing / server logic at root lvl and separate modules like dashboard and onboarding into their separate folders and allow them to use their own node modules.
Will this work? Will node modules be included correctly?
webpack will build assets to public/ folder, with some vendor assets and several entry points i.e. all index.js files
What you are proposing will work fine and compile as you expect. NodeJS will initially look for modules included in your modules 'node_modules' sub-directory and then work up the ladder.

config file on node_modules

I created a generic npm package which has my business logic, but I need some google cloud storage information that is in my config files. How can I access this file, if my package is in my node_modules folder? What would be a good solution for that?
this is the structure:
-config
-google_storage_config
-node_modules
-package
-serviceWhichNeedsThatConfig
Based on your folder structure, we will assume your path to the config will be ../../../config/google_storage_config, since node_modules/package/serviceWhichNeedsThatConfig should always be in the root directory.
Now, to access any variables from this config file, simply include the following code in the serviceWhichNeedsThatConfig,
var config = require('../../../config/google_storage_config');
console.log(config.myVariable);
Hi~Have you tried require?
var config = require('../../config/google_storage_config');

compiling typescript and keep directory structure correctly

I have a build and a src dir. And in my src dir with my public folder where all my css's, assets and html's and the server folder with all server files. And now for an example in my server.ts I use a view in public/html. And if I transpiling my code in the build folder only the .ts files are and then the server.js tries to find the view in cmpl/public/html...
What is now a good way to transpiling the full src folder to build? Can Gulp this? Or TSC directly?
Greetz
You can just copy your html files to cmpl directory:
gulp.task('copyHtml', function () {
return gulp
.src('public/html/**/*.html')
.pipe(gulp.dest('cmpl'));
});

How to ignore node_modules directory from gulp:watch?

I have a gulp script that watches for code changes in all javascript files. It looks like this:
gulp.watch(['./**/*.jsx', './**/*.js'], ['build:react']);
So, it watches for all .js and .jsx files from root directory, but I want to ignore node_modules directory.
How can I do that ?
You ca use gulp unwatch This removes the folder specified from your pipe. However, it's generally a good idea to have a source folder that gulp watches instead of the project root folder (then the .git directory and all sorts will be caught up in your task.

Categories

Resources