Submodules in Browserify - javascript

/foo
/bar.js
/foobar.js
/index.js
In node.js if you a require a directory (require('foo')), it would look into that directory and find an index.js file and return whatever exports I have in that file, so I can just bundle up the contents of the directory in an index.js file. Therefore, I dont have to require bar and foobar separately if index.js already includes them.
However this approach doesn't work with browserify. It seems like only thing browserify understands is relative paths.
/star
/star.js
/starfoo.js
/index.js
/foo
/bar.js
/foobar.js
/index.js
In other words I want to separate my project into submodules, call require on a directory as if I am calling require on a dependency. For example in the star.js file I want to be able to require('foo') and get the exports of bar.js and foobar.js (as long as /foo/index.js is importing bar.js and foobar.js)
edit:
Looking at the react source code, i think what i am describing is possible
https://github.com/facebook/react/blob/master/src/isomorphic/ReactIsomorphic.js
In this file they call require on React-Children in line 14.
var ReactChildren = require('ReactChildren');
However react children is couple directories deeper.
https://github.com/facebook/react/blob/master/src/isomorphic/children/ReactChildren.js
Where is this mapping defined?

There isn't a way to specify a base directory because that's not how node modules work. If you want non-relative paths, use the node_modules directory. If you want require('foo') to work from any directory, just make a symlink from your project root:
ln -s foo node_modules/foo

Related

Can I make TypeScript include node_modules for transpilation?

OK so I have an interesting situation in setting up my Node.js TypeScript project. I want to be able to refer to my local modules using a non-relative require reference. The way TypeScript and Node.js look up modules is to look for a node_modules directory in the current directory and then each parent directory until they find such a directory containing the reference. So let's say I have a module I want to reference in the following directory structure:
/node_modules <-- Main NPM modules dir
/...
/package.json
/src
/node_modules <-- My local modules dir
/modules
/myModule.ts
/scripts
/init.ts
... and in init.ts I reference myModule like this:
import myModule from "modules/myModule";
As I understand it, I want TypeScript to transpile my node_modules directory over to the output dist directory along with all my other .ts file directories, so that the dist directory looks like this:
/node_modules <-- Main NPM modules dir
/...
/package.json
/dist
/node_modules <-- My local modules dir
/modules
/myModule.js
/scripts
/init.js
Then, when Node.js looks for the module it will find it at dist/node_modules/modules/myModule.js. So in this case I actually do want TypeScript to include the node_modules directory in its input files. I seem to be missing something fundamental, though, because TypeScript actually ignores this directory by default.
Is my scenario a legitimate one for including the node_modules directory, and if so, how can I make TypeScript include it in the transpilation when I just run tsc (and therefore it will use my tsconfig.json file)?
UPDATE:
I have found that I can make it include my node_modules directory by explicitly putting it in the include directive in the .tsconfig.json like so:
"include": [
"./src/node_modules/**/*",
"./src/**/*"
]
The question remains; am I getting something fundamentally wrong here, because I am having to override something TypeScript excludes by default? I've tested this and when I make it transpile node_modules, it does indeed find my local modules correctly.
That's not correct to include node_modules for transpilation. Because node_modules contains javascript and doesn't requires second transpilation in case of ts libraries, they should have declarations in .d.ts.
If you have custom types, or you want to add types to some libraries that don't have them, you need to use own declarations or #types/* packages.
in your tsconfig.json you can define type roots (where to load declarations from).
{
"compilerOptions": {
"typeRoots": ["./declarations", "./node_modules/#types"],
"types": ["node"],
}
}
then you can install npm i --save-dev #types/node#^12 for example to get declarations of nodejs v12.
and define your own declarations in declarations for express for example: ./declarations/express/index.d.ts
import * as express from 'express';
declare module 'express' {
export interface Request {
user?: {
id: string;
name: string;
}
}
}

webpack - require script in index.js file without parsing

I'm using webpack with babel-loader to organize my webdriverio scripts. Since I'm writing automated web-site testing scripts, I don't have a production environment per-se, so the point of using webpack is really just to organize my code chunks better and transpile my es6 code to es5 since node does not allow all es6 features.
I have a script: "../../../external/file-search.js" which I am requiring at the top of an index.js file. The point of file-search.js is to search through the directory and require all files in that directory using fs. This is what my index.js file looks like (located in ~/tasks/):
var fileSearch = require("../../../external/file-search.js");
var d = __dirname;
fileSearch(d);
when I run "webpack tasks test.js" webpack compiles file-search.js into my "test.js" file rather than requiring file-search.js and allowing me to use it's exported method in my index.js file. I will use file-search.js in all my index.js files so it's important to include it as a module. I've tried using externals but as far as I know, externals simply exclude certain modules from being compile/transpiled and try to bundle them into the final script. I actually want to require that script and use it right away in my index.js file. How can I require file-search.js and use it right away as part of my index.js file?

Requring all the files in a directory nodejs

The topic may seem to be duplicate. Read it completely
I know there are multiple packages available in nodejs to require all the files in a directory.
But I am in a research to require all the files in a folder and use the variables and functions which are exported in each js file. Need to perform this just by requiring the directory name.
For example,
var files = require("./folder");
The folder may contain some files like
File1.js, File2.js, File3.js
I want to use all the variables and functions which are exported in all the js files.
I think there might be some way in the "Package.json" file.
But I am not expert in "Package.json".
Can anyone help me to figure out the senario?
Could you just make an index.js file with the modules and just require that.
From the docs http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
It is convenient to organize programs and libraries into self-contained directories, and then provide a single entry point to that library. There are three ways in which a folder may be passed to require() as an argument.
The first is to create a package.json file in the root of the folder, which specifies a main module. An example package.json file might look like this:
{ "name" : "some-library",
"main" : "./lib/some-library.js" }
If this was in a folder at ./some-library, then require('./some-library') would attempt to load ./some-library/lib/some-library.js.
This is the extent of Node's awareness of package.json files.
If there is no package.json file present in the directory, then node will attempt to load an index.js or index.node file out of that directory. For example, if there was no package.json file in the above example, then require('./some-library') would attempt to load:
./some-library/index.js
./some-library/index.node

Unable to set baseUrl for intern-runner

My project follows the following (simplified) directory structure:
\
|- app
|- script1.js
|- script2.js
|- test
|- intern.conf.js
|- test.spec.js
I'm using requirejs in my application and thus all scripts under app/ directory have their dependencies relative to that folder.
Because Intern baseUrl defaults to the root folder, the scripts under app/ fail to load.
However, setting the baseUrl under loader to 'app' or '/app' and so forth, results in failure to load the test suite..
Error: Failed to load module ../test/intern.conf from
test/intern.conf.js (parent: *2)
I tried to set the test suite location to '../test/test.spec.js' and so forth, with no success.
The baseUrl must be the base URL common to all modules, including test modules, so in your case would be the parent directory of the app and test directories. Normally this means that you will cd to the parent directory and simply run Intern from there, like intern-runner config=test/intern.conf, with no additional loader configuration necessary.
If you do need extra configuration (for example, to define app as a package), the loader configuration in your Intern configuration file doesn’t need to be the same as the loader configuration in your application, so in practice any difference between the two should never be an issue. You will have one configuration in your application entrypoint that works for your app, and one configuration in your test configuration that works for your test environment.
Relative AMD module IDs are relative to the module itself, so if your module app/script1 has a dependency ./script2, it will correctly load /root/app/script2.js, not /root/script2.js. When you load app/script1 from your test/test.spec module, so long as your baseUrl is the parent directory, you can either require ../app/script1 (if this makes sense, i.e. if the two are part of the same logical package) or app/script1 (if test and app are supposed to be two different packages).
I believe you may need to setup some paths in your requirejs configuration object. It will allow you to load in scripts outside of the baseUrl directory. So, you can still setup the baseUrl to be "app" while loading in your test directory though a path alias (http://requirejs.org/docs/api.html#jsfiles ).
In the example below both "app" and "test" are at the root along with the requirejs.config call, so using "/test" works. You could alternatively use "../" should files not be in the root.
requirejs.config({
baseUrl: "/app",
paths: {
"test" : "/test"
},
});
This would allow you to require a test file through:
define(["test/test.spec.js"], function(){ ... });
as it will use the "test" path to find out where that directory is.

require a file outside of node_modules with grunt

I am trying to require a node module that is used in my app into my gruntfile.js but whatever I do i get : Cannot find module 'appjs/config
In the gruntfile I simply do : var myconfig = require('appjs/config');
But it just don't want to load, is there a way to import this files easily? I also tried various path but appjs is at the root where the gruntfile is.
Have you tried this?
var myconfig = require('./appjs/config');
Seems like this should work, if appjs is in the same directory as your gruntfile.

Categories