require a file outside of node_modules with grunt - javascript

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.

Related

Webpack require npm module dynamiclly by variable

I can't find a way to require NPM module dynamically by variable.
Here is a sample code what I'm trying to do, Everything works great expect import NPM module dynamically.
const firstModule = 'my-npm-module';
const secondModule = './MyReactComponent';
// NPM Module
import(firstModule).then(...); // Doesn't work
import('my-npm-module').then(...); // Works
// Local React Component
import(secondModule).then(...); // Works
import('./MyReactComponent').then(...); // Works
From the Webpack docs on dynamic import:
Fully dynamic statements, such as import(foo), will fail because
webpack requires at least some file location information. This is
because foo could potentially be any path to any file in your system
or project. The import() must contain at least some information about
where the module is located, so bundling can be limited to a specific
directory or set of files.
Your best option would probably be to either not use dynamic loading for anything in node_modules, or add the explicit path to the module, e.g.
import(`./node_modules/${firstModule}/index.js`);

Require node_modules from a relative path in gulpfile.js

All works fine when I have the node_modules folder in the sameone as the gulpfile.js.
But whenever I place node_modules anywhere outside it, I can't seem to make it work by using relative paths:
var gulp = require('../../node_modules/gulp');
var rename = require('../../node_modules/gulp-rename');
I get the following message:
[20:11:45] Task 'default' is not in your gulpfile
[20:11:45] Please check the documentation for proper gulpfile formatting
But I in fact have a default task and it runs as expected when not using relative paths.
I also tried including them without specifiying the node_modules folder, as I though it should be:
var gulp = require('../../gulp');
var rename = require('../../gulp-rename');
But I get the following error:
module.js:538
throw err;
^
Error: Cannot find module '../../gulp'
What am I doing wrong?
Try calling require without a path, like this:
var gulp = require('gulp');
var rename = require('gulp-rename');
This triggers require to search for the node_modules folder, automatically checking each parent folder up to the root. https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders

require path in browserify is not recognized

I have below project structure as shown in the screenshot below. Both index.js and test.js are located at the same path i.e. ./assets/js/ but still the path is not recognized. The error says
Error: Cannot find module 'test.js' from
'C:\Users\meghshyam\desktop\browserifyproj\assets\js'
Use var test = require('./test.js');
This way you are letting node know that its a file in the same folder

Module not found in Electron / Atom Shell

I'm new to npm, node and Electron.
My Folder looks like:
-package.json
-index.html
-main.js
-js/myStuff.js
-node_modules
In the file myStuff.jsI have var chokidar = require('chokidar'); but it shows then module not found error.
In the index.html I included the myFile.js like normaly in a script tag.
I did read how node is looking for modules. But that doesn't help cause then it should find it, cause it's looks in all parent dirs for the node_modules folder and then in there for a chokidar folder and in that for the index.js file, which is there.
What am I doing wrong?
It works now, but i have no idea why..
In contrast to other node apps, electron doesn't automatically set your NODE_PATH to global (i.e. /usr/bin) or local (e.g. node_modules) folders. So you need to export it manually:
NODE_PATH=/path/to/node_modules electron my_app

Submodules in Browserify

/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

Categories