I am trying to modify this existing npm package without much success. I have wasted several hours troubleshooting would appreciate some guidance.
https://www.npmjs.com/package/dxf
I have forked the package into my Github account. I have then used npm to install the module locally (W10 running latest LTS Node).
npm i https://github.com/mgbolts/dxf
It installs with no errors but when I require it in my app, I get this error:
Error: Cannot find module 'S:\mgbolts\coding\Udemy\Nodejs\projects\plasma\node_modules\dxf\lib\index.js'.
Please verify that the package.json has a valid "main" entry
Looking in the dxf module folder, there is no lib subdirectory and the index.js file is located in the src subdirectory. I have modified the package.json file in the dxf package folder to reflect this. After this edit, I now get this error:
S:\mgbolts\coding\Udemy\Nodejs\projects\plasma\node_modules\dxf\src\index.js:1
import config from './config'
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (S:\mgbolts\coding\Udemy\Nodejs\projects\plasma\src\index.js:2:13)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
I have then tried adding adding a new entry ("type": "module") to the package.json file in the module folder. After that I get this error:
S:\mgbolts\coding\Udemy\Nodejs\projects\plasma\src\index.js:2
const dxf = require('dxf')
^
Error [ERR_REQUIRE_ESM]: require() of ES Module S:\mgbolts\coding\Udemy\Nodejs\projects\plasma\node_modules\dxf\src\index.js from S:\mgbolts\coding\Udemy\Nodejs\projects\plasma\src\index.js not supported.
My code is very simple to solve this issue, just a simple const dxf = require('dxf').
Here is an extract of the package.json file as with the two edits mentioned above:
"name": "dxf",
"version": "4.6.1",
"description": "DXF parser for node/browser",
"main": "src/index.js",
"type": "module",
"bin": {
"dxf-to-svg": "src/cli.js"
}
The matter has been resolved. I did not realise that you needed to compile the package when installing from github. Not something required when installing from NPM registry.
The module's package.json file has a script to run the compile process script.
npm run prepublishOnly
Took a little while to install the dependencies (browserify, babel, rimraf) but it compiled and now works...