Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module X... #10567

Closed
MCKRUZ opened this issue Aug 26, 2016 · 15 comments
Closed

Cannot find module X... #10567

MCKRUZ opened this issue Aug 26, 2016 · 15 comments
Labels
Fixed A PR has been merged for this issue

Comments

@MCKRUZ
Copy link

MCKRUZ commented Aug 26, 2016

TypeScript Version: 1.8.0

Code
I have two projects. One I'm building general gulp tasks in. One I'm consuming those general gulp tasks.

Project 1 contains a class called TasksTools that lives in a file called tasksTools.ts

import { existsSync, lstatSync, readdirSync } from 'fs';
import * as gulp from 'gulp';
import * as util from 'gulp-util';
import * as isstream from 'isstream';
import { join } from 'path';
import * as tildify from 'tildify';

export class TasksTools {
    private _gulpConfig: any;

    constructor(config: any) {
        this._gulpConfig = config;
    }

    /**
     * Loads the tasks within the given path.
     * @param {string} path - The path to load the tasks from.
     * @param {any} config      - The configuration file.
     */
    public loadTasksFromDir(path: string, config: any): void {
        this._readDir(path, taskname => this.registerTask(taskname, path, config));
    }

    /**
     * Registers the task by the given taskname and path.
     * @param {string} taskname - The name of the task.
     * @param {string} path     - The path of the task.
     * @param {any} config      - The configuration file.
     */
    public registerTask(taskname: string, path: string, config: any): void {
        const TASK = join(path, taskname);
        gulp.task(taskname, (done: any) => {
            const task = require(TASK);
            if (task.length > 0) {
                return task(config, done);
            }

            const taskReturnedValue = task();
            if (isstream(taskReturnedValue)) {
                return taskReturnedValue;
            }

            // TODO: add promise handling if needed at some point.

            done();
        });
    }

    /**
     * Reads the files in the given root directory and executes the given callback per found file.
     * @param {string}   root - The root directory to read.
     * @param {function} cb   - The callback to execute per found file.
     */
    private _readDir(root: string, cb: (taskname: string) => void): void {
        if (!existsSync(root)) {
            return;
        }

        walk(root);

        function walk(path: string) {
            let files = readdirSync(path);
            for (let i = 0; i < files.length; i += 1) {
                let file = files[i];
                let curPath = join(path, file);
                if (lstatSync(curPath).isFile() && /\.ts$/.test(file)) {
                    let taskname = file.replace(/\.ts$/, '');
                    cb(taskname);
                }
                else if (lstatSync(curPath).isDirectory()) { // recurse
                    walk(curPath);
                }
            }
        }
    }
}

it also has a definitions file that lives in a file called index.d.ts

declare module "@mtm/mtm-ng2-gulp-tasks" {
export class TasksTools {
        public constructor(config: any)

        /**
            * Loads the tasks within the given path.
            * @param {string} path - The path to load the tasks from.
            * @param {any} config       - The configuration file.
            */
        public loadTasksFromDir(path: string, config: any): void

        /**
            * Registers the task by the given taskname and path.
            * @param {string} taskname - The name of the task.
            * @param {string} path     - The path of the task.
            * @param {any} config       - The configuration file.
            */
        public registerTask(taskname: string, path: string, config: any): void
    }
}

so I wrap these up and make an NPM library. Then I import that lib into project 2
Here is my gulp file gulpfile.ts

import * as gulp from 'gulp';
import * as config from './gulp.config';
import * as runSequence from 'run-sequence';
import * as util from 'gulp-util';
// This line doesn't work
import { TasksTools } from '@mtm/mtm-ng2-gulp-tasks';

// If I uncomment this line it works
//import { TasksTools } from './node_modules/@mtm/mtm-ng2-gulp-tasks/utils/momentum.utils';

let _gulpConfig = new config.ProjectConfig();
let _taskTools = new TasksTools(_gulpConfig);

// Load Default Tasks
_taskTools.loadTasksFromDir(_gulpConfig.MOMENTUM_TASKS_DIR, _gulpConfig);

// Load Project Tasks
_taskTools.loadTasksFromDir(_gulpConfig.PROJECT_TASKS_DIR, _gulpConfig);

Now when I try to run this I get the error

Failed to run "C:\TFS\Dev\Momentum.Torques\Client\Gulp\mtm-ng2-gulp-tasks\mtm-ng2-gulp-tasks-test\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
module.js:442
throw err;
^
Error: Cannot find module '@mtm/mtm-ng2-gulp-tasks'

Expected behavior:
I expect that this would work
Actual behavior:
It did not work

@RyanCavanaugh
Copy link
Member

How are you building the gulp task?

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Aug 27, 2016
@MCKRUZ
Copy link
Author

MCKRUZ commented Aug 29, 2016

Here is an example https://github.com/MCKRUZ/GulpTypescriptError

@mhegazy
Copy link
Contributor

mhegazy commented Aug 29, 2016

running tsc from your repo gives me these errors:

c:\test\10567\GulpTypescriptError\mtm-ng2-gulp-tasks\mtm-ng2-gulp-tasks>tsc --v
Version 2.1.0-dev.20160829

c:\test\10567\GulpTypescriptError\mtm-ng2-gulp-tasks\mtm-ng2-gulp-tasks>tsc

1 export * from './utils/momentum.utils.ts';
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~

mtm-ng2-gulp-tasks.ts(1,15): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './utils/momentum.utils' instead.


2 export * from './config/momentum.config.interfaces.ts';
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

mtm-ng2-gulp-tasks.ts(2,15): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './config/momentum.config.interfaces' instead.


3 export * from './config/momentum.config.methods.ts';
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

mtm-ng2-gulp-tasks.ts(3,15): error TS2691: An import path cannot end with a '.ts' extension. Consider importing './config/momentum.config.methods' instead.

the errors look right to me.

@MCKRUZ
Copy link
Author

MCKRUZ commented Aug 29, 2016

If you run it in studio it compiles just fine. It doesn't have anything to do with the error that I was originally talking about. Try running it through studio.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 29, 2016

the error is introduced in TS 2.0.2 (post TS 2.0 beta). you should not be requiring a ".ts" file as the file does not exist at runtime, and the compiler will not rewrite this import to .js. see #9538 and #9646 for more details.

I get the same build errors in VS with latest TS drop (has not shipped yet). so looks like the original issue will be fixed with the next release.

@MCKRUZ
Copy link
Author

MCKRUZ commented Aug 29, 2016

Right I get the error that you're referencing. I actually just fixed it if you want to pull down the latest. What I'm saying is that I don't think the original error has anything to do with that b/c I'm still seeing the original problem after I fix it. Are you saying you don't see the original problem in the new build?

@mhegazy
Copy link
Contributor

mhegazy commented Aug 29, 2016

nope. fixing the 3 errors leads to a successful build for me from VS and on the commandline.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 29, 2016

i am gonna mark this as fixed in TS 2.0 RC for now. feel free to reopen if you see it after installing the next TS version.

@mhegazy mhegazy added Fixed A PR has been merged for this issue and removed Needs More Info The issue still hasn't been fully clarified labels Aug 29, 2016
@mhegazy mhegazy added this to the TypeScript 2.0.1 milestone Aug 29, 2016
@MCKRUZ
Copy link
Author

MCKRUZ commented Aug 29, 2016

Sorry sorry...this is my bad. I accidentally uploaded the file with the fix. Try pulling again and running it. If you look at the task runner explorer it will show an error.

@MCKRUZ
Copy link
Author

MCKRUZ commented Aug 29, 2016

the line you're looking for is line 5 in gulpfile.ts. Where it references not the full path. When I do it like this the task runner explorer doesn't pick up any of the tasks.

@mhegazy mhegazy removed the Fixed A PR has been merged for this issue label Aug 29, 2016
@mhegazy mhegazy removed this from the TypeScript 2.0.1 milestone Aug 29, 2016
@MCKRUZ
Copy link
Author

MCKRUZ commented Aug 31, 2016

were you able to get it to error?

@mhegazy
Copy link
Contributor

mhegazy commented Aug 31, 2016

Sorry, i did not have time to try it again after your last post.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 31, 2016

OK tried again. looks like fixed for me. i am gonna mark it as fixed. if you feel this still an issue please reopen and i would appreciate it if you can provide some additional information why you believe this is a TS issue and not caused by other parts of the tool chain.

@mhegazy mhegazy closed this as completed Aug 31, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Aug 31, 2016
@mhegazy mhegazy added this to the TypeScript 2.1 milestone Aug 31, 2016
@MCKRUZ
Copy link
Author

MCKRUZ commented Aug 31, 2016

What version of typescript are you on?

From: Mohamed Hegazy notifications@github.com
Reply-To: Microsoft/TypeScript reply@reply.github.com
Date: Wednesday, August 31, 2016 at 1:37 PM
To: Microsoft/TypeScript TypeScript@noreply.github.com
Cc: Matt Kruczek kruz79@gmail.com, Author author@noreply.github.com
Subject: Re: [Microsoft/TypeScript] Cannot find module X... (#10567)

OK tried again. looks like fixed for me. i am gonna mark it as fixed. if you feel this still an issue please reopen and i would appreciate it if you can provide some additional information why you believe this is a TS issue and not caused by other parts of the tool chain.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 31, 2016

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants