简体   繁体   中英

Gulp - Cannot find module 'gulp-rename'

I trying to set up Gulp to compile a SCSS file and also rename the SCSS file in the process - so for example:

I want SCSS/original.scss to be saved as CSS/new.css

This is on a Windows 10 VM

I have installed Gulp, installed gulp-sass which all worked just fine

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

gulp.task('compile-new-main', function(){
 return gulp.src('scss/new-main.scss')
  .pipe(sass()) // Using gulp-sass
  .pipe(gulp.dest('css'))
 });

Running the above gulp task works just fine but when I try and install gulp-rename as the following the task breaks:

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

gulp.task('compile-new-main', function(){
 return gulp.src('scss/new-main.scss')
  .pipe(sass()) // Using gulp-sass
  .pipe(rename("styles.css"))
  .pipe(gulp.dest('css'))
 });

I initially tried just installing gulp rename with this command

npm install gulp-rename

The same as I did for gulp-sass which seemed to work just fine as before, clearly not but I'm not sure how or why.

I've done some Googling and tried installing it globally which again seemed to install fine

npm install gulp-rename -g

And I also saw some recommendations of saving gulp-rename as a dev-dependancy so I tried

npm install gulp-rename --save-dev

Again no errors in installing.

When I try and run my gulp task this is the error message I get:

Error: Cannot find module 'gulp-rename' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object. (C:\Projects\VAT-Expert\gulpfile.js:3:14) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3)

I can't get my head around what I've done wrong here, think I've added all of the relevant information here - very much appreciate any guidance or advice on what I could try on this one!

N

First, make sure that gulp-rename is listed in your package.json under dependencies or devDependencies . Then run:

npm uninstall -g gulp
npm install -g gulp
rm -rf node_modules
npm install 

Then check again.

install the gulp-rename package from cli:

npm install --save-dev gulp-rename

then import the package from node_modules

const rename = require("gulp-rename");

use it before compiling

Example:

const miniCSS = () => {
    return gulp.src('./css/*.css')
    .pipe(rename('style.min.css'))
    .pipe(uglifycss({
        "uglyComments": true
    }))
    .pipe(gulp.dest('./dist/'))
}

I know this is already answered, but I'm posting this as another solution for a PHP project.

  1. $ npm install gulp-rename would work just fine and install the package but still got the Cannot find module 'gulp-rename' error.
  2. I could find the package in node_modules if I went back one directory further back from my root project folder. I did not have a package.json file in this directory for whatever reason.
  3. Doing $ which gulp-rename would not display anything, but I know it was already installed in node_modules .

To fix it under these conditions, I did:

var rename = require('../node_modules/gulp-rename/index.js');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM