I searched for this error and found dozens of answers, but none worked for me. I'm writing microservices using serverless framework with node.js (v12.22.7) & typescript (v4.2.4)
Everything worked so far, until I tried to add PKIJS as a dependency
My code snippet:
/// <reference types="@types/pkijs" />
import OCSPResponse from "pkijs/src/OCSPResponse";
const ocspRespSimpl = new OCSPResponse({ schema: asn1.result });
fails with SyntaxError: Cannot use import statement outside a module
my tsconfig.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Node 12",
"compilerOptions": {
"outDir": "./dist",
"incremental": true,
"target": "ES2019",
"module": "commonjs",
"lib": [
"ES2019"
],
"declaration": true,
"sourceMap": true,
"composite": false,
"removeComments": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"moduleResolution": "node",
"types": [
"node"
],
"esModuleInterop": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "__tests__/**/*"]
}
my package.json
{
"name": "@services/admin-apis",
"version": "1.0.0",
"private": true,
"scripts": {
"clean": "rimraf ./dist ./.serverless",
"lint": "eslint -c ../../../.eslintrc.js --fix ./src/**/*.ts",
"prebuild": "yarn lint",
"build": "tsc -b",
"package": "sls package",
"deploy": "sls deploy",
"test": "jest -c ../../../jest.config.js --rootDir . --passWithNoTests --colors --silent"
},
"devDependencies": {
"@types/pkijs": "^0.0.12",
},
"dependencies": {
"@peculiar/x509": "^1.6.0",
"@peculiar/webcrypto": "^1.2.2",
"@peculiar/asn1-ocsp": "^2.0.38",
"pkijs": "^2.2.1",
}
}
Obviously, I cannot modify PKIJS, how can I make it so that I can use that dependency? I tried already adding "type": "module" to package.json but it didn't work, also tried several combinations of target and module in tsconfig, didn't work either.