Skip to content
This repository has been archived by the owner on Jan 25, 2020. It is now read-only.

Cannot read property 'replace' of undefined #33

Closed
bartspedden opened this issue Dec 31, 2014 · 5 comments
Closed

Cannot read property 'replace' of undefined #33

bartspedden opened this issue Dec 31, 2014 · 5 comments

Comments

@bartspedden
Copy link

We are using the following kraken version: "kraken-js": "^1.0.3",

And we are seeing the following error:

2014-12-31 15:06 -06:00: TypeError: Cannot read property 'replace' of undefined
    at Object.nameify (/opt/pm2/lq-mserver/source/node_modules/adaro/lib/utils.js:44:16)
    at engine (/opt/pm2/lq-mserver/source/node_modules/adaro/lib/engine.js:116:22)
    at View.proto.render (/opt/pm2/lq-mserver/source/node_modules/engine-munger/lib/expressView.js:45:9)
    at EventEmitter.app.render (/opt/pm2/lq-mserver/source/node_modules/express/lib/application.js:530:10)
    at ServerResponse.res.render (/opt/pm2/lq-mserver/source/node_modules/express/lib/response.js:904:7)
    at serverError (/opt/pm2/lq-mserver/source/node_modules/kraken-js/middleware/500.js:30:17)
    at serverError (eval at createToggleWrapper (/opt/pm2/lq-mserver/source/node_modules/kraken-js/node_modules/meddleware/index.js:1:0), <anonymous>:1:77)
    at Layer.handle_error (/opt/pm2/lq-mserver/source/node_modules/express/lib/router/layer.js:58:5)
    at trim_prefix (/opt/pm2/lq-mserver/source/node_modules/express/lib/router/index.js:269:13)
    at /opt/pm2/lq-mserver/source/node_modules/express/lib/router/index.js:238:9

I took a look at the code

exports.nameify = function nameify(file, views, ext) {
    // Windows. Ugh.
    views = views && views.replace(CRAZY_SLASHES, '\\\\');

    // Escape special characters in path
    views = views && views.replace(REGEX_SPECIAL_CHARACTERS, "\\$&");

    // Remove absolute path (if necessary)
    file = file.replace(new RegExp('^' + views), '');

    // Remove file extension
    file = file.replace(ext, '');

    // Remove leading slash or drive (platform-dependent, e.g. `c:\\` or '/');
    file = exports.isAbsolutePath(file) ? file.replace(ROOT_DIR, '') : file;

    // Remove remaining leading slashes (if not abs path).
    file = file.replace(LEADING_SEPARATOR, '');

    // Ensure path separators in name are all forward-slashes.
    file = file.replace(ALL_SEPARATORS, '/');

    return file;
};

and the line in question is

file = file.replace(new RegExp('^' + views), '');

So it appears that the file being passed in is undefined.

Any ideas how to further debug this?

Thanks,

bart

@aredridel
Copy link
Contributor

Have you run grunt build?

Have you got your locale set to something your app supports?

@bartspedden
Copy link
Author

Thanks for responding aredridel.

Each node of cluster has their default locale set to en_US.UTF-8, which our app supports. I'll have to test what would happen if a browser with an unsupported locale were to load the application. Do we need to filter out calls from all unsupported locales?

We are using prepackaged versions (npm's) of kraken, so no, I have not run "grunt build" for adaro.

@aredridel
Copy link
Contributor

The locale comes from the user and the web, not the system locale.

grunt build would be in your app, if you built it using the generator.

@lmarkus
Copy link
Contributor

lmarkus commented Jan 2, 2015

TL;DR:
Run:
$ grunt build
On the base directory of your app.

To expand on @aredridel 's answer a bit,
A Kraken app behaves differently on production vs development environments. (NODE_ENV=production)
On a development environment, which is the default, it will compile templates, localization strings and stylesheets on the fly as required (The resulting artifacts will be placed in the .build directory). This gives you the ability to make a change on your code, reload the page, and see these changes immediately.

Since this requires some overhead, it's not suitable for production environments, so when you're ready to deploy the application, you need to pre-build and pre-compile all resources by running grunt build on the base directory of your app.

In development mode, when you access a resource for the first time, it will go look for it in the .build directory. If it's not found, it will try to compile it on the fly.

In production mode, it expects all resources to be there, and will not attempt to recreate them. So if you haven't built, it will blow up in a number of ways, eg: the error you see above.

One interesting tidbit, is that this may go unnoticed if you do testing on dev mode before switching to production on the same machine. This happens because as you test, the resources will be built and placed on the .build directory as needed, so when you switch to production mode, they are already there. This may leave you scratching your head when you deploy to a new machine where you hadn't tested before.

@bartspedden
Copy link
Author

Thanks IMarkus! You got me going in the correct direction. aredridel was correct - I was not running grunt build, but you explained why I needed to. Interestingly I had to add "i18n" to my grunt build task to get everything to work correctly.

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants