【问题标题】:Hardhat.config.ts throwing "Cannot use import statement outside a module" for NestJSHardhat.config.ts 为 NestJS 抛出 \"Cannot use import statement outside a module\"
【发布时间】:2022-09-26 13:18:52
【问题描述】:

我想将安全帽与nestjs一起使用并通过HRE部署合同

  • 如果我使用 require 语句,插件将不起作用
  • 如果我使用 import 语句,它会给出 can\'t use import outside module

部署合约的脚本

const tokenFactory = await ethers.getContractFactory(\'Token\')
const token = await tokenFactory.deploy(args)

tsconfig.json

{
  \"compilerOptions\": {
    \"module\": \"CommonJS\",
    \"declaration\": true,
    \"removeComments\": true,
    \"emitDecoratorMetadata\": true,
    \"experimentalDecorators\": true,

    \"moduleResolution\": \"node\",
    \"resolveJsonModule\": true,
    \"esModuleInterop\": true,

    \"allowSyntheticDefaultImports\": true,
    \"target\": \"es2017\",
    \"sourceMap\": true,
    \"outDir\": \"./dist\",
    \"baseUrl\": \"./\",
    \"incremental\": true,
    \"skipLibCheck\": true,
    \"strictNullChecks\": false,
    \"noImplicitAny\": false,
    \"strictBindCallApply\": false,
    \"forceConsistentCasingInFileNames\": false,
    \"noFallthroughCasesInSwitch\": false
  },
  \"include\": [\"/**/*.ts\", \"hardhat.config.js\"],
  \"files\": [\"hardhat.config.js\"]
}

安全帽配置文件

require(\'hardhat\')

require(\'hardhat-deploy\')
require(\'hardhat-deploy-ethers\')
require(\'@nomicfoundation/hardhat-toolbox\')

require(\'@nomiclabs/hardhat-ethers\')

require(\'dotenv\').config()

module.exports = {
  solidity: \'0.8.15\',
  
  ---- NETWORKS SECTION ----

  paths: {
    sources: \'tokens\'
  }
}

    标签: typescript nestjs blockchain hardhat


    【解决方案1】:

    我设法解决它的方法是拥有这种结构:

    project/
      solidity/
        contracts/
        test/
        scripts/
        ...
        hardhat.config.ts
      src/
      dist/
      artifacts/
      ...
      hardhat.config.js
    

    基本上你需要配置 Solidity 文件夹

    npm i --save-dev hardhat
    npx hardhat
    

    使用打字稿模板。

    然后在 hardhat.config.ts 中有工件路径位于父文件夹中,如下所示

    paths: { artifacts: '../artifacts' }
    

    然后在项目文件夹中安装 hardhat 和 hardhat-toolbox

    npm i --save-dev hardhat
    npm i --save-dev @nomicfoundation/hardhat-toolbox
    

    并通过选择在没有模板的情况下对其进行配置

    Create an empty hardhat.config.js
    

    然后将其设置为默认的js配置

    require("@nomicfoundation/hardhat-toolbox");
    
    /** @type import('hardhat/config').HardhatUserConfig */
    module.exports = {
      solidity: "0.8.17"
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-23
      • 2022-11-26
      • 2021-11-21
      • 1970-01-01
      • 2022-11-21
      • 2021-07-26
      • 2020-08-30
      相关资源
      最近更新 更多