spatialillusions / milsymbol

Military Symbols in JavaScript
www.spatialillusions.com/milsymbol
MIT License
570 stars 138 forks source link

Using directly in Node #299

Open kdickerson opened 2 months ago

kdickerson commented 2 months ago

I seem to be missing something. Without modifying milsymbol's package.json I can't seem to use it directly in a Node environment:

Note in the information below that Node is complaining about milsymbol's package.json--not my example project which already defines "type": "module".

$ node --version
v20.17.0

package.json:

{
  "name": "sandbox",
  "type": "module",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "milsymbol": "^2.2.0"
  }
}
  1. $ npm install
  2. $ echo "import ms from 'milsymbol';" > index.js
  3. 
    $ node index.js
    (node:32734) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
    (Use `node --trace-warnings ...` to show where the warning was created)
    /...../sandbox/node_modules/milsymbol/index.js:8
    import {
    ^^^^^^

SyntaxError: Cannot use import statement outside a module at wrapSafe (node:internal/modules/cjs/loader:1378:20) at Module._compile (node:internal/modules/cjs/loader:1428:41) at Module._extensions..js (node:internal/modules/cjs/loader:1548:10) at Module.load (node:internal/modules/cjs/loader:1288:32) at Module._load (node:internal/modules/cjs/loader:1104:12) at cjsLoader (node:internal/modules/esm/translators:346:17) at ModuleWrap. (node:internal/modules/esm/translators:286:7) at ModuleJob.run (node:internal/modules/esm/module_job:234:25) at async ModuleLoader.import (node:internal/modules/esm/loader:473:24) at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:123:5)



If I edit `node_modules/milsymbol/package.json` to add `"type": "module"` then everything works.

What's the proper way to use this package in a server-side Node environment?
spatialillusions commented 2 months ago

This is probably the same reason as for #284, and the way I tried to fix that broke even more things. If anyone can make a package.json that solves all problems, a pull request is very welcome.

kjellmf commented 2 months ago

Publishing NPM packages correctly is frustratingly hard. There is a recent article https://www.totaltypescript.com/how-to-create-an-npm-package on the topic. I have not read it yet, but it may give some hints on how to configure the project.

carlocorradini commented 1 month ago

@kdickerson Temporary solution:

  1. Install patch-package npm install --save patch-package
  2. Add patch file milsymbol+2.2.0.patch in patches directory (same directory where package.json resides):

    - package.json
    - patches
      | - milsymbol+2.2.0.patch

    milsymbol+2.2.0.patch:

    diff --git a/node_modules/milsymbol/index.d.ts b/node_modules/milsymbol/index.d.ts
    index d6c0451..5df96fb 100644
    --- a/node_modules/milsymbol/index.d.ts
    +++ b/node_modules/milsymbol/index.d.ts
    @@ -221,3 +221,5 @@ export function getVersion(): string;
    
    /** Sets the preferred standard. */
    export function setStandard(standard: "2525" | "APP6"): boolean;
    +
    +export default { Symbol };
    \ No newline at end of file
    diff --git a/node_modules/milsymbol/package.json b/node_modules/milsymbol/package.json
    index ea5e270..3c2602a 100644
    --- a/node_modules/milsymbol/package.json
    +++ b/node_modules/milsymbol/package.json
    @@ -2,8 +2,15 @@
      "name": "milsymbol",
      "version": "2.2.0",
      "description": "Milsymbol.js is a small library in pure javascript that creates symbols according to MIL-STD-2525 and APP6.",
    -  "main": "dist/milsymbol.js",
    -  "exports": "./index.js",
    +  "type": "module",
    +  "main": "./index.js",
    +  "module": "./index.js",
    +  "exports": {
    +    ".": {
    +      "import": "./index.js",
    +      "types": "./index.d.ts"
    +    }
    +  },
      "types": "index.d.ts",
      "directories": {
        "doc": "docs",
  3. Add postinstall script in package.json:
    // ...
    "scripts": {
       // ...
       "postinstall": "npx patch-package --error-on-fail --error-on-warn"
    }