jlguenego / node-expose-sspi

Expose Microsoft Windows SSPI to Node for SSO authentication.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Facing issue while building the app using webpack

seshunareshbonthula opened this issue · comments

When building the app using webpack, the build is successful but, I am seeing the below warning

WARNING in ./node_modules/node-expose-sspi/lib/api.js 2:17-34
Critical dependency: the request of a dependency is an expression
@ ./node_modules/node-expose-sspi/dist/index.js
@ ./app.js

When I start the application, I am receiving the below error message.

D:\Sources\API\src\dist\app.js:235101
throw e;
^

Error: Cannot find module './arch/x64/api.node'
at webpackEmptyContext (D:\Sources\API\src\dist\app.js:235099:10)
at Object. (D:\Sources\API\src\dist\app.js:15131:43)
at webpack_require (D:\Sources\API\src\dist\app.js:20:30)
at Object. (D:\Sources\API\src\dist\app.js:29199:10)
at webpack_require (D:\Sources\API\src\dist\app.js:20:30)
at Object. (D:\Sources\API\src\dist\app.js:123324:13)
at webpack_require (D:\Sources\API\src\dist\app.js:20:30)
at D:\Sources\API\src\dist\app.js:84:18
at Object. (D:\Sources\API\src\dist\app.js:87:10)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND'
}

Later, when I wet ahead and changed the app.js file present in the below folder, I was able to get it working

\node_modules\node-expose-sspi\lib\api.js

Changed code: module.exports = require("./arch/"+ process.arch + "/api.node");

Here is the webpack configuration I am using.

webpack.config.js

const path = require('path');
// const { CleanWebpackPlugin } = require('clean-webpack-plugin');
var nodeExternals = require('webpack-node-externals');
module.exports = {
entry: {
app: './app.js',
},
output: {
path: path.join(__dirname, 'dist'),
publicPath: '/',
filename: '[name].js'
},
target: 'node',
node: {
// Need this when working with express, otherwise the build fails
__dirname: false, // if you don't put this is, __dirname
__filename: false, // and __filename return blank or /
},
optimization: {
minimize: false
},
module: {
rules: [
{
test: /.node$/,
loader: 'node-loader'
}
]
}
// "plugins": [
// new CleanWebpackPlugin({protectWebpackAssets: false, cleanOnceBeforeBuildPatterns: !['./Web.config']})
// ]
}

Can you please let me know if their is any issue or suggest me if I am doing anything wrong

Hello,

First of all, I do not understand why using webpack for a node application. If you want to do a commercial app and not let the code accessible to the client, I think the best way is to use the pkg node module that will build a .exe file.

But this is a personal point of view. And I am going to help as I can. First please update to the last version the node-expose-sspi module. Because you have not specified the version of node-expose-sspi, please be sure to use the last.

I wanted to verify what your issue is and I reproduced an example of webpack - express - node-expose-sspi project.

You will find it under the examples directory : https://github.com/jlguenego/node-expose-sspi/tree/master/examples/express-webpack

And it perfectly work, without any warnings.

Please test it and confirm. If ok, I suggest closing this defect.
If not let me know. Please show more info about versions, etc.

I've encountered the same issue with Webpack 5.66.0 and node-expose-sspi 0.1.59.

The root of the problem is this dynamic import:

const filename = `./arch/${process.arch}/node-expose-sspi.node`;
module.exports = require(filename);

Webpack doesn't seem to be able to derive the list of possible files from this pattern, so it doesn't bundle .node files to the output folder.

However, if filename is inlined, everything works: both possible .node files are successfully bundled and the correct one is picked up dynamically.

module.exports = require(`./arch/${process.arch}/node-expose-sspi.node`);

If anyone else stumbles upon this issue, the workaround that I've used is to transform this line during bundling by string-replace-loader:

// webpack.config
return {
    // ...
    module: {
        rules: [
            {
                test: /api\.js$/,
                include: /node-expose-sspi/,
                loader: 'string-replace-loader',
                options: {
                    search: 'module.exports = require(filename);',
                    replace: 'module.exports = require(`./arch/${process.arch}/node-expose-sspi.node`);',
                }
            },
            {
                test: /\.node$/,
                loader: 'node-loader',
            },
            // ...
        ]
    },
    // ...
}

I've encountered the same issue with Webpack 5.66.0 and node-expose-sspi 0.1.59.

The root of the problem is this dynamic import:

const filename = `./arch/${process.arch}/node-expose-sspi.node`;
module.exports = require(filename);

Webpack doesn't seem to be able to derive the list of possible files from this pattern, so it doesn't bundle .node files to the output folder.

However, if filename is inlined, everything works: both possible .node files are successfully bundled and the correct one is picked up dynamically.

module.exports = require(`./arch/${process.arch}/node-expose-sspi.node`);

If anyone else stumbles upon this issue, the workaround that I've used is to transform this line during bundling by string-replace-loader:

// webpack.config
return {
    // ...
    module: {
        rules: [
            {
                test: /api\.js$/,
                include: /node-expose-sspi/,
                loader: 'string-replace-loader',
                options: {
                    search: 'module.exports = require(filename);',
                    replace: 'module.exports = require(`./arch/${process.arch}/node-expose-sspi.node`);',
                }
            },
            {
                test: /\.node$/,
                loader: 'node-loader',
            },
            // ...
        ]
    },
    // ...
}

this is my issue when apply tour code.

ERROR in ./node_modules/node-expose-sspi/lib/arch/ia32/node-expose-sspi.node 1:2
Module parse failed: Unexpected character '�' (1:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
@ ./node_modules/node-expose-sspi/lib/arch/ sync ^./.*/node-expose-sspi.node$ ./ia32/node-expose-sspi.node
@ ./node_modules/node-expose-sspi/lib/api.js 2:17-72
@ ./node_modules/node-expose-sspi/dist/index.js 29:13-34
@ ./dist/app.js 32:25-52

ERROR in ./node_modules/node-expose-sspi/lib/arch/x64/node-expose-sspi.node 1:2
Module parse failed: Unexpected character '�' (1:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
@ ./node_modules/node-expose-sspi/lib/arch/ sync ^./.*/node-expose-sspi.node$ ./x64/node-expose-sspi.node
@ ./node_modules/node-expose-sspi/lib/api.js 2:17-72
@ ./node_modules/node-expose-sspi/dist/index.js 29:13-34
@ ./dist/app.js 32:25-52

and this is my webpack config:

const webpack = require('webpack');
// const nodeExternals = require('webpack-node-externals')

module.exports = {
entry: "./dist/app.js",
output: {
filename: ../prod/built.js,
chunkFormat: 'commonjs'
},
target: 'node',
mode: 'production',
// experiments: {
// outputModule: true,
// },
module: {
rules: [
{
test: /api.js$/,
include: /node-expose-sspi/,
loader: 'string-replace-loader',
options: {
search: 'module.exports = require(filename);',
replace: 'module.exports = require(./arch/${process.arch}/node-expose-sspi.node);',
}
}
],
},
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
],
resolve: {
fallback: {
bufferutil: false,
utf8: false,
fs: false,
tls: false,
net: false,
path: false,
zlib: false,
http: false,
https: false,
stream: false,
crypto: false,
assert: false,
}
},
// externals: [nodeExternals()],
};

can you help me ?