Can I force node.js require to be case sensitive?

拈花ヽ惹草 提交于 2019-11-30 00:18:58

问题


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.


回答1:


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 ...
};



回答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




回答3:


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




回答4:


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?




回答5:


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.



来源:https://stackoverflow.com/questions/13617863/can-i-force-node-js-require-to-be-case-sensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!