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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With