webpack: webpack native require doesn't work in windows
I want to natively require modules without using webpack requiring mechanism, i.e keeping require(..)
as it without touch.
to achieve my goal I created a file called native-require.js
with this content
module.exports = require;
and in webpack.config.js
module: {
noParse: /\/native-require.js$/,
}
now, in my script file I do:
let filePath = resolve(__dirname, `../config/file.js`);
let content = nativeRequire(filePath);
console.log(content)
it works fine in Linux but in windows I got the error Cannot find module 'D:\Downloads\config\file.js'
I also tried to remove the file extension from filePath
I don’t want to lock my project to webpack-only using, I need my project to be able to run without webpact (for example using ts-node)
so I don’t want to use webpack-specific variables such as __webpack_require__
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 29 (15 by maintainers)
@eng-dibo Please enable them https://webpack.js.org/configuration/module/#moduleparserjavascriptcommonjsmagiccomments, it was disable by default due perf reasons
Feel free to feedback
Webpack should not throw erros in this case
great idea because it takes OS-specific path into consideration. I think this will solve the first issue (i.e using native-require), but is there a workaround to use dynamic import with webpack magic comments in Angular projects?
I will try your recommendation thank you @alexander-akait so much for your great help
What is the error on windows? Sounds like the problem with can you try:
noParse: /[\/\\]native-require.js$/,
?@eng-dibo when you write:
You create asset module (i.e. webpack copy this file). But as you written above you already have files, so you need to disable it:
So it will be not statically analizable, but note -
import.meta.url
will be convert tofile://
(look at generatedwebpack.js
, for better debug just disabledevtool: false
)import.meta.url
using configuration - https://webpack.js.org/configuration/module/#moduleparserjavascriptimportmeta, you can special it only for some files usingtest
, but looks angular-cli register own loader for typescript files so you need to look at docs how change options for iti.e.
For
import(...)
you have valid solution, i.e.:webpack keeps
import(...)
as is (looks like you want it)This error from
typescript
(not webpack), very strange that ts doesn’t supportnew URL(...)
as argument, but using.toString()
is good solution hereI tried your reproducible test repo and all works fine:
My code:
I want to ask you what do you expected? Do you want to keep
import(...)
as is and allow to async loading or do you want to bundle files which will be resolved innew URL()
?@eng-dibo
is not statically analizable, also
resolve
is not good idea to use here, becauseimport(...)
should have URL, not path@eng-dibo please create small reproducible repo. I’ll take a look
please use webpack magic comments
require(/* webpackIgnore: true */"blabla");
instead ofnativeRequire