30

Since my Mac has a case-insensitive filesystem case related typos will not be caught when running tests locally, however they fail on the build server which is running Linux.

For example: require('./mymodule') will find ./myModule.js when running on Lion, but not on Linux.

Since I'd like to have the tests fail locally as well in order to not break the build on the server, I'm looking for a way to make node.js require more strict in that it throws an error if it the filename is not exact (i.e. has a difference in casing).

Does anyone know of a way to accomplish this?

EDIT:

Since there seemed no good solution for this problem out there I created valiquire.

This tool validates all requires found in an entire nodejs project also ensuring that the casing is correct.

Thorsten Lorenz
  • 11,781
  • 8
  • 52
  • 62
  • I don't normally use macs so this is just an idea, but could you possibly search for the file yourself, and get the name of it, and then use a string comparison that checks for case too and see if it matches the file you were searching. When you get the file name on a mac, does it keep the case the same? – M. Laing Nov 29 '12 at 03:03

7 Answers7

8

If you use webpack, check out https://github.com/Urthen/case-sensitive-paths-webpack-plugin

Just installed it for our dev builds. Would have saved us from taking prod down multiple times... Which, if you're on this question, is already probably happening

Install

npm install --save-dev case-sensitive-paths-webpack-plugin

Usage

const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');

const webpackConfig = {
    plugins: [
        new CaseSensitivePathsPlugin(),
        // other plugins ...
    ],
    // other webpack config ...
};
Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
3

This is an old question but it was the first google result I came across. For anyone else who stumbles across this, the 2021 answer is to use ESLint package eslint-plugin-import "no-unresolved" rule - https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-unresolved.md#casesensitive

By default, it will show a warning/error if a require path case does not match exactly. When the require path has a difference in case, you will see this:

/Users/<your user>/projects/my-app/api/v1/createApplication.js:
    Line 7: import/no-unresolved - Casing of ../../../repositories/Application does not match the underlying filesystem.

To get this working:

npm install --save-dev eslint-plugin-import

And add to .eslintrc(.js or .json):

    "extends": [
        ...
        "plugin:import/recommended",
        ...
    ],
    ...
    "rules": {
        "import/no-unresolved": [2, {commonjs: true, amd: true}],
        ...
2

Since your hfs filesystem is not case sensitive, lookups for 'fileName' will match 'filename' at OS lib level, and therefore node.js will behave the same. So by definition there is no workaround.

But at the price of a reformating you can change your fs format to use hfs case sensitive.

http://www.coriolis-systems.com/iPartition.php is mentioned in this thread : https://superuser.com/questions/380330/mac-convert-from-case-sensitive-to-case-insensitive-file-system

Community
  • 1
  • 1
1

The bulletproof answer is to reformat your MacOS filesystem to be case-sensitive but this is of course extremely inconvenient. Instead have a look at this answer to a related question, which describes a very elegant solution for this problem on MacOS, which is to create a virtual case sensitive partition, and perform all development work on that: How do I commit case-sensitive only filename changes in Git?

0

Without changing your OS file partition, you can just use a bundler that auto-detect theses changes, I personnaly use webpack 2. It gives warning when the file is resolved but case is different. It will not prevent from compiling on OSX / Windows but you'll have a hint. Plus having a bundler is a great asset in js dev nowadays

the hacky and not recommended way would be to shim require and force a certain case or try most possible cases possible but that might be very ugly to do and create poor performance without speaking of potential danger to load the wrong file if there is a case-renamed file and the old file isn't cleaned

zeachco
  • 754
  • 4
  • 16
0

It looks like you're using Javascript, but if you were using Typescript there is a tsconfig.json compiler option "forceConsistentCasingInFileNames": "true" that helps you avoid this on case-insensitive filesystems like MacOS and Windows have.

stefan2718
  • 1,071
  • 7
  • 6
-6

Always use lowercase for filenames, then you won't have to about whether the filesystem supports it or not.

Reminds me of when people used to use spaces in urls.

chovy
  • 72,281
  • 52
  • 227
  • 295