wclr / ts-node-dev

Compiles your TS app and restarts when files are modified.
MIT License
3.43k stars 123 forks source link

Error: Could not require <path>/node_modules/koa/lib/response.js #1

Closed abossard closed 6 years ago

abossard commented 6 years ago

Hi, I habe a Koa based Typescript application and when I try to run it, I get this error:

$ ts-node-dev src/server/server.ts
Error: Could not require /Users/<path>/node_modules/koa/lib/response.js, compiled path:/var/folders/bd/9xvc6tls6t59tfrxn_vm22m80000gp/T/.ts-node/compiled/node_modules_koa_lib_response_js_ec7c7147dccedf853a26eeb31b46f4250fa2f99a9774232bc686fb937e034307.js
    at compile (/private/var/folders/bd/9xvc6tls6t59tfrxn_vm22m80000gp/T/ts-node-dev-hook-1508310397187.js:26:13)
    at Module.m._compile (/private/var/folders/bd/9xvc6tls6t59tfrxn_vm22m80000gp/T/ts-node-dev-hook-1508310397187.js:70:38)
    at Module._extensions..js (module.js:635:10)
    at require.extensions..js (/private/var/folders/bd/9xvc6tls6t59tfrxn_vm22m80000gp/T/ts-node-dev-hook-1508310397187.js:73:14)
    at Object.nodeDevHook [as .js] (/Users/<path>/.nvm/versions/node/v8.7.0/lib/node_modules/ts-node-dev/lib/hook.js:61:7)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
[ERROR] 09:06:48 Error
wclr commented 6 years ago

What is your tsconfig? You probably should exclude **/node_modules/* from the compilation.

abossard commented 6 years ago

I already excluded the node_modules:

{
  "compilerOptions": {
    "outDir": "build",
    "module": "commonjs",
    "target": "es5",
    "lib": [
      "ES2017",
      "es6",
      "dom"
    ],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "baseUrl": "./",
    "paths": {
      "*": ["src/types/*", "*"]
    },
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": false
  },
  "exclude": [
    "node_modules",
    "build"
  ]
}
wclr commented 6 years ago

if you add "*/node_modules/" instead of "node_modules" the same result? What is the code in server.ts? (to reproduce)

abossard commented 6 years ago

Under Mac OS: same result. Works with Windows! No joke. This is my server.ts

import * as dotenv from "dotenv";
import * as Koa from "koa";
import * as bodyParser from "koa-bodyparser";
import * as mount from "koa-mount";
import * as passport from "koa-passport";
import * as route from "koa-route";
import * as session from "koa-session";
import * as serve from "koa-static";
import * as AD from "passport-azure-ad";
import {handleRender} from "./handleRender";

dotenv.config();

const {
    PORT = 3001,
    HOST = "0.0.0.0",
    REDIS_API_URL = "",
    AD_CALLBACK_URL = "",
    AD_CLIENT_ID = "",
    AD_TENANT = "",
    AD_RESOURCE = "",
    SESSION_SECRET = "",
    AD_CLIENT_SECRET = "",
} = process.env;

const app = new Koa();

const fetchUser = async (criterion: object) => {
    return Object.assign({ id: 1, username: "test" }, criterion);
};

passport.serializeUser((user, done) => {
    done(null, JSON.stringify(user));
});

passport.deserializeUser(async (id, done) => {
    try {
        const user = await fetchUser(JSON.parse(id));
        done(null, user);
    } catch (err) {
        done(err, null);
    }
});

passport.use(new AD.OIDCStrategy({
    allowHttpForRedirectUrl: true,
    clientID: AD_CLIENT_ID,
    clientSecret: AD_CLIENT_SECRET,
    identityMetadata: "https://login.microsoftonline.com/common/.well-known/openid-configuration",
    redirectUrl: AD_CALLBACK_URL,
    responseMode: "query",
    responseType: "id_token code",
}, (iss, sub, profile, accessToken, refreshToken, done) => {
    fetchUser({ accessToken, name: profile.displayName, upn: profile.upn})
        .then((user) => {
            done(null, user);
        } ).catch(done);
}));

app.proxy = true;

app.keys = [SESSION_SECRET];

// sessions
app.use(session({}, app));

// body parser
app.use(bodyParser());

app.use(passport.initialize());
app.use(passport.session());

app.use(mount("/static", serve("./build/htdocs")));

app.use(route.get("/auth/ad", passport.authenticate("azuread-openidconnect", {
    prompt: "login",
    resourceURL: AD_RESOURCE,
    tenantIdOrName: AD_TENANT,
})));

app.use(route.get("/auth/ad/callback", passport.authenticate("azuread-openidconnect", {
    failureRedirect: "/",
    resourceURL: AD_RESOURCE,
    successRedirect: "/nice",
    tenantIdOrName: AD_TENANT,
})));

app.use(route.get("/nice", (context) => {
    if (context.isAuthenticated()) {
        context.body = "Logged in: " + JSON.stringify(context.state.user);
    } else {
        context.body = "Really not logged in";
    }
    context.body += JSON.stringify(context.session);
}));

app.use(route.get("/auth/logout", (context) => {
    context.logout();
    context.redirect("/");
}));

app.use(handleRender());

app.listen(Number(PORT), HOST, () => {
    process.stdout.write(`Running on http://${HOST}:${PORT} \n`);
    process.stdout.write(`Redis at ${REDIS_API_URL}\n`);
});

process.on("SIGINT", () => {
    process.stdout.write("Shutdown.");
    process.exit(0);
});

And as I mentioned: works with Windows and Node.js 8.7.0.

Maybe it's a Mac specific issue?

wclr commented 6 years ago

Suggestions, either: 1) Change cache-dir (--cache-directory option) maybe to cwd, and check if compiled file exists. 2) Set allowJS: false in tsconfig and try without it.

abossard commented 6 years ago

Hey @whitecolor, thank you so much! allowJs: false did the trick.

So for future reference: on Mac OS it required allowJs: false in the tsconfig.

wclr commented 6 years ago

on Mac OS it required allowJs: false in the tsconfig.

I actually will fix it here too, files in /node_modules/ should be ignore by default. Thanks for reporting about the problem.

wclr commented 6 years ago

should be fixed now