Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import or include a javascript file in a gulp file

Tags:

gulp

How can I import or include my own javascript file into a gulpfile?

Gulp is installed via npm, so I can include other npm modules like this:

var sass = require('gulp-sass');

So I would like to do something like:

var karmaConfig = require('karma-config.js');

The directory structure is like this:

node_modules/
gulpfile.js
package.json
karma-config.js

so the gulpfile is at the same level as the file that I want to import.

like image 485
thetallweeks Avatar asked May 28 '14 05:05

thetallweeks


Video Answer


1 Answers

You can do this by:

var karmaConfig = require('./karma-config');

And whatever this file exports can now be accessed from karmaConfig.

hendrikswan is right that you can leave off from the end .js as that's the default. But you don't need to.

The ./ part means that it should be found from the current folder rather than in node_modules. If your structure was like this:

node_modules/
gulpfile.js
package.json
config/karma.js

You would require it thus:

var karmaConfig = require('./config/karma');
like image 62
Bart S Avatar answered Sep 21 '22 18:09

Bart S