webpack-contrib / imports-loader

Imports Loader
MIT License
520 stars 63 forks source link

Trying to access the global window object - Error: Cannot find module "window" #26

Closed happyvig closed 4 years ago

happyvig commented 8 years ago

Hi., I am trying to access window object inside main.js file by injecting it from index.js via imports-loader syntax and webpack-ing it.

ERROR in ./~/imports-loader?global=window!./main.js
Module not found: Error: Cannot resolve module 'window' in C:\Users\user\Desktop\webpack-dummy
 @ ./~/imports-loader?global=window!./main.js 2:13-30
C:\Users\user\Desktop\webpack-dummy\dist\bundle.js:55
        var global = __webpack_require__(!(function webpackMissingModule() { var e = new Error("Cannot find module \"window\""); e.code = 'M
ODULE_NOT_FOUND'; throw e; }()));                  ^

Error: Cannot find module "window"
C:\Users\user\Desktop\webpack-dummy\dist\bundle.js:62
        }.call(window));              
                 ^
ReferenceError: window is not defined
C:\Users\user\Desktop\webpack-dummy\dist\bundle.js:55
        var global = window;
                            ^
ReferenceError: window is not defined

index.js Tried this : require('imports?global=window!./main.js'); Tried this: require('imports?this=>window!./main.js'); Tried this : require('imports?global=>window!./main.js');

main.js

module.exports = ((function () {
    window.myName = "Clark Kent";
    console.log(window.myName);
    return {};
})());

webpack.config.js

module.exports = {
    entry: "./index.js",
    output: {
        path: 'dist',
        filename: "bundle.js"
    },
    resolve: {
        extensions: ['', '.js']
    },
    target: "web"    
};
SpaceK33z commented 7 years ago

Could you try again with imports-loader@0.7.0?

0x5929 commented 7 years ago

Any updates on this problem? Did it work with import-loader@0.7.0?

I have a similar problem in importing angularjs 1.5.11, whereas the angular script from node_module is as follows, and window is not defined when webpack bundles the file.

(function(window){ //angular code })(window) <-- this is not defined when webpack bundles the file.

even when webpack.config.target = 'web'

Can anyone shed some light on this issue?

Ziden commented 6 years ago

same issue here

tryed to require ("window") as well but it seems i cant mock window as a module

even with target: 'web'

cybermerlin commented 6 years ago

trouble is actual

nidhinmk commented 5 years ago

Is there a way to not to comment the imports and make it work?

alexander-akait commented 4 years ago

Fixed